/* * Complex.java * a complex number class * * Login: * * 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... // 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) { // You can also use the main method for testing! System.out.println("where the Complex... isn't!"); } }