1、将课本上的复数类补充抽象完善,类名为公有的Complex,抽象私有双精度类型的实部和虚部,提供构造方法和get方法和set方法,重写equals方法、hashcode方法、toString方法,提供类方法用于加减乘除计算,当然要有输入和输出方法。最后还要提供一个类方法getDesigner,返回字符串:“设计者:[姓名] 学号:[学号] 班级:[班级]”。
import java.util.Scanner;
public class Complex {
private double realpart;private double imaginarypart;
public double getRealpart() {
return realpart;
}
public void setRealpart(double realpart) {
this.realpart = realpart;
}
public double getImaginarypart() {
return imaginarypart;
}
public void setImaginarypart(double imaginarypart) {
this.imaginarypart = imaginarypart;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(imaginarypart);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(realpart);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Complex other = (Complex) obj;
if (Double.doubleToLongBits(imaginarypart) != Double.doubleToLongBits(other.imaginarypart))
return false;
if (Double.doubleToLongBits(realpart) != Double.doubleToLongBits(other.realpart))
return false;
return true;
}
Complex(){realpart = 0;imaginarypart = 0;}
Complex(double s,double x) {realpart = s;imaginarypart = x;}
public boolean equals(Complex another) {
return(this.realpart== another.realpart
&&this.imaginarypart == another.imaginarypart);
}
public String toString() {
String str =""+realpart;
if(imaginarypart<0.0) str = str + imaginarypart +"i";
else str = str + "+"+imaginarypart +"i";
return str;
}
public void input() {
Scanner keyin = new Scanner(System.in);
System.out.println("real:");
realpart = keyin.nextDouble();
System.out.println("imaginary:");
imaginarypart = keyin.nextDouble();
}
public void display() {
System.out.println(toString());
}
public static Complex add(Complex m1, Complex m2) {
double realpart=m1.getRealpart()+m2.getRealpart();
double imaginarypart=m1.getImaginarypart()+m2.getImaginarypart();
return new Complex(realpart,imaginarypart);
}
public static Complex sub(Complex m1, Complex m2)
{
double realpart=m1.getRealpart()-m2.getRealpart();
double imaginarypart=m1.getImaginarypart()-m2.getImaginarypart();
return new Complex(realpart,imaginarypart);
}
public static Complex mul(Complex m1,Complex m2)
{
double realpart=m1.getRealpart()*m2.getRealpart()-m1.getImaginarypart()*m2.getImaginarypart();
double imaginarypart=m1.getRealpart()*m2.getImaginarypart()+m2.getRealpart()*m1.getImaginarypart();
return new Complex(realpart,imaginarypart);
}
public static Complex div(Complex m1,Complex m2)
{
double realpart=(m1.getRealpart()*m2.getRealpart()+m1.getRealpart()*m2.getImaginarypart())/(m2.getRealpart()*m2.getRealpart()+m2.getImaginarypart()*m2.getImaginarypart());
double imaginarypart=(m2.getRealpart()*m1.getImaginarypart()-m1.getRealpart()*m2.getImaginarypart())/(m2.getRealpart()*m2.getRealpart()+m2.getImaginarypart()*m2.getImaginarypart());
return new Complex(realpart,imaginarypart);
}
public static String getDesigner() {
String content ="设计者:[] 学号:[] 班级:[]";
return content;
}
public static void main (String args[]) {
System.out.println(Complex.getDesigner());
Complex m1 = new Complex();
Complex m2 = new Complex();
System.out.println("Please input the first complex's real part and imaginary part :");
m1.input();
System.out.print("The first Complex is :");
m1.display();
System.out.println("Please input the second complex's real part and imaginary part :");
m2.input();
System.out.print("The second Complex is :");
m2.display();
System.out.println("m1==m2="+(m1==m2));
System.out.println("m1.equals(m2)="+m1.equals(m2));
Complex m3 = Complex.add(m1,m2);
Complex m4 = Complex.sub(m1,m2);
Complex m5 =Complex.mul(m1,m2);
Complex m6 =Complex.div(m1,m2);
System.out.println("m1+m2="+m3);
System.out.println("m2-m3="+m4);
System.out.println("m1*m2="+m5);
System.out.println("m1/m2="+m6);
}
}
2、抽象四维空间(三维空间坐标+一维时间坐标)的质点,要求用实数表示坐标值,还应该有质点的质量等属性。要求对所有属性的抽象都是封装的,并提供相应的get方法和set方法。要求抽象的方法中有toString方法、equals方法、相应的构造方法和hashcode方法。另外还要求提供质点的移动方法,提供两个质点之间距离计算方法,提供两个质点之间的引力计算方法等。并设计main方法来完成各方法的测试,最后还要提供一个类方法getDesigner,返回字符串:“设计者:[姓名] 学号:[学号] 班级:[班级]”。
import java.util.Scanner;
class Point
{private double x,y,z,t,m;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(m);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(t);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(x);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(y);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(z);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (Double.doubleToLongBits(m) != Double.doubleToLongBits(other.m))
return false;
if (Double.doubleToLongBits(t) != Double.doubleToLongBits(other.t))
return false;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
return false;
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
return false;
if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
return false;
return true;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public double getT() {
return t;
}
public void setT(double t) {
this.t = t;
}
public double getM() {
return m;
}
public void setM(double m) {
this.m = m;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + ", z=" + z + ", t=" + t + ", m=" + m + "]";
}
Point() {x = 0;y=0;z=0;t=0;m=0;}
Point( double a, double b, double c, double d, double e) {x = a;y=b;z=c;t=d;m=e;}
public void input() {
Scanner keyin = new Scanner(System.in);
System.out.println("x=:");
x = keyin.nextDouble();
System.out.println("y=:");
y = keyin.nextDouble();
System.out.println("z=:");
z = keyin.nextDouble();
System.out.println("m=:");
m = keyin.nextDouble();
System.out.println("t=:");
t = keyin.nextDouble();
}
public void display() {
System.out.println(toString());
}
public static void Move(Point p1,Point p2)
{double x=p1.getX() -p2.getX(),
y=p1.getY() -p2.getY(),
z=p1.getZ() -p2.getZ(),
t=p1.getT()-p2.getT(),
Dist = Math.sqrt(x*x+y*y+z*z),
Speed = Dist/t;
System.out.println("p1--->p2's movespeed and coordinate vectorsis :"+Dist/t+"("+x+","+y+","+z+","+z+")");
}
public static void Dist(Point p1,Point p2)
{
double x=p1.getX() -p2.getX(),
y=p1.getY() -p2.getY(),
z=p1.getZ() -p2.getZ(),
Dist = Math.sqrt(x*x+y*y+z*z);
System.out.println("p1--->p2's distance is :"+Dist);
}
public static void Gravi(Point p1,Point p2)
{
double Gra = p1.getM()*p2.getM()/ (p1.getX() -p2.getX()+p1.getY() -p2.getY()+p1.getZ() -p2.getZ());
System.out.println("p1--->p2's gravitation is :"+Gra);
}
public static String getDesigner() {
String content ="设计者:[] 学号:[] 班级:[]";
return content;
}
public static void main(String[] args)
{
System.out.println(Point.getDesigner());
Point p1 = new Point();
Point p2 = new Point();
System.out.println("Please input the first partical's property :");
p1.input();
System.out.print("The first partical is :");
p1.display();
System.out.println("Please input the second partical's property :");
p2.input();
System.out.print("The second partical is :");
p2.display();
System.out.println("p1==p2="+(p1==p2));
System.out.println("p1.equals(p2)="+p1.equals(p2));
Point.Move(p1,p2);
Point.Dist(p1,p2);
Point.Gravi(p1,p2);
}
}