Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match_all(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 834

Warning: Invalid argument supplied for foreach() in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 835

Warning: preg_replace(): Compilation failed: group name must start with a non-digit at offset 4 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 839

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860

Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/e5m7uo8vro0d/public_html/mediawiki/includes/MagicWord.php on line 860
No Tribble At All - Huben's Wiki

No Tribble At All

From Huben's Wiki
Jump to: navigation, search
(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 complex number class called Tribble, because when we use it in the next class it will fill up the screen.
+
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.
 +
 
 +
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>
 +
// 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();
 +
// implement toString before proceding further
 +
// System.out.println(c1);            // 0.0+0.0i
 +
// Complex c2 = new Complex(3);
 +
// System.out.println(c2);            // 3.0+0.0i
 +
// Complex c3 = new Complex(-2, -5.5);
 +
// System.out.println(c3);            // -2.0-5.5i
 +
// Complex c4 = new Complex(2, 3);
 +
// System.out.println(c4);            // 2.0+3.0i
 +
// Complex c5 = new Complex(c4);
 +
// 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, optional exercise
 +
// 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>

Latest revision as of 13:17, 26 October 2012

Personal tools
translate