/* * Complex.java * a complex number class * * Name: * * Comments to the graders: */ class Complex { private double real; private double imag; // place your constructors here: public Complex( double real_in, double imag_in ) { this.real = real_in; this.imag = imag_in; } public Complex( ) { this( 0.0, 0.0 ); } // *** Please use this toString method for consistency! *** // method: toString // in: no inputs // out: the String version of this Complex object public String toString() { String sign = "+"; // default sign is plus if (this.imag < 0.0) sign = "-"; // if imag is negative, make it a minus return "" + this.real + " " + sign + " " + Math.abs( this.imag ) + "i"; } // method: equals // in: any object o // out: true if o is Complex and its contents // match this's contents public boolean equals(Object o) { if (o instanceof Complex) { Complex c = (Complex)o; // cast o to a Complex // now we can access c's imaginary and real parts... // it's never a good idea to compare doubles for equality // s0 we check if they are within 0.000000042 return (Math.abs(c.imag - this.imag) + Math.abs(c.real - this.real) < 0.000000042); } else { return false; } } // place your other methods here: // main: one way to test things out // in: command-line arguments (not used) // out: nothing (it's void) public static void main(String[] args) { // a welcoming statement... System.out.println("Welcome to our Complex!"); // Uncomment these tests one at a time... /* // zero-input constructor Complex actual = new Complex( ); Complex expected = new Complex( 0.0, 0.0 ); test( actual, expected, 1 ); // two-input constructor actual = new Complex( 42.0, -60.0 ); expected = new Complex( 42.0, -60.0 ); test( actual, expected, 2 ); // addition Complex ca = new Complex(20.0,20.0); Complex cb = new Complex(22.0,40.0); actual = ca.add( cb ); expected = new Complex( 42.0, 60.0 ); test( actual, expected, 3 ); // negate ca = new Complex(-5.0,-47.0); actual = ca.negate( ); expected = new Complex( 5.0, 47.0 ); test( actual, expected, 4 ); // conjugate ca = new Complex(-5.0,-47.0); actual = ca.conjugate( ); expected = new Complex( -5.0, 47.0 ); test( actual, expected, 5 ); // addition ca = new Complex(2.0,3.0); cb = new Complex(4.0,-5.0); actual = ca.add( cb ); expected = new Complex( 6.0, -2.0 ); test( actual, expected, 6 ); // multiplication ca = new Complex(2.0,3.0); cb = new Complex(4.0,-5.0); actual = ca.multiply( cb ); expected = new Complex( 23.0, 2.0 ); test( actual, expected, 7 ); // division ca = new Complex(5.0,-200.0); cb = new Complex(1.0,2.0); actual = ca.divide( cb ); expected = new Complex( -79.0, -42.0 ); test( actual, expected, 8 ); */ // a departing statement... System.out.println("where the Complex... isn't!"); } // this method, named test, takes in // inputs: an actual and expected Complex value // also, the number of the test // outputs: no return value, but it prints whether the // test was passed or not... public static void test( Complex actual, Complex expected, int testnum ) { if ( actual.equals( expected ) ) { System.out.println("Test " + testnum + " passed."); } else { System.out.println("--- Test " + testnum + " failed:"); System.out.println("actual value: " + actual); System.out.println("expected value: " + expected); } } }