|
| 1 | +package ch_13.exercise13_17; |
| 2 | + |
| 3 | +/** |
| 4 | + * 13.17 (Math: The Complex class) A complex number is a number in the form a + bi, |
| 5 | + * -where a and b are real numbers and i is SQRT(-1). The numbers a and b are known |
| 6 | + * as the real part and imaginary part of the complex number, respectively. You can |
| 7 | + * perform addition, subtraction, multiplication, and division for complex numbers |
| 8 | + * using the following formulas: |
| 9 | + * a + bi + c + di = (a + c) + (b + d)i |
| 10 | + * a + bi - (c + di) = (a - c) + (b - d)i |
| 11 | + * (a + bi)*(c + di) = (ac - bd) + (bc + ad)i |
| 12 | + * (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2) |
| 13 | + * <p> |
| 14 | + * You can also obtain the absolute value for a complex number using the following |
| 15 | + * formula: |
| 16 | + * |a + bi| = SQRT(a2 + b2) |
| 17 | + * (A complex number can be interpreted as a point on a plane by identifying the (a,b) |
| 18 | + * values as the coordinates of the point. The absolute value of the complex number |
| 19 | + * corresponds to the distance of the point to the origin, as shown in Figure 13.10b.) |
| 20 | + * <p> |
| 21 | + * Design a class named Complex for representing complex numbers and the |
| 22 | + * methods add, subtract, multiply, divide, and abs for performing complex number operations, |
| 23 | + * and override toString method for returning a string representation for a complex number. |
| 24 | + * The toString method returns (a + bi) as a string. If b is 0, it simply returns a. |
| 25 | + * Your Complex class should also implement the Cloneable interface. |
| 26 | + * <p> |
| 27 | + * Provide three constructors Complex(a, b), Complex(a), and Complex(). |
| 28 | + * Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. |
| 29 | + * Also provide the getRealPart() and getImaginaryPart() methods for returning the real and imaginary part |
| 30 | + * of the complex number, respectively. |
| 31 | + */ |
| 32 | +public class Complex extends Number implements Cloneable { |
| 33 | + |
| 34 | + private double a; |
| 35 | + private double b; |
| 36 | + private static double i = Math.sqrt(-1); |
| 37 | + |
| 38 | + public Complex() { |
| 39 | + this(0); |
| 40 | + } |
| 41 | + |
| 42 | + public Complex(double a) { |
| 43 | + this(a, 0); |
| 44 | + } |
| 45 | + |
| 46 | + public Complex(double a, double b) { |
| 47 | + this.a = a; |
| 48 | + this.b = b; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Example addition: (a + bi) + (c + di) = (a + c) + (b + d)i |
| 53 | + * |
| 54 | + * @param complex Complex number to add |
| 55 | + * @return the addition result |
| 56 | + */ |
| 57 | + public double add(Complex complex) { |
| 58 | + return (this.a + complex.a) + (this.b + complex.b) * i; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Subtract example: |
| 63 | + * a + bi - (c + di) = (a - c) + (b - d)i |
| 64 | + * |
| 65 | + * @param complex |
| 66 | + * @return |
| 67 | + */ |
| 68 | + public double subtract(Complex complex) { |
| 69 | + return (this.a - complex.a) + (this.b - complex.b) * i; |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * (a + bi)*(c + di) = (ac - bd) + (bc + ad)i |
| 75 | + * |
| 76 | + * @param complex |
| 77 | + * @return |
| 78 | + */ |
| 79 | + public double multiply(Complex complex) { |
| 80 | + return (this.a * complex.a - this.b * complex.b) + ((this.b * complex.a + this.a * complex.b) * i); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * (a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2) |
| 85 | + * |
| 86 | + * @param complex |
| 87 | + * @return |
| 88 | + */ |
| 89 | + public double divide(Complex complex) { |
| 90 | + return (this.a * complex.a + this.b * complex.b) / (Math.pow(complex.a, 2) + Math.pow(complex.b, 2)) + ((this.b * complex.a - this.a * complex.b) * i) / (Math.pow(complex.a, 2) + Math.pow(complex.b, 2)); |
| 91 | + } |
| 92 | + |
| 93 | + public double abs(Complex complex) { |
| 94 | + return Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2)); |
| 95 | + } |
| 96 | + |
| 97 | + public double getRealPart() { |
| 98 | + return a; |
| 99 | + } |
| 100 | + |
| 101 | + public double getImaginaryPart() { |
| 102 | + return b * i; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Complex clone() throws CloneNotSupportedException { |
| 107 | + return (Complex) super.clone(); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int intValue() { |
| 113 | + return (int) Math.round(a + b * i); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public long longValue() { |
| 118 | + return Math.round(a + b * i); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public float floatValue() { |
| 123 | + return (float) (a + b * i); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public double doubleValue() { |
| 128 | + return a + b * i; |
| 129 | + } |
| 130 | +} |
0 commit comments