diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-02-12 15:55:21 +0800 | 
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-02-12 15:55:21 +0800 | 
| commit | 10eb95869601e145b1d8bc909424777c25752d51 (patch) | |
| tree | 49449a4076ded9bd937a51679318edbe2a532cae /works/life/java-practice | |
| parent | 29ba3e88b1a7425fe00af0005b8a8228103aa21c (diff) | |
| parent | f8c10dd1fc55e60f35286475356e48c4f642eb63 (diff) | |
| download | crupest-10eb95869601e145b1d8bc909424777c25752d51.tar.gz crupest-10eb95869601e145b1d8bc909424777c25752d51.tar.bz2 crupest-10eb95869601e145b1d8bc909424777c25752d51.zip  | |
import(life): IMPORT crupest/life COMPLETE.
Diffstat (limited to 'works/life/java-practice')
34 files changed, 1471 insertions, 0 deletions
diff --git a/works/life/java-practice/.gitignore b/works/life/java-practice/.gitignore new file mode 100644 index 0000000..acec74a --- /dev/null +++ b/works/life/java-practice/.gitignore @@ -0,0 +1,60 @@ +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ +.apt_generated_test/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +# Uncomment this line if you wish to ignore the project description file. +# Typically, this file would be tracked if it contains build/dependency configurations: +#.project diff --git a/works/life/java-practice/javatest/.classpath b/works/life/java-practice/javatest/.classpath new file mode 100644 index 0000000..9af0373 --- /dev/null +++ b/works/life/java-practice/javatest/.classpath @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?>
 +<classpath>
 +	<classpathentry kind="src" path="src"/>
 +	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
 +		<attributes>
 +			<attribute name="module" value="true"/>
 +		</attributes>
 +	</classpathentry>
 +	<classpathentry kind="output" path="bin"/>
 +</classpath>
 diff --git a/works/life/java-practice/javatest/.project b/works/life/java-practice/javatest/.project new file mode 100644 index 0000000..34df9f4 --- /dev/null +++ b/works/life/java-practice/javatest/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?>
 +<projectDescription>
 +	<name>javatest</name>
 +	<comment></comment>
 +	<projects>
 +	</projects>
 +	<buildSpec>
 +		<buildCommand>
 +			<name>org.eclipse.jdt.core.javabuilder</name>
 +			<arguments>
 +			</arguments>
 +		</buildCommand>
 +	</buildSpec>
 +	<natures>
 +		<nature>org.eclipse.jdt.core.javanature</nature>
 +	</natures>
 +</projectDescription>
 diff --git a/works/life/java-practice/javatest/src/javatest/ArrayDemo2.java b/works/life/java-practice/javatest/src/javatest/ArrayDemo2.java new file mode 100644 index 0000000..8d7b2ae --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/ArrayDemo2.java @@ -0,0 +1,13 @@ +package javatest;
 +
 +public class ArrayDemo2 {
 +	public static void main(String args[]) {
 +		int score[][] = { { 10, 3 }, { -1, 119, -51 }, { 100, 56, 90, 49 } };
 +		for (int i = 0; i < score.length; i++) {
 +			for (int j = 0; j < score[i].length; j++) {
 +				System.out.print(score[i][j] + "\t");
 +			}
 +			System.out.println("");
 +		}
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/ArrayDemo3.java b/works/life/java-practice/javatest/src/javatest/ArrayDemo3.java new file mode 100644 index 0000000..f053b4b --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/ArrayDemo3.java @@ -0,0 +1,31 @@ +package javatest;
 +
 +public class ArrayDemo3 {
 +	public static void main(String args[]) {
 +		int score[] = { 25, 78, 35, 84, 69, 74, 12, 56 };
 +		int age[] = { 32, 52, 12, 63, 42, 15, 75, 23, 56 };
 +		sort(score); // 调用排序方法
 +		print(score); // 调用输出方法
 +		System.out.println("\n-----------------------------------------");
 +		sort(age);
 +		print(age);
 +	}
 +
 +	public static void sort(int temp[]) { // 数组排序
 +		for (int i = 1; i < temp.length; i++) { // 冒泡算法
 +			for (int j = 0; j < temp.length; j++) {
 +				if (temp[i] < temp[j]) {
 +					int x = temp[i];
 +					temp[i] = temp[j];
 +					temp[j] = x;
 +				}
 +			}
 +		}
 +	}
 +
 +	public static void print(int x[]) { // 数组输出
 +		for (int i = 0; i < x.length; i++) {
 +			System.out.print(x[i] + " , ");
 +		}
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/E0201.java b/works/life/java-practice/javatest/src/javatest/E0201.java new file mode 100644 index 0000000..5cda62a --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/E0201.java @@ -0,0 +1,21 @@ +package javatest;
 +
 +public class E0201 {
 +	public static int max(int a, int b) {
 +		return a > b ? a : b;
 +	}
 +
 +	public static double max(double a, double b) {
 +		return a > b ? a : b;
 +	}
 +
 +	public static int max(int a, int b, int c) {
 +		return max(a, max(b, c));
 +	}
 +
 +	public static void main(String[] args) {
 +		System.out.println(max(1, 2));
 +		System.out.println(max(2.2, 3.3));
 +		System.out.println(max(1, 2, 3));
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/E0202.java b/works/life/java-practice/javatest/src/javatest/E0202.java new file mode 100644 index 0000000..c74beec --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/E0202.java @@ -0,0 +1,12 @@ +package javatest;
 +
 +public class E0202 {
 +	public static int F(int n) {
 +		if (n == 1 || n == 2) return 1;
 +		return F(n - 2) + F(n - 1);
 +	}
 +	
 +	public static void main(String[] args) {
 +		System.out.println(F(6));
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/E0203.java b/works/life/java-practice/javatest/src/javatest/E0203.java new file mode 100644 index 0000000..7bec46f --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/E0203.java @@ -0,0 +1,26 @@ +package javatest;
 +
 +public class E0203 {
 +	public static void main(String[] args) {
 +		int[] array = new int[] { 1, 2, 3, 4 };
 +		intArray.printArray(array);
 +		System.out.println();
 +		intArray.printArray(intArray.reverse(array));
 +	}
 +}
 +
 +class intArray {
 +	public static void printArray(int[] array) {
 +		for (int i = 0; i < array.length; i++) {
 +			System.out.print(array[i] + " ");
 +		}
 +	}
 +
 +	public static int[] reverse(int[] list) {
 +		int[] result = new int[list.length];
 +		for (int i = 0; i < list.length; i++) {
 +			result[i] = list[list.length - i - 1];
 +		}
 +		return result;
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/E0204.java b/works/life/java-practice/javatest/src/javatest/E0204.java new file mode 100644 index 0000000..c79e33c --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/E0204.java @@ -0,0 +1,90 @@ +package javatest;
 +
 +import java.util.HashSet;
 +import java.util.Random;
 +
 +public class E0204 {
 +	public static void main(String[] args) {
 +		Random r = new Random();
 +		HashSet<Card> set = new HashSet<Card>();
 +
 +		while (set.size() != 4) {
 +			set.add(Card.random(r));
 +		}
 +
 +		for (Card card : set) {
 +			System.out.println(card);
 +		}
 +	}
 +}
 +
 +class Card {
 +	public static final String[] suits = { "Spades", "Hearts", "Clubs", "Diamonds" };
 +	public static final String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen",
 +			"King" };
 +
 +	public static Card random(Random r) {
 +		return new Card(suits[r.nextInt(suits.length)], ranks[r.nextInt(ranks.length)]);
 +	}
 +
 +	public Card(String suit, String rank) {
 +		super();
 +		this.suit = suit;
 +		this.rank = rank;
 +	}
 +
 +	private String suit;
 +	private String rank;
 +
 +	public String getSuit() {
 +		return suit;
 +	}
 +
 +	public void setSuit(String suit) {
 +		this.suit = suit;
 +	}
 +
 +	public String getRank() {
 +		return rank;
 +	}
 +
 +	public void setRank(String rank) {
 +		this.rank = rank;
 +	}
 +
 +	@Override
 +	public int hashCode() {
 +		final int prime = 31;
 +		int result = 1;
 +		result = prime * result + ((rank == null) ? 0 : rank.hashCode());
 +		result = prime * result + ((suit == null) ? 0 : suit.hashCode());
 +		return result;
 +	}
 +
 +	@Override
 +	public boolean equals(Object obj) {
 +		if (this == obj)
 +			return true;
 +		if (obj == null)
 +			return false;
 +		if (getClass() != obj.getClass())
 +			return false;
 +		Card other = (Card) obj;
 +		if (rank == null) {
 +			if (other.rank != null)
 +				return false;
 +		} else if (!rank.equals(other.rank))
 +			return false;
 +		if (suit == null) {
 +			if (other.suit != null)
 +				return false;
 +		} else if (!suit.equals(other.suit))
 +			return false;
 +		return true;
 +	}
 +
 +	@Override
 +	public String toString() {
 +		return "Card [suit=" + suit + ", rank=" + rank + "]";
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/Main.java b/works/life/java-practice/javatest/src/javatest/Main.java new file mode 100644 index 0000000..4c4a70c --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/Main.java @@ -0,0 +1,7 @@ +package javatest;
 +
 +public class Main {
 +	public static void main(String[] args) {
 +		System.out.println("Hello world!");
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/MethodDemo2.java b/works/life/java-practice/javatest/src/javatest/MethodDemo2.java new file mode 100644 index 0000000..161de5d --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/MethodDemo2.java @@ -0,0 +1,30 @@ +package javatest;
 +
 +public class MethodDemo2 {
 +	public static void main(String args[]) {
 +		int one = add(29, 58); // 调用addOne()方法
 +		int three = add(25, 58, 10); // 调用addOne()方法
 +		float two = add(26.35f, -12.84f); // 调用addTwo()方法
 +		System.out.println("add(int x,int y)=" + one);
 +		System.out.println("add(int x,int y,int z)=" + three);
 +		System.out.println("add(float x,float y)=" + two);
 +	}
 +
 +	public static int add(int x, int y) { // 定义方法,两个整数加法,返回一个整型数据
 +		int temp = 0; // temp为局部变量,只在此方法中有效
 +		temp = x + y;
 +		return temp; // 返回计算结果
 +	}
 +
 +	public static int add(int x, int y, int z) { // 定义方法,两个整数加法,返回一个整型数据
 +		int temp = 0; // temp为局部变量,只在此方法中有效
 +		temp = x + y + z;
 +		return temp; // 返回计算结果
 +	}
 +
 +	public static float add(float x, float y) {
 +		float temp = 0;
 +		temp = x + y;
 +		return temp;
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0301/Circle.java b/works/life/java-practice/javatest/src/javatest/e0301/Circle.java new file mode 100644 index 0000000..ec380ec --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0301/Circle.java @@ -0,0 +1,33 @@ +package javatest.e0301;
 +
 +public class Circle {
 +	private static int numOfObjects = 0;
 +
 +	public static int getNumOfObjects() {
 +		return numOfObjects;
 +	}
 +
 +	private double radius;
 +
 +	public Circle(double radius) {
 +		this.radius = radius;
 +		numOfObjects++;
 +	}
 +
 +	public double getArea() {
 +		return radius * radius * Math.PI;
 +	}
 +
 +	public static void printCircle(Circle c) {
 +		System.out.println("Radius of circle is " + c.radius);
 +		System.out.println("Area of circle is " + c.getArea());
 +	}
 +
 +	public static void main(String[] args) {
 +		Circle c1 = new Circle(2);
 +		Circle c2 = new Circle(3);
 +		Circle.printCircle(c1);
 +		Circle.printCircle(c2);
 +		System.out.println("Num of objects is " + Circle.getNumOfObjects());
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0302/Rational.java b/works/life/java-practice/javatest/src/javatest/e0302/Rational.java new file mode 100644 index 0000000..772cf12 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0302/Rational.java @@ -0,0 +1,45 @@ +package javatest.e0302;
 +
 +public class Rational {
 +	private long numerator;
 +	private long denominator;
 +
 +	private static long gcd(long a, long b) {
 +		return b != 0 ? gcd(b, a % b) : a;
 +	}
 +
 +	public Rational(long numerator, long denominator) {
 +		long d = gcd(numerator, denominator);
 +		this.numerator = numerator / d;
 +		this.denominator = denominator / d;
 +	}
 +
 +	public Rational add(Rational secondRational) {
 +		long d = gcd(this.denominator, secondRational.denominator);
 +		long dd = this.denominator / d * secondRational.denominator;
 +		long a = this.numerator * (dd / this.denominator);
 +		long b = secondRational.numerator * (dd / secondRational.denominator);
 +		return new Rational(a + b, dd);
 +	}
 +
 +	public Rational subtract(Rational secondRational) {
 +		long d = gcd(this.denominator, secondRational.denominator);
 +		long dd = this.denominator / d * secondRational.denominator;
 +		long a = this.numerator * (dd / this.denominator);
 +		long b = secondRational.numerator * (dd / secondRational.denominator);
 +		return new Rational(a - b, dd);
 +	}
 +
 +	public Rational multiply(Rational secondRational) {
 +		return new Rational(this.numerator * secondRational.numerator, this.denominator * secondRational.denominator);
 +	}
 +
 +	public Rational divide(Rational secondRational) {
 +		return new Rational(this.numerator * secondRational.denominator, this.denominator * secondRational.numerator);
 +	}
 +
 +	@Override
 +	public String toString() {
 +		return denominator == 1 ? Long.toString(numerator) : numerator + "/" + denominator;
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0302/TestRationalClass.java b/works/life/java-practice/javatest/src/javatest/e0302/TestRationalClass.java new file mode 100644 index 0000000..2be02b9 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0302/TestRationalClass.java @@ -0,0 +1,12 @@ +package javatest.e0302;
 +
 +public class TestRationalClass {
 +	public static void main(String[] args) {
 +		Rational r1 = new Rational(2, 1);
 +		Rational r2 = new Rational(2, 3);
 +		System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
 +		System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
 +		System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
 +		System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));		
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0401/Program.java b/works/life/java-practice/javatest/src/javatest/e0401/Program.java new file mode 100644 index 0000000..c7472b8 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0401/Program.java @@ -0,0 +1,72 @@ +package javatest.e0401;
 +
 +public class Program {
 +	public static boolean equalArea(GeometricObject object1, GeometricObject object2) {
 +		return object1.findArea() == object2.findArea();
 +	}
 +
 +	public static void displayGeometricObject(GeometricObject object) {
 +		if (object instanceof Circle) {
 +			System.out.println("A Circle.");
 +		} else if (object instanceof Rectangle) {
 +			System.out.println("A Rectangle.");
 +		}
 +	}
 +
 +	public static void main(String[] args) {
 +		Circle circle = new Circle(3);
 +		Rectangle rectangle = new Rectangle(2, 4);
 +
 +		System.out.println(equalArea(circle, rectangle));
 +		displayGeometricObject(circle);
 +		displayGeometricObject(rectangle);
 +	}
 +}
 +
 +abstract class GeometricObject {
 +	protected String color;
 +
 +	public abstract double findArea();
 +
 +	public abstract double findPerimeter();
 +}
 +
 +class Circle extends GeometricObject {
 +
 +	private double radius;
 +
 +	public Circle(double radius) {
 +		this.radius = radius;
 +	}
 +
 +	@Override
 +	public double findArea() {
 +		return radius * radius * Math.PI;
 +	}
 +
 +	@Override
 +	public double findPerimeter() {
 +		return radius * 2 * Math.PI;
 +	}
 +}
 +
 +class Rectangle extends GeometricObject {
 +
 +	private double width;
 +	private double height;
 +
 +	public Rectangle(double width, double height) {
 +		this.width = width;
 +		this.height = height;
 +	}
 +
 +	@Override
 +	public double findArea() {
 +		return width * height;
 +	}
 +
 +	@Override
 +	public double findPerimeter() {
 +		return (width + height) * 2;
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0402/Person.java b/works/life/java-practice/javatest/src/javatest/e0402/Person.java new file mode 100644 index 0000000..b7e0e14 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0402/Person.java @@ -0,0 +1,40 @@ +package javatest.e0402;
 +
 +public class Person {
 +	public void cut() {
 +	}
 +
 +	public static void main(String[] args) {
 +		Person player = new Player();
 +		Person cooker = new Cooker();
 +		Person cuter = new Cuter();
 +
 +		player.cut();
 +		cooker.cut();
 +		cuter.cut();
 +	}
 +}
 +
 +/** 演员 */
 +class Player extends Person {
 +	/** 重写 */
 +	public void cut() {
 +		System.out.println("停止演戏");
 +	}
 +}
 +
 +/** 厨师 */
 +class Cooker extends Person {
 +	/** 重写 */
 +	public void cut() {
 +		System.out.println("开始切菜");
 +	}
 +}
 +
 +/** 厨师 */
 +class Cuter extends Person {
 +	/** 重写 */
 +	public void cut() {
 +		System.out.println("开始剪头发");
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0501/Main.java b/works/life/java-practice/javatest/src/javatest/e0501/Main.java new file mode 100644 index 0000000..7639bc8 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0501/Main.java @@ -0,0 +1,93 @@ +package javatest.e0501;
 +
 +import java.util.Scanner;
 +
 +interface Soundable {
 +	void makeSound();
 +
 +	void turnUpVolumn();
 +
 +	void turnDownVolumn();
 +}
 +
 +class Radio implements Soundable {
 +	private int volumn = 5;
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("Radio sound...");
 +	}
 +
 +	@Override
 +	public void turnUpVolumn() {
 +		volumn++;
 +	}
 +
 +	@Override
 +	public void turnDownVolumn() {
 +		volumn--;
 +	}
 +}
 +
 +class Walkman implements Soundable {
 +	private int volumn = 5;
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("Walkman sound...");
 +	}
 +
 +	@Override
 +	public void turnUpVolumn() {
 +		volumn++;
 +	}
 +
 +	@Override
 +	public void turnDownVolumn() {
 +		volumn--;
 +	}
 +}
 +
 +class Mobilephone implements Soundable {
 +	private int volumn = 5;
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("Mobilephone sound...");
 +	}
 +
 +	@Override
 +	public void turnUpVolumn() {
 +		volumn++;
 +	}
 +
 +	@Override
 +	public void turnDownVolumn() {
 +		volumn--;
 +	}
 +}
 +
 +public class Main {
 +	public static void main(String[] args) {
 +		Scanner scanner = new Scanner(System.in);
 +		String t = scanner.next();
 +		Soundable soundable = null;
 +
 +		switch (t) {
 +		case "radio":
 +			soundable = new Radio();
 +			break;
 +		case "walkman":
 +			soundable = new Walkman();
 +			break;
 +		case "mobilephone":
 +			soundable = new Mobilephone();
 +			break;
 +		default:
 +			System.out.println("Unknown device.");
 +			System.exit(1);
 +		}
 +		
 +		soundable.makeSound();
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0502/Main.java b/works/life/java-practice/javatest/src/javatest/e0502/Main.java new file mode 100644 index 0000000..405a278 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0502/Main.java @@ -0,0 +1,101 @@ +package javatest.e0502;
 +
 +abstract class Animal {
 +	public Animal(String name, String color, String type) {
 +		super();
 +		this.name = name;
 +		this.color = color;
 +		this.type = type;
 +	}
 +
 +	private String name;
 +	private String color;
 +	private String type;
 +
 +	abstract void eat();
 +
 +	abstract void makeSound();
 +
 +	public String getName() {
 +		return name;
 +	}
 +
 +	public void setName(String name) {
 +		this.name = name;
 +	}
 +
 +	public String getColor() {
 +		return color;
 +	}
 +
 +	public void setColor(String color) {
 +		this.color = color;
 +	}
 +
 +	public String getType() {
 +		return type;
 +	}
 +
 +	public void setType(String type) {
 +		this.type = type;
 +	}
 +}
 +
 +class Rabbit extends Animal {
 +	public Rabbit() {
 +		super("兔子", "白色", "哺乳类");
 +	}
 +
 +	@Override
 +	public void eat() {
 +		System.out.println("兔子吃...");
 +	}
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("兔子叫...");
 +	}
 +}
 +
 +interface Swimmable {
 +	void swim();
 +}
 +
 +class Frog extends Animal implements Swimmable {
 +	public Frog() {
 +		super("青蛙", "绿色", "非哺乳类");
 +	}
 +
 +	@Override
 +	public void eat() {
 +		System.out.println("青蛙吃...");
 +	}
 +
 +	@Override
 +	public void makeSound() {
 +		System.out.println("青蛙叫...");
 +	}
 +
 +	@Override
 +	public void swim() {
 +		System.out.println("青蛙游泳...");
 +	}
 +}
 +
 +public class Main {
 +	public static void main(String[] args) {
 +		Rabbit rabbit = new Rabbit();
 +		Frog frog = new Frog();
 +		
 +		Animal animal = rabbit;
 +		animal.eat();
 +		animal.makeSound();
 +		
 +		animal = frog;
 +		animal.eat();
 +		animal.makeSound();
 +		
 +		Swimmable swimmable = frog;
 +		swimmable.swim();
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0701/Main.java b/works/life/java-practice/javatest/src/javatest/e0701/Main.java new file mode 100644 index 0000000..0211750 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0701/Main.java @@ -0,0 +1,25 @@ +package javatest.e0701;
 +
 +public class Main {
 +	private static int countSubstring(String string, String substring) {
 +		int count = 0;
 +		int currentIndex = 0;
 +
 +		while (true) {
 +			int i = string.indexOf(substring, currentIndex);
 +			if (i == -1)
 +				break;
 +			count++;
 +			currentIndex = i + 1;
 +		}
 +
 +		return count;
 +	}
 +
 +	public static void main(String[] args) {
 +		String string = "ababaaba";
 +		String substring = "aba";
 +
 +		System.out.println(substring + "在" + string + "中出现了" + countSubstring(string, substring) + "次!");
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/e0702/Main.java b/works/life/java-practice/javatest/src/javatest/e0702/Main.java new file mode 100644 index 0000000..15ececc --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/e0702/Main.java @@ -0,0 +1,15 @@ +package javatest.e0702;
 +
 +import java.util.*;
 +import java.time.*;
 +import java.time.format.DateTimeFormatter;
 +
 +public class Main {
 +	public static void main(String[] args) {
 +		try (Scanner scanner = new Scanner(System.in)) {
 +			LocalDateTime birthday = LocalDate.parse(scanner.nextLine(), DateTimeFormatter.ISO_DATE).atStartOfDay();
 +			LocalDateTime now = LocalDate.now().atStartOfDay();
 +			System.out.println("你的生日距今天" + Duration.between(birthday, now).toDays() + "天!");
 +		}
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0301/AreaAndLength.java b/works/life/java-practice/javatest/src/javatest/p0301/AreaAndLength.java new file mode 100644 index 0000000..5216fb1 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0301/AreaAndLength.java @@ -0,0 +1,120 @@ +package javatest.p0301;
 +
 +class Trangle {
 +	double sideA, sideB, sideC, area, length;
 +	boolean boo;
 +
 +	public Trangle(double a, double b, double c) {
 +		sideA = a;
 +		sideB = b;
 +		sideC = c;// 参数a,b,c分别赋值给sideA,sideB,sideC
 +		if (a + b > c && a + c > b && c + b > a) // a,b,c构成三角形的条件表达式
 +		{
 +			boo = true; // 给boo赋值。
 +		} else {
 +			boo = false; // 给boo赋值。
 +		}
 +	}
 +
 +	double getLength() {
 +		if (boo) {
 +			length = sideA + sideB + sideC;
 +			return length;
 +		} else {
 +			System.out.println("不是一个三角形,不能计算周长");
 +			return 0;
 +		}
 +		// 方法体,要求计算出length的值并返回
 +	}
 +
 +	public double getArea() {
 +		if (boo) {
 +			double p = (sideA + sideB + sideC) / 2.0;
 +			area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
 +			return area;
 +		} else {
 +			System.out.println("不是一个三角形,不能计算面积");
 +			return 0;
 +		}
 +	}
 +
 +	public void setABC(double a, double b, double c) {
 +		sideA = a;
 +		sideB = b;
 +		sideC = c; // 参数a,b,c分别赋值给sideA,sideB,sideC
 +		if (a + b > c && a + c > b && c + b > a) // a,b,c构成三角形的条件表达式
 +		{
 +			boo = true; // 给boo赋值。
 +		} else {
 +			boo = false; // 给boo赋值。
 +		}
 +	}
 +}
 +
 +class Lader {
 +	double above, bottom, height, area;
 +
 +	Lader(double a, double b, double h) {
 +		above = a;
 +		bottom = b;
 +		height = h;
 +		// 方法体,将参数a,b,c分别赋值给above,bottom,height
 +	}
 +
 +	double getArea() {
 +		area = (above + bottom) / 2 * height;
 +		return area;
 +		// 方法体,,要求计算出area返回
 +	}
 +}
 +
 +class Circle {
 +	double radius, area;
 +
 +	Circle(double r) {
 +		radius = r; // 方法体
 +	}
 +
 +	double getArea() {
 +		return 3.14 * radius * radius; // 方法体,要求计算出area返回
 +	}
 +
 +	double getLength() {
 +		return 3.14 * 2 * radius; // getArea方法体的代码,要求计算出length返回
 +	}
 +
 +	void setRadius(double newRadius) {
 +		radius = newRadius;
 +	}
 +
 +	double getRadius() {
 +		return radius;
 +	}
 +}
 +
 +public class AreaAndLength {
 +	public static void main(String args[]) {
 +		double length, area;
 +		Circle circle = null;
 +		Trangle trangle;
 +		Lader lader;
 +		circle = new Circle(10); // 创建对象circle
 +		trangle = new Trangle(3, 4, 5);// 创建对象trangle。
 +		lader = new Lader(3, 4, 10); // 创建对象lader
 +		length = circle.getLength(); // circle调用方法返回周长并赋值给length
 +		System.out.println("圆的周长:" + length);
 +		area = circle.getArea();// circle调用方法返回面积并赋值给area
 +		System.out.println("圆的面积:" + area);
 +		length = trangle.getLength(); // trangle调用方法返回周长并赋值给length
 +		System.out.println("三角形的周长:" + length);
 +		area = trangle.getArea(); // trangle调用方法返回面积并赋值给area
 +		System.out.println("三角形的面积:" + area);
 +		area = lader.getArea(); // lader调用方法返回面积并赋值给area
 +		System.out.println("梯形的面积:" + area);
 +		trangle.setABC(12, 34, 1); // trangle调用方法设置三个边,要求将三个边修改为12,34,1。
 +		area = trangle.getArea(); // trangle调用方法返回面积并赋值给area
 +		System.out.println("三角形的面积:" + area);
 +		length = trangle.getLength(); // trangle调用方法返回周长并赋值给length
 +		System.out.println("三角形的周长:" + length);
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0302/Example.java b/works/life/java-practice/javatest/src/javatest/p0302/Example.java new file mode 100644 index 0000000..600a899 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0302/Example.java @@ -0,0 +1,47 @@ +package javatest.p0302;
 +
 +class A {
 +	float a; // 声明一个float型实例变量a
 +	static float b; // 声明一个float型类变量b,即static变量b
 +
 +	void setA(float a) {
 +		this.a = a; // 将参数a的值赋值给成员变量a
 +	}
 +
 +	void setB(float b) {
 +		this.b = b; // 将参数b的值赋值给成员变量b
 +	}
 +
 +	float getA() {
 +		return a;
 +	}
 +
 +	float getB() {
 +		return b;
 +	}
 +
 +	void inputA() {
 +		System.out.println(a);
 +	}
 +
 +	static void inputB() {
 +		System.out.println(b);
 +	}
 +}
 +
 +public class Example {
 +	public static void main(String args[]) {
 +		A.b = 100; // 通过类名操作类变量b,并赋值100
 +		A.inputB(); // 通过类名调用方法inputB()
 +		A cat = new A();
 +		A dog = new A();
 +		cat.setA(200); // cat象调用方法setA(int a)将cat的成员a的值设置为200
 +		cat.setB(400); // cat调用方法setB(int b)将cat的成员b的值设置为400
 +		dog.setA(150); // dog象调用方法setA(int a)将dog的成员a的值设置为150
 +		dog.setB(300); // dog调用方法setB(int b)将dog的成员b的值设置为300
 +		cat.inputA(); // cat调用inputA()。
 +		cat.inputB(); // cat调用inputB()。
 +		dog.inputA(); // dog调用inputA()。
 +		dog.inputB(); // dog调用inputB()。
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0303/SunRise.java b/works/life/java-practice/javatest/src/javatest/p0303/SunRise.java new file mode 100644 index 0000000..dbd964c --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0303/SunRise.java @@ -0,0 +1,12 @@ +package javatest.p0303;
 +
 +import javatest.p0303.sub.*;
 +
 +class SunRise {
 +	public static void main(String args[]) {
 +		SquareEquation equation = new SquareEquation(4, 5, 1);
 +		equation.getRoots();
 +		equation.setCoefficient(-3, 4, 5);
 +		equation.getRoots();
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0303/sub/SquareEquation.java b/works/life/java-practice/javatest/src/javatest/p0303/sub/SquareEquation.java new file mode 100644 index 0000000..0d7546c --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0303/sub/SquareEquation.java @@ -0,0 +1,46 @@ +package javatest.p0303.sub;
 +
 +public class SquareEquation {
 +	double a, b, c;
 +	double root1, root2;
 +	boolean boo;
 +
 +	public SquareEquation(double a, double b, double c) {
 +		this.a = a;
 +		this.b = b;
 +		this.c = c;
 +		if (a != 0) {
 +			boo = true;
 +		} else {
 +			boo = false;
 +		}
 +	}
 +
 +	public void getRoots() {
 +		if (boo) {
 +			System.out.println("是一元2次方程");
 +			double disk = b * b - 4 * a * c;
 +			if (disk >= 0) {
 +				root1 = (-b + Math.sqrt(disk)) / (2 * a);
 +				root2 = (-b - Math.sqrt(disk)) / (2 * a);
 +				System.out.printf("方程的根:%f,%f\n", root1, root2);
 +			} else {
 +				System.out.printf("方程没有实根\n");
 +			}
 +
 +		} else {
 +			System.out.println("不是一元2次方程");
 +		}
 +	}
 +
 +	public void setCoefficient(double a, double b, double c) {
 +		this.a = a;
 +		this.b = b;
 +		this.c = c;
 +		if (a != 0) {
 +			boo = true;
 +		} else {
 +			boo = false;
 +		}
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0401/Example.java b/works/life/java-practice/javatest/src/javatest/p0401/Example.java new file mode 100644 index 0000000..d673821 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0401/Example.java @@ -0,0 +1,113 @@ +package javatest.p0401;
 +
 +class People {
 +	protected double weight, height;
 +
 +	public void speakHello() {
 +		System.out.println("yayawawa");
 +	}
 +
 +	public void averageHeight() {
 +		height = 173;
 +		System.out.println("average height:" + height);
 +	}
 +
 +	public void averageWeight() {
 +		weight = 70;
 +		System.out.println("average weight:" + weight);
 +	}
 +}
 +
 +class ChinaPeople extends People {
 +	public void speakHello() {
 +		System.out.println("你好,吃饭了吗?");
 +	}
 +
 +	// 重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的
 +	// 汉语信息
 +	public void averageHeight() {
 +		height = 173;
 +		System.out.println("中国人的平均身高:" + height + "厘米");
 +	}
 +
 +	// 重写public void averageHeight()方法,要求输出类似
 +	// “中国人的平均身高:168.78厘米”这样的汉语信息
 +	public void averageWeight() {
 +		weight = 67.34;
 +		System.out.println("中国人的平均体重:" + weight + "公斤");
 +	}
 +
 +	// 重写public void averageWeight()方法,
 +	// 要求输出类似“中国人的平均体重:65公斤”这样的汉语信息
 +	public void chinaGongfu() {
 +		System.out.println("坐如钟,站如松,睡如弓");// 输出中国武术的信息,例如:"坐如钟,站如松,睡如弓"等
 +	}
 +}
 +
 +class AmericanPeople extends People {
 +	public void speakHello() {
 +		System.out.println("How do You do");
 +	}
 +
 +	// 重写public void speakHello()方法,要求输出类似
 +	// “How do you do”这样的英语信息。
 +	public void averageHeight() {
 +		height = 188;
 +		System.out.println("Amerian Average height:" + height + " cm");
 +	}
 +
 +	// 重写public void averageHeight()方法
 +	public void averageWeight() {
 +		weight = 80.23;
 +		System.out.println("Amerian Average weight:" + weight + " kg");
 +	}
 +
 +	// 重写public void averageWeight()方法
 +	public void americanBoxing() {
 +		System.out.println("直拳、钩拳");// 输出拳击的信息,例如,“直拳”、“钩拳”等
 +	}
 +}
 +
 +class BeijingPeople extends ChinaPeople {
 +	public void speakHello() {
 +		System.out.println("您好");
 +	}
 +
 +	// 重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息
 +	public void averageHeight() {
 +		height = 16;
 +		System.out.println("北京人的平均身高:" + height + "厘米");
 +	}
 +
 +	// 重写public void averageHeight()方法
 +	public void averageWeight() {
 +		weight = 6;
 +		System.out.println("北京人的平均体重:" + weight + "公斤");
 +	}
 +
 +	// 重写public void averageWeight()方法
 +	public void beijingOpera() {
 +		System.out.println("京剧术语");// 输出京剧的信息
 +	}
 +}
 +
 +public class Example {
 +	public static void main(String args[]) {
 +		ChinaPeople chinaPeople = new ChinaPeople();
 +		AmericanPeople americanPeople = new AmericanPeople();
 +		BeijingPeople beijingPeople = new BeijingPeople();
 +		chinaPeople.speakHello();
 +		americanPeople.speakHello();
 +		beijingPeople.speakHello();
 +		chinaPeople.averageHeight();
 +		americanPeople.averageHeight();
 +		beijingPeople.averageHeight();
 +		chinaPeople.averageWeight();
 +		americanPeople.averageWeight();
 +		beijingPeople.averageWeight();
 +		chinaPeople.chinaGongfu();
 +		americanPeople.americanBoxing();
 +		beijingPeople.beijingOpera();
 +		beijingPeople.chinaGongfu();
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0402/Subroutine.java b/works/life/java-practice/javatest/src/javatest/p0402/Subroutine.java new file mode 100644 index 0000000..6b11ace --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0402/Subroutine.java @@ -0,0 +1,23 @@ +package javatest.p0402;
 +
 +class Parent { // 父类
 +	Parent() {
 +		System.out.println("调用父类的parent()构造方法");
 +	}
 +}
 +
 +class SubParent extends Parent { // 继承Parent类
 +	SubParent() {
 +		System.out.println("调用子类的SubParent()构造方法");
 +	}
 +}
 +
 +public class Subroutine extends SubParent { // 继承SubParent类
 +	Subroutine() {
 +		System.out.println("调用子类的Subroutine()构造方法");
 +	}
 +
 +	public static void main(String[] args) {
 +		Subroutine s = new Subroutine(); // 实例化子类对象
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0403/Parallelogram.java b/works/life/java-practice/javatest/src/javatest/p0403/Parallelogram.java new file mode 100644 index 0000000..329aa80 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0403/Parallelogram.java @@ -0,0 +1,31 @@ +package javatest.p0403;
 +
 +class Quadrangle {
 +	public static void draw(Quadrangle q) {
 +		// SomeSentence
 +	}
 +}
 +
 +class Square extends Quadrangle {
 +	// SomeSentence
 +}
 +
 +class Anything {
 +	// SomeSentence
 +}
 +
 +public class Parallelogram extends Quadrangle {
 +	public static void main(String args[]) {
 +		Quadrangle q = new Quadrangle(); // 实例化父类对象
 +		// 判断父类对象是否为Parallelogram子类的一个实例
 +		if (q instanceof Parallelogram) {
 +			Parallelogram p = (Parallelogram) q; // 向下转型操作
 +		}
 +		// 判断父类对象是否为Parallelogram子类的一个实例
 +		if (q instanceof Square) {
 +			Square s = (Square) q; // 进行向下转型操作
 +		}
 +		// 由于q对象不为Anything类的对象,所以这条语句是错误的
 +		// System.out.println(q instanceof Anything);
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0404/Quadrangle.java b/works/life/java-practice/javatest/src/javatest/p0404/Quadrangle.java new file mode 100644 index 0000000..c6b6630 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0404/Quadrangle.java @@ -0,0 +1,36 @@ +package javatest.p0404;
 +
 +public class Quadrangle {
 +	// 实例化保存四边形对象的数组对象
 +	private Quadrangle[] qtest = new Quadrangle[6];
 +	private int nextIndex = 0;
 +	
 +	public void draw(Quadrangle q) { // 定义draw()方法,参数为四边形对象
 +		if (nextIndex < qtest.length) {
 +			qtest[nextIndex] = q;
 +			System.out.println(nextIndex);
 +			nextIndex++;
 +		}
 +	}
 +	
 +	public static void main(String[] args) {
 +		// 实例化两个四边形对象,用于调用draw()方法
 +		Quadrangle q = new Quadrangle();
 +		q.draw(new Square()); // 以正方形对象为参数调用draw()方法
 +		// 以平行四边形对象为参数调用draw()方法
 +		q.draw(new Parallelogramgle());
 +	}
 +}
 +
 +class Square extends Quadrangle { // 定义一个正方形类,继承四边形类
 +	public Square() {
 +		System.out.println("正方形");
 +	}
 +}
 +
 +// 定义一个平行四边形类,继承四边形类
 +class Parallelogramgle extends Quadrangle {
 +	public Parallelogramgle() {
 +		System.out.println("平行四边形");
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0501/HardWork.java b/works/life/java-practice/javatest/src/javatest/p0501/HardWork.java new file mode 100644 index 0000000..b4121af --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0501/HardWork.java @@ -0,0 +1,60 @@ +package javatest.p0501;
 +
 +abstract class Employee {
 +	public abstract double earnings();
 +}
 +
 +class YearWorker extends Employee {
 +	public double earnings() {
 +		return 50000.456;
 +	}
 +	// 重写earnings()方法
 +}
 +
 +class MonthWorker extends Employee {
 +	public double earnings() {
 +		return 12 * 2300;
 +	}
 +	// 重写earnings()方法。
 +}
 +
 +class WeekWorker extends Employee {
 +	public double earnings() {
 +		return 52 * 500;
 +	}
 +	// 重写earnings()方法。
 +}
 +
 +class Company {
 +	Employee[] employee;
 +	double salaries = 0;
 +
 +	Company(Employee[] employee) {
 +		this.employee = employee;
 +	}
 +
 +	public double salariesPay() {
 +		salaries = 0;
 +		for (int i = 0; i < employee.length; i++) {
 +			salaries = salaries + employee[i].earnings();
 +		}
 +		// 计算salaries。
 +		return salaries;
 +	}
 +}
 +
 +public class HardWork {
 +	public static void main(String args[]) {
 +		Employee[] employee = new Employee[20];
 +		for (int i = 0; i < employee.length; i++) {
 +			if (i % 3 == 0)
 +				employee[i] = new WeekWorker();
 +			else if (i % 3 == 1)
 +				employee[i] = new MonthWorker();
 +			else if (i % 3 == 2)
 +				employee[i] = new YearWorker();
 +		}
 +		Company company = new Company(employee);
 +		System.out.println("公司年工资总额:" + company.salariesPay());
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0502/Road.java b/works/life/java-practice/javatest/src/javatest/p0502/Road.java new file mode 100644 index 0000000..7731494 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0502/Road.java @@ -0,0 +1,70 @@ +package javatest.p0502;
 +
 +interface ComputerWeight {
 +	public double computeWeight();
 +}
 +
 +class Television implements ComputerWeight {
 +	public double computeWeight() {
 +		return 45.5;
 +	}
 +	// 实现computeWeight()方法。
 +}
 +
 +class Computer implements ComputerWeight {
 +	public double computeWeight() {
 +		return 65.5;
 +	}
 +	// 实现computeWeight()方法。
 +}
 +
 +class WashMachine implements ComputerWeight {
 +	public double computeWeight() {
 +		return 145;
 +	}
 +	// 实现computeWeight()方法。
 +}
 +
 +class Car {
 +	ComputerWeight[] goods;
 +	double totalWeights = 0;
 +
 +	Car(ComputerWeight[] goods) {
 +		this.goods = goods;
 +	}
 +
 +	public double getTotalWeights() {
 +		totalWeights = 0;
 +		for (int k = 0; k < goods.length; k++) {
 +			totalWeights = totalWeights + goods[k].computeWeight();
 +		}
 +		// 计算totalWeights
 +		return totalWeights;
 +	}
 +}
 +
 +public class Road {
 +	public static void main(String args[]) {
 +		ComputerWeight[] goodsOne = new ComputerWeight[50], goodsTwo = new ComputerWeight[22];
 +		for (int i = 0; i < goodsOne.length; i++) {
 +			if (i % 3 == 0)
 +				goodsOne[i] = new Television();
 +			else if (i % 3 == 1)
 +				goodsOne[i] = new Computer();
 +			else if (i % 3 == 2)
 +				goodsOne[i] = new WashMachine();
 +		}
 +		for (int i = 0; i < goodsTwo.length; i++) {
 +			if (i % 3 == 0)
 +				goodsTwo[i] = new Television();
 +			else if (i % 3 == 1)
 +				goodsTwo[i] = new Computer();
 +			else if (i % 3 == 2)
 +				goodsTwo[i] = new WashMachine();
 +		}
 +		Car 大货车 = new Car(goodsOne);
 +		System.out.println("大货车装载的货物重量:" + 大货车.getTotalWeights());
 +		Car 小货车 = new Car(goodsTwo);
 +		System.out.println("小货车装载的货物重量:" + 小货车.getTotalWeights());
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0503/QuadrangleUseInterface.java b/works/life/java-practice/javatest/src/javatest/p0503/QuadrangleUseInterface.java new file mode 100644 index 0000000..6cead68 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0503/QuadrangleUseInterface.java @@ -0,0 +1,46 @@ +package javatest.p0503;
 +
 +interface drawTest { // 定义接口
 +	public void draw(); // 定义方法
 +}
 +
 +// 定义平行四边形类,该类继承了四边形类,并实现了drawTest接口
 +class ParallelogramgleUseInterface extends QuadrangleUseInterface implements drawTest {
 +	public void draw() { // 由于该类实现了接口,所以需要覆盖draw()方法
 +		System.out.println("平行四边形.draw()");
 +	}
 +
 +	void doAnyThing() { // 覆盖父类方法
 +		// SomeSentence
 +	}
 +}
 +
 +class SquareUseInterface extends QuadrangleUseInterface implements drawTest {
 +	public void draw() {
 +		System.out.println("正方形.draw()");
 +	}
 +
 +	void doAnyThing() {
 +		// SomeSentence
 +	}
 +}
 +
 +class AnyThingUseInterface extends QuadrangleUseInterface {
 +	void doAnyThing() {
 +
 +	}
 +}
 +
 +public class QuadrangleUseInterface { // 定义四边形类
 +	public void doAnyTthing() {
 +		// SomeSentence
 +	}
 +
 +	public static void main(String[] args) {
 +		drawTest[] d = { // 接口也可以进行向上转型操作
 +				new SquareUseInterface(), new ParallelogramgleUseInterface() };
 +		for (int i = 0; i < d.length; i++) {
 +			d[i].draw(); // 调用draw()方法
 +		}
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0701/StringExample.java b/works/life/java-practice/javatest/src/javatest/p0701/StringExample.java new file mode 100644 index 0000000..be2c7b9 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0701/StringExample.java @@ -0,0 +1,45 @@ +package javatest.p0701;
 +
 +public class StringExample {
 +	public static void main(String args[]) {
 +		String s1 = new String("you are a student"), s2 = new String("how are you");
 +		if (s1.equals(s2)) // 使用equals方法判断s1与s2是否相同
 +		{
 +			System.out.println("s1与s2相同");
 +		} else {
 +			System.out.println("s1与s2不相同");
 +		}
 +		String s3 = new String("22030219851022024");
 +		if (s3.startsWith("220302")) // 判断s3的前缀是否是“220302”。
 +		{
 +			System.out.println("吉林省的身份证");
 +		}
 +		String s4 = new String("你"), s5 = new String("我");
 +		if (s4.compareTo(s5) > 0)// 按着字典序s4大于s5的表达式。
 +		{
 +			System.out.println("按字典序s4大于s5");
 +		} else {
 +			System.out.println("按字典序s4小于s5");
 +		}
 +		int position = 0;
 +		String path = "c:\\java\\jsp\\A.java";
 +		position = path.lastIndexOf('\\'); // 获取path中最后出现目录分隔符号的位置
 +		System.out.println("c:\\java\\jsp\\A.java中最后出现\\的位置:" + position);
 +		String fileName = path.substring(12, 18); // 获取path中“A.java”子字符串。
 +		System.out.println("c:\\java\\jsp\\A.java中含有的文件名:" + fileName);
 +		String s6 = new String("100"), s7 = new String("123.678");
 +		int n1 = Integer.parseInt(s6); // 将s6转化成int型数据。
 +		double n2 = Double.parseDouble(s7); // 将s7转化成double型数据。
 +		double m = n1 + n2;
 +		System.out.println(m);
 +		String s8 = String.valueOf(m); // String调用valuOf(int n)方法将m转化为字符串对象
 +		position = s8.indexOf(".");
 +		String temp = s8.substring(position + 1);
 +		System.out.println("数字" + m + "有" + temp.length() + "位小数");
 +		String s9 = new String("ABCDEF");
 +		char a[] = s8.toCharArray(); // 将s8存放到数组a中。
 +		for (int i = a.length - 1; i >= 0; i--) {
 +			System.out.print(" " + a[i]);
 +		}
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0702/DateExample.java b/works/life/java-practice/javatest/src/javatest/p0702/DateExample.java new file mode 100644 index 0000000..74e4901 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0702/DateExample.java @@ -0,0 +1,40 @@ +package javatest.p0702;
 +
 +import java.util.*;
 +import java.time.*;
 +
 +import javax.swing.JOptionPane;
 +
 +public class DateExample {
 +	public static void main(String args[]) {
 +		String str = JOptionPane.showInputDialog("输入第一个日期的年份:");
 +		int yearOne = Integer.parseInt(str);
 +		str = JOptionPane.showInputDialog("输入该年的月份:");
 +		int monthOne = Integer.parseInt(str);
 +		str = JOptionPane.showInputDialog("输入该月份的日期:");
 +		int dayOne = Integer.parseInt(str);
 +		str = JOptionPane.showInputDialog("输入第二个日期的年份:");
 +		int yearTwo = Integer.parseInt(str);
 +		str = JOptionPane.showInputDialog("输入该年的月份:");
 +		int monthTwo = Integer.parseInt(str);
 +		str = JOptionPane.showInputDialog("输入该月份的日期:");
 +		int dayTwo = Integer.parseInt(str);
 +		Calendar calendar = Calendar.getInstance(); // 初始化日历对象
 +		calendar.set(yearOne, monthOne, dayOne); // 将calendar的时间设置为yearOne年monthOne月dayOne日
 +		long timeOne = calendar.getTimeInMillis(); // calendar表示的时间转换成毫秒
 +		calendar.set(yearTwo, monthTwo, dayTwo); // 将calendar的时间设置为yearTwo年monthTwo月dayTwo日
 +		long timeTwo = calendar.getTimeInMillis(); // calendar表示的时间转换成毫秒。
 +		Date date1 = new Date(timeOne); // 用timeOne做参数构造date1
 +		Date date2 = new Date(timeTwo); // 用timeTwo做参数构造date2
 +		if (date2.equals(date1)) {
 +			System.out.println("两个日期的年、月、日完全相同");
 +		} else if (date2.after(date1)) {
 +			System.out.println("您输入的第二个日期大于第一个日期");
 +		} else if (date2.before(date1)) {
 +			System.out.println("您输入的第二个日期小于第一个日期");
 +		}
 +		long days = Duration.between(date1.toInstant(), date2.toInstant()).toDays();// 计算两个日期相隔天数
 +		System.out.println(yearOne + "年" + monthOne + "月" + dayOne + "日和" + yearTwo + "年" + monthTwo + "月" + dayTwo
 +				+ "相隔" + days + "天");
 +	}
 +}
 diff --git a/works/life/java-practice/javatest/src/javatest/p0703/BigIntegerExample.java b/works/life/java-practice/javatest/src/javatest/p0703/BigIntegerExample.java new file mode 100644 index 0000000..05b2e38 --- /dev/null +++ b/works/life/java-practice/javatest/src/javatest/p0703/BigIntegerExample.java @@ -0,0 +1,29 @@ +package javatest.p0703;
 +
 +import java.math.*;
 +
 +class BigIntegerExample {
 +	public static void main(String args[]) {
 +		BigInteger n1 = new BigInteger("987654321987654321987654321"),
 +				n2 = new BigInteger("123456789123456789123456789"), result = null;
 +		result = n1.add(n2); // n1和n2做加法运算
 +		System.out.println("和:" + result.toString());
 +		result = n1.subtract(n2);// n1和n2做减法运算
 +		System.out.println("差:" + result.toString());
 +		result = n1.multiply(n2);// n1和n2做乘法运算
 +		System.out.println("积:" + result.toString());
 +		result = n1.divide(n2);// n1和n2做除法运算
 +		System.out.println("商:" + result.toString());
 +		BigInteger m = new BigInteger("1968957"), COUNT = new BigInteger("0"), ONE = new BigInteger("1"),
 +				TWO = new BigInteger("2");
 +		System.out.println(m.toString() + "的因子有:");
 +		for (BigInteger i = TWO; i.compareTo(m) < 0; i = i.add(ONE)) {
 +			if ((n1.remainder(i).compareTo(BigInteger.ZERO)) == 0) {
 +				COUNT = COUNT.add(ONE);
 +				System.out.print("  " + i.toString());
 +			}
 +		}
 +		System.out.println("");
 +		System.out.println(m.toString() + "一共有" + COUNT.toString() + "个因子");
 +	}
 +}
  | 
