Define a class for complex numbers. A complex number is a number of the form
a + b*i
where for our purposes, a and b are numbers of type double, and i is a number
that represents the square root of -1. Represent a complex number as two values
of type double. Name the instance variables real and imaginary. (The instance
variable for the number that is multiplied by i is the one called imaginary.)
Call the class Complex. Include a constructor with two parameters of type double
that can be used to set the instance variables of an object to any values. Also,
include a constructor that has only a single parameter of type double; call this
parameter realPart and define the constructor so that the object will be initialized
to realPart + 0*i. Also, include a noargument constructor that initializes an
object to 0 (that is, to 0 + 0*i). Define accessor and mutator methods as well as
the methods equals and toString. Define methods for addition, subtraction, and
multiplication of objects of your class Complex. These methods will be static and
will each have two parameters of type Complex and returns a value of type Complex.
For example, Complex.add(c1, c2) will returns the result of adding the two complex
numbers (two objects of the class Complex) c1 and c2. Also write a test program to
test your class
Hints:
To add or subtract two complex numbers, you add or subtract the two instance
variables of type double. The product of two complex numbers is given by the following formula:
(a + b*i)*(c + d*i) = (a*c - b*d) + (a*d + b*c)*i
附上學長精簡的翻譯 * 題目 : Complex (複數) * Page 368 #7 * 製作一個類別Complex 用以表示複數與進行複數運算。 * * 要求 * 1.提供兩個instance variable,real、imaginary,型態為double。 * 2.提供含有兩個參數的建構子,型態double,用來設定兩個變數。 * 3.提供只有一個參數的建構子,參數名為realPart,用來初始化實部(real),並將虛部(imaginary)設為零 * 4.提供無參數建構子,將real、imaginary設為零 * 5.定義accessor,提供instance variable的讀取。作者定義了getReal()與getImaginary()用以分別取得real與imaginary的值。 * 6.定義mutator,提供instance variable的寫入。作者定義了setComplex(double,double)、setReal(double otherReal)與setImaginary(double otherImaginary)用以改變兩個變數的值。 * 7.定義equals,檢查兩個屬於Complex型態的物件,它們的內容是否相等。 * 8.定義toString(),將資料內容以 a + b*i 的形式印出。 * 9.提供addition,加法運算。 * 10.提供subtraction,減法運算。 * 11.提供multiplication,乘法運算。 * 9-11皆為static method 並有兩個Complex型態的參數。需傳回結果,型態為Complex。以add(c1,c2)的形式被呼叫。 * * 12.提供addition、subtraction、multiplication的第二種版本,以c1.add(c2)的形式被呼叫。 * 13.撰寫測試程式用以測試Complex類別。測試程式在main中。
/** * * @author Shiang-Yun Yang */public class Complex { private double real, imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public Complex(double real) { this.real = real; } public Complex() { this.real = 0; this.imaginary = 0; } public double getReal() { return real; } public double getImaginary() { return imaginary; } public void setReal(double real) { this.real = real; } public void setImaginary(double imaginary) { this.imaginary = imaginary; } public void setComplex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public boolean equals(Complex otherComplex) { if(otherComplex == null) return false; else return (otherComplex.getReal() == real && otherComplex.getImaginary() == imaginary); } public String toString() { if(imaginary >= 0) return real + " + " + imaginary + "*i"; else return real + " - " + Math.abs(imaginary) + "*i"; } public static Complex add(Complex A, Complex B) { return new Complex(A.real+B.real, A.imaginary+B.imaginary); } public static Complex subtract(Complex A, Complex B) { return new Complex(A.real-B.real, A.imaginary-B.imaginary); } public static Complex multiply(Complex A, Complex B) { return new Complex(A.real*B.real - A.imaginary*B.imaginary, A.real*B.imaginary + A.imaginary*B.real); } public Complex add(Complex B) { return new Complex(real+B.real, imaginary+B.imaginary); } public Complex subtract(Complex B) { return new Complex(real-B.real, imaginary-B.imaginary); } public Complex multiply(Complex B) { return new Complex(real*B.real - imaginary*B.imaginary, real*B.imaginary + imaginary*B.real); } public static void main(String[] args) { //-------------------------------------------------------------------// System.out.println("1. Test toString()"); System.out.println(" System.out.println(new Complex(5.7, 6.9))"); System.out.println(" Result : " + new Complex(5.7, 6.9)); Complex C1 = new Complex(1.25, 4.5); System.out.println(" C1.toString()"); System.out.println(" Result C1: " + C1.toString()); //-------------------------------------------------------------------// System.out.println("2. Test Complex(), Complex(double), Complex(double, double)"); Complex C2 = new Complex(); Complex C3 = new Complex(2.718); Complex C4 = new Complex(3.0, 4.125); System.out.println(" Result C2: " + C2); System.out.println(" Result C3: " + C3); System.out.println(" Result C4: " + C4); //-------------------------------------------------------------------// System.out.println("3. Test Accessor"); System.out.println(" C3.getReal() : Result : " + C3.getReal()); System.out.println(" C3.getImaginary() : Result : " + C3.getImaginary()); //-------------------------------------------------------------------// System.out.println("4. Test Mutator"); C2.setReal(0.98); System.out.println(" C2.setReal(0.98) : Result : " + C2); C2.setImaginary(8.91); System.out.println(" C2.setImaginary(8.91) : Reslut : " + C2); C2.setComplex(C4.real, C4.imaginary); System.out.println(" C2.setComplex(C4,real, C4.imaginary) : " + C2); //-------------------------------------------------------------------// System.out.println("5. Test equals()"); System.out.println(" C1 : " + C1 + " C2 : " + C2 + " C3 : " + C3); System.out.println(" C1.equals(C2) = " + C1.equals(C2)); System.out.println(" C2.equals(C3) = " + C2.equals(C3)); Complex C5 = null; System.out.println(" C1.equals(C5) = " + C1.equals(C5)); //-------------------------------------------------------------------// System.out.println("6. Test add()"); System.out.println(" Ver1 Complex.add(C1, C2): C1 + C2 = (" + C1 + ") + (" + C2 +")" + " = " + Complex.add(C1, C2)); System.out.println(" Ver2 C1.add(C2): C1 + C2 = " + C1.add(C2)); //-------------------------------------------------------------------// System.out.println("7. Test subtract()"); System.out.println(" Ver1 Complex.subtract(C1, C2): C1 - C2 = (" + C1 + ") - (" + C2 +")" + " = " + Complex.subtract(C1, C2)); System.out.println(" Ver2 C1.subtract(C2): C1 - C2 = " + C1.subtract(C2)); //-------------------------------------------------------------------// System.out.println("8. Test subtract()"); System.out.println(" Ver1 Complex.multiply(C1, C2): C1 * C2 = (" + C1 + ") * (" + C2 +")" + " = " + Complex.multiply(C1, C2)); System.out.println(" Ver2 C1.multiply(C2): C1 * C2 = " + C1.multiply(C2)); }}
文章定位: