/* * Complex.java * a complex number class * * Name: * * Comments to the graders: */ class Complex { private double real; private double imag; // place your constructors here: // *** 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... return c.imag == this.imag && c.real == this.real; } 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 the Complex..."); // feel free to add to these lines of code // to test using main, but this is optional // they won't run until you add constructors /* Complex c0 = new Complex( 0.0, 0.0 ); System.out.println("c0 should be 0.0 + 0.0i"); System.out.println("c0 is " + c0); Complex c1 = new Complex( 42.0, -60.0 ); System.out.println("c1 should be 42.0 - 60.0i"); System.out.println("c1 is " + c1); */ // a departing statement... System.out.println("where the Complex isn't!"); } }