题目:在完成以下题目后还要实现compareable接口来比较两组复数的接口
注意:Cloneable与Comparable两个接口Java已经定义好了,不需要我们重新定义。
package stackofintegers;
import java.util.Scanner;
public class new530 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入第一组复数的实部和虚部:");
double a = input.nextDouble();
double b = input.nextDouble();
Complex num1 = new Complex(a,b);
Complex b1 = new Complex(b);
System.out.println("请输入第二组复数的实部和虚部:");
double c = input.nextDouble();
double d = input.nextDouble();
Complex num2 = new Complex(c,d);
Complex b2 = new Complex(d);
System.out.println("(" + num1 + ")" + " + " + "(" + num2 + ")" + " = " + num1.add(num2));
System.out.println("(" + num1 + ")" + " - " + "(" + num2 + ")" + " = " + num1.subtract(num2));
System.out.println("(" + num1 + ")" + " * " + "(" + num2 + ")" + " = " + num1.multiply(num2));
System.out.println("(" + num1 + ")" + " / " + "(" + num2 + ")" + " = " + num1.divide(num2));
System.out.println("|" + num1 + "| =" + num1.abs());
System.out.println("第一组复数的实部是:" + num1.getRealpart());
System.out.println("第一组复数的虚部是:" + num1.getImaginaryPart());
System.out.println("第二组复数的实部是:" + num2.getRealpart());
System.out.println("第二组复数的虚部是:" + num2.getImaginaryPart());
System.out.println("****1:第一组复数的虚部较大***");
System.out.println("****0:两组复数的虚部相等*****");
System.out.println("***-1:第二组复数的虚部较大***");
System.out.println("两组复数的虚部大小比较为:" + b1.compareTo(b2));
input.close();
}
}
@SuppressWarnings("rawtypes")
class Complex implements Cloneable,Comparable{
private double a = 0, b = 0;
public Complex() {
}
Complex(double a,double b){
this.a = a;
this.b = b;
}
public Complex(double a) {
this.a = a;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public Complex add(Complex secondComplex) {
//加法
double newA = a + secondComplex.getA();
double newB = b + secondComplex.getB();
return new Complex(newA,newB);
}
public Complex subtract(Complex secondComplex) {
//减法
double newA = a - secondComplex.getA();
double newB = b - secondComplex.getB();
return new Complex(newA,newB);
}
public Complex multiply(Complex secondComplex) {
//乘法
double newA = a * secondComplex.getA() - b * secondComplex.getB();
double newB = b * secondComplex.getA() - a * secondComplex.getB();
return new Complex(newA,newB);
}
public Complex divide(Complex secondComplex) {
//乘法
double newA = (a * secondComplex.getA() + b*secondComplex.getB()) / (Math.pow(secondComplex.getA(), 2.0) + Math.pow(secondComplex.getB(), 2.0));
double newB = (b * secondComplex.getA() + a*secondComplex.getB()) / (Math.pow(secondComplex.getA(), 2.0) + Math.pow(secondComplex.getB(), 2.0));
return new Complex(newA,newB);
}
public double abs() {
//求模
return Math.sqrt(a * a + b * b);
}
public int compareTo(Object obj) {
//比较两个虚部的大小
Complex com = (Complex)obj;
if(this.a > com.a)
return 1;
else if(this.a == com.a)
return 0;
else
return -1;
}
public String toString() {
if(b != 0)
return a + "+" + b + "i";
return a + " ";
}
public double getRealpart() {
return a;//返回实部
}
public double getImaginaryPart() {
return b;//返回虚部
}
public Object clone() {
try {
return super.clone();
}
catch(CloneNotSupportedException ex) {
return null;
}
}
}