Java基础版第十三章课后题关于Cloneable和Comparable接口问题

题目:在完成以下题目后还要实现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;
		}
	}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值