No Tribble At All
From Huben's Wiki
Line 2: | Line 2: | ||
Copy this code into Dr. Java. Then follow the numbered instructions. | Copy this code into Dr. Java. Then follow the numbered instructions. | ||
+ | |||
+ | For those of you who have forgotten complex math, assume two complex numbers a and b. | ||
+ | |||
+ | a + b = (a<sub>real</sub> + b<sub>real</sub>) + (a<sub>imag</sub> + b<sub>imag</sub>) | ||
+ | |||
+ | a * b = (a<sub>real</sub> * b<sub>real</sub> - a<sub>imag</sub> * b<sub>imag</sub>) + (a<sub>real</sub> * b<sub>imag</sub> + a<sub>imag</sub> * b<sub>real</sub>) | ||
+ | |||
+ | b. conjugate = b<sub>real</sub> - b<sub>imag</sub> | ||
+ | |||
+ | a / b = (a * b.conjugate) / (b * b.conjugate) // note that b * b.conjugate is a real number, not complex. | ||
+ | |||
<pre> | <pre> | ||
// Write a class Complex similar to your class Fraction. | // Write a class Complex similar to your class Fraction. | ||
Line 36: | Line 47: | ||
{ | { | ||
// Complex c1 = new Complex(); | // Complex c1 = new Complex(); | ||
− | |||
− | |||
− | |||
− | |||
// implement toString before proceding further | // implement toString before proceding further | ||
// System.out.println(c1); // 0.0+0.0i | // System.out.println(c1); // 0.0+0.0i | ||
+ | // Complex c2 = new Complex(3); | ||
// System.out.println(c2); // 3.0+0.0i | // System.out.println(c2); // 3.0+0.0i | ||
+ | // Complex c3 = new Complex(-2, -5.5); | ||
// System.out.println(c3); // -2.0-5.5i | // System.out.println(c3); // -2.0-5.5i | ||
+ | // Complex c4 = new Complex(2, 3); | ||
// System.out.println(c4); // 2.0+3.0i | // System.out.println(c4); // 2.0+3.0i | ||
+ | // Complex c5 = new Complex(c4); | ||
// System.out.println(c5); // 2.0+3.0i | // System.out.println(c5); // 2.0+3.0i | ||
// System.out.println(c3.getReal()); // -2.0 | // System.out.println(c3.getReal()); // -2.0 | ||
Line 54: | Line 65: | ||
// System.out.println(c3.times(c4)); // 12.5-17.0i | // System.out.println(c3.times(c4)); // 12.5-17.0i | ||
// System.out.println(c3.conjugate()); // -2.0+5.5i | // System.out.println(c3.conjugate()); // -2.0+5.5i | ||
− | // System.out.println(c3.divide(c4)); // compare with your calculator | + | // System.out.println(c3.divide(c4)); // compare with your calculator, optional exercise |
// Make the following work like Math.PI. | // Make the following work like Math.PI. | ||
// System.out.println(Complex.I); // 0.0+1.0i | // System.out.println(Complex.I); // 0.0+1.0i |