// Represents a complex number of the form // // a + i * b // // Where i is the imaginary unit. class Complex { // real part private double a; // imaginary part private double b; // Construct a new complex number with real part a and // imginary part b. public Complex(double a, double b) { this.a = a; this.b = b; } // Returns a new complex number which is the sum of this // number and the passed number. public Complex add(Complex other) { return new Complex(this.a + other.a, this.b + other.b); } // Returns a squared version of this number. public Complex square() { return new Complex( this.a * this.a - this.b * this.b, this.a * this.b + this.a * this.b); } // Returns the magnitude of this number, defined as the // square root of a^2 + b^2. public double magnitude() { return Math.sqrt(a * a + b * b); } // Returns a String representation of the complex number. public String toString() { return this.a + " + i * " + this.b; } }