第十三章第十七题(数学:Complex类)(Math:Complex class)

第十三章第十七题(数学:Complex类)(Math:Complex class)

  • *13.17(数学:Complex类)设计一个名为Complex的类来表示复数以及完成复数运算的add、substract、,ultiply、divide和abs方法,并且重写toString方法以返回一个表示复数的字符串。方法toString返回字符串a+bi。如果b是0,那么他返回a。Complex类还需要实现Cloneable和Comparable。使用他们的绝对值来比较两个复数。
    提供三个构造方法Complex(a,b)、Complex(a)和Complex()。COmplex()为数字0创建Complex对象,而Complex(a)创建一个b为0的Complex对象。还提供getRealPart()和getImageinaryPart()方法以分别返回复数的实部和虚部。
    绘制UML类图并实现该类。编写一个测试程序,提示用户输入两个复数,然后显示他们做加、减、乘、除之后的结果。下面是一个运行示例:
    Enter the first complex number: 3.5 5.5
    Enter the second complex number: -3.5 1
    (3.5 + 5.5 i ) + (-3.5 + 1.0 i ) = 0.0 + 6.5 i
    (3.5 + 5.5 i ) - (-3.5 + 1.0 i ) = 7.0 + 4.5 i
    (3.5 + 5.5 i ) * (-3.5 + 1.0 i ) = -17.75 + -15.75 i
    (3.5 + 5.5 i ) / (-3.5 + 1.0 i ) = -0.5094339622641509 + -1.7169811320754718 i
    *13.17(Math:Complex class)A class named complex is designed to represent complex number and add, substract, multiply, divide and ABS methods to complete complex operation, and override toString method to return a string representing complex number. Method toString returns the string a + bi. If B is 0, then he returns a. The complex class also needs to implement clonable and comparable. Use their absolute values to compare two complex numbers.
    Three construction methods, complex (a, b), complex (a) and complex(), are provided. Complex() creates a complex object for the number 0, while complex (a) creates a complex object with B = 0. Getrealpart() and getimagenarypart() methods are also provided to return the real and imaginary parts of a complex number, respectively.
    Draw UML class diagram and implement the class. Write a test program, prompt the user to enter two complex numbers, and then display their results after adding, subtracting, multiplying and dividing. Here is a running example:
    Enter the first complex number: 3.5 5.5
    Enter the second complex number: -3.5 1
    (3.5 + 5.5 i ) + (-3.5 + 1.0 i ) = 0.0 + 6.5 i
    (3.5 + 5.5 i ) - (-3.5 + 1.0 i ) = 7.0 + 4.5 i
    (3.5 + 5.5 i ) * (-3.5 + 1.0 i ) = -17.75 + -15.75 i
    (3.5 + 5.5 i ) / (-3.5 + 1.0 i ) = -0.5094339622641509 + -1.7169811320754718 i
  • 参考代码:
package chapter13;

import java.util.Scanner;

public class Code_17 {
    public static void main(String[] args) {
        Complex number1 = new Complex();
        Complex number2 = new Complex();
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first complex number: ");
        number1.aDouble = input.nextDouble();
        number1.bDouble = input.nextDouble();
        System.out.print("Enter the second complex number: ");
        number2.aDouble = input.nextDouble();
        number2.bDouble = input.nextDouble();
        Complex number3 = number1.add(number2);
        System.out.println("(" + number1.toString() + ")" + " + " + "(" + number2.toString() + ")" + " = " + number3.toString());
        number3 = number1.substract(number2);
        System.out.println("(" + number1.toString() + ")" + " - " + "(" + number2.toString() + ")" + " = " + number3.toString());
        number3 = number1.multiply(number2);
        System.out.println("(" + number1.toString() + ")" + " * " + "(" + number2.toString() + ")" + " = " + number3.toString());
        number3 = number1.divide(number2);
        System.out.println("(" + number1.toString() + ")" + " / " + "(" + number2.toString() + ")" + " = " + number3.toString());
    }
}
class Complex{
    double aDouble,bDouble;
    Complex(double a,double b){
        aDouble = a;
        bDouble = b;
    }
    Complex(double a){
        aDouble = a;
    }
    Complex(){

    }
    double getRealPart(){
        return aDouble;
    }
    double getImaginaryPart(){
        return bDouble;
    }
    public Complex add(Complex two){
        Complex three = new Complex();
        three.aDouble = two.aDouble + this.aDouble;
        three.bDouble = two.bDouble + this.bDouble;
        return three;
    }
    public Complex substract(Complex two){
        Complex three = new Complex();
        three.aDouble = this.aDouble - two.aDouble;
        three.bDouble = this.bDouble - two.bDouble;
        return three;
    }
    public Complex multiply(Complex two){
        Complex three = new Complex();
        three.aDouble = this.aDouble * two.aDouble - two.bDouble * this.bDouble;
        three.bDouble = this.bDouble * two.aDouble + this.aDouble * two.bDouble;
        return three;
    }
    public Complex divide(Complex two){
        Complex three = new Complex();
        double sum = two.aDouble * two.aDouble + two.bDouble * two.bDouble;
        three.aDouble = (this.aDouble * two.aDouble + two.bDouble * this.bDouble) / sum;
        three.bDouble = (this.bDouble * two.aDouble - this.aDouble * two.bDouble) / sum;
        return three;
    }
    public double abs(){
        return Math.sqrt(this.aDouble * this.aDouble + this.bDouble * this.bDouble);
    }
    @Override
    public String toString(){
        if (this.bDouble == 0)
            return this.aDouble + "";
        return this.aDouble + " + " + this.bDouble + " i ";
    }
}

  • 结果显示:
Enter the first complex number: 3.5 5.5
Enter the second complex number: -3.5 1
(3.5 + 5.5 i ) + (-3.5 + 1.0 i ) = 0.0 + 6.5 i 
(3.5 + 5.5 i ) - (-3.5 + 1.0 i ) = 7.0 + 4.5 i 
(3.5 + 5.5 i ) * (-3.5 + 1.0 i ) = -17.75 + -15.75 i 
(3.5 + 5.5 i ) / (-3.5 + 1.0 i ) = -0.5094339622641509 + -1.7169811320754718 i 

Process finished with exit code 0

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值