No Tribble At All
From Huben's Wiki
(Created page with "Today we will create a complex number class called Tribble, because when we use it in the next class it will fill up the screen.") |
|||
Line 1: | Line 1: | ||
− | Today we will create a | + | Today we will create a class called Complex that represents complex numbers. We ought to call it Tribble, because when we use it in the next session, it will fill up the screen. |
+ | |||
+ | Copy this code into Dr. Java. Then follow the numbered instructions. | ||
+ | <pre> | ||
+ | // Write a class Complex similar to your class Fraction. | ||
+ | // this class will have two double fields, real and imag. | ||
+ | // class Complex should be IMMUTABLE: you cannot change a complex | ||
+ | // number after you create it. You can only make new complex numbers. | ||
+ | // | ||
+ | // Replace this line with the class declaration. | ||
+ | { | ||
+ | // Contructors: | ||
+ | // no-arguments creates a 0.0+0.0i complex number | ||
+ | // 1 argument creates a complex representation of a real number. | ||
+ | // 2 arguments creates a complex number with real and imaginary parts. | ||
+ | // copy constructor that takes a Complex | ||
+ | // | ||
+ | // Write a toString method. | ||
+ | // | ||
+ | // Methods such as plus() need to create and return a new Complex. | ||
+ | // | ||
+ | // Write a static final constant I that is 0.0+1.0i. | ||
+ | // | ||
+ | // To do this assignment: | ||
+ | // | ||
+ | // (1) Write the class header (above.) | ||
+ | // (2) Create the fields. | ||
+ | // (3) Make the constructors. | ||
+ | // (4) Make the main() by writing the header and uncommenting parts | ||
+ | // one line at a time. | ||
+ | // (5) Write each method needed as you uncomment more of main. | ||
+ | // (6) Compare your results to the comments after each line. | ||
+ | // (7) If there is something you can't do, leave it commented out. | ||
+ | // | ||
+ | // Replace this line with the declaration of the main method. | ||
+ | { | ||
+ | // Complex c1 = new Complex(); | ||
+ | // Complex c2 = new Complex(3); | ||
+ | // Complex c3 = new Complex(-2, -5.5); | ||
+ | // Complex c4 = new Complex(2, 3); | ||
+ | // Complex c5 = new Complex(c4); | ||
+ | // implement toString before proceding further | ||
+ | // System.out.println(c1); // 0.0+0.0i | ||
+ | // System.out.println(c2); // 3.0+0.0i | ||
+ | // System.out.println(c3); // -2.0-5.5i | ||
+ | // System.out.println(c4); // 2.0+3.0i | ||
+ | // System.out.println(c5); // 2.0+3.0i | ||
+ | // System.out.println(c3.getReal()); // -2.0 | ||
+ | // System.out.println(c3.getImag()); // -5.5 | ||
+ | // no set methods, because immutable! | ||
+ | // System.out.println(c3.plus(c2)); // 1.0-5.5i | ||
+ | // System.out.println(c3.minus(c2)); // -5.0-5.5i | ||
+ | // System.out.println(c3.times(2.0)); // -10.0-11.0i | ||
+ | // System.out.println(c3.times(c4)); // 12.5-17.0i | ||
+ | // System.out.println(c3.conjugate()); // -2.0+5.5i | ||
+ | // System.out.println(c3.divide(c4)); // compare with your calculator | ||
+ | // Make the following work like Math.PI. | ||
+ | // System.out.println(Complex.I); // 0.0+1.0i | ||
+ | // System.out.println(Complex.I.times(3.3)); // 0.0+3.3i | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | </pre> |