第十三章第十七題(數學:Complex類)(Math:Complex class)

jxxxh發表於2020-11-07

第十三章第十七題(數學: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

在這裡插入圖片描述

相關文章