Complex Numbers

Create a class named ComplexNumber that will allow someone to create and work with complex numbers.

The following methods should be implemented:

public String toString() 
//returns the string representation for the current complex value.  
//(something similar in format to "(3.0 + 4.0i)" or "(3.0 - 4.0i)"
//Note carefully the spacing and that the parentheses should be included

public double real()
//returns the real part of the current complex number 
//(i.e., if z = a + bi, this method returns the value of "a")

public double imag()
//returns the imaginary coefficient of the current complex number 
//(i.e., if z = a + bi, this method returns the value of "b")

public void multiplyByScalar(double c);
//multiplies the current complex number by c
//note: this method should change the value of the current complex number to this new value

public static ComplexNumber scalarProduct(double c, ComplexNumber z)
//returns a new complex number equal to c times z

public ComplexNumber conjugate()
//returns a new complex number that is the conjugate of the current complex number

public static ComplexNumber sum(ComplexNumber z1, ComplexNumber z2)  
//returns a new complex number equal to z1 + z2

public ComplexNumber plus(ComplexNumber z)
//returns a new complex number equal to the sum of the current complex number and z

public void add(ComplexNumber z)
//adds z to the current complex number 
//note: this method should change the value of the current complex number to this new value

public static ComplexNumber difference(ComplexNumber z1, ComplexNumber z2)
//returns a new complex number equal to z1 - z2

public ComplexNumber minus(ComplexNumber z)
//returns a new complex number equal to the difference of the current complex number and z

public void subtract(ComplexNumber z)
//subtracts z from the current complex number
//note: this method should change the value of the current complex number to this new value

public static ComplexNumber product(ComplexNumber z1, ComplexNumber z2)
//returns a new complex number equal to z1 * z2

public ComplexNumber times(ComplexNumber z)
//returns a new complex number equal to the product of the current complex number and z

public void multiply(ComplexNumber z)
//multiplies the current complex number by z
//note: this method should change the value of the current complex number to this new value

public static ComplexNumber quotient(ComplexNumber z1, ComplexNumber z2)
//returns a new complex number equal to z1/z2, unless z2 = 0, in which case it returns null

public ComplexNumber dividedBy(ComplexNumber z)
//returns a new complex number equal to the quotient of the current complex number and z, unless z = 0, in which case it returns null

public void divide(ComplexNumber z)
//divides the current complex number by z
//note: this method should change the value of the current complex number to this new value

public double norm()
//returns the distance from the current complex number to zero (i.e., 0 + 0i)

public double arg()
//returns the angle in radians between the positive real axis (i.e. the x-axis) 
//and the line segment connecting zero (i.e., 0 + 0i) and the current complex 
//number.  Example: arg(1+i) = pi/4

As a way to test your code, note that the following class should produce the output given below it

public class ComplexNumberFun {

	public static void main(String[] args) {
		ComplexNumber z1 = new ComplexNumber(3,4);
		ComplexNumber z2 = new ComplexNumber(-5,7);
		ComplexNumber z3 = new ComplexNumber(9,-4);
		ComplexNumber z4 = new ComplexNumber(2,-3);
		ComplexNumber z5 = new ComplexNumber(-Math.sqrt(3.0)/2.0, 1.0/2.0);
		System.out.println(z1.imag());
		System.out.println(z1.real());  
		System.out.println(z1.conjugate());
		System.out.println(z1.norm());   
		System.out.println(6.0*z5.arg()/5.0);  
		System.out.println("-----------");
		System.out.println("5 * " + z1 + " = " + 
                          ComplexNumber.scalarProduct(5,z1));
		System.out.println(z1 + " + " + z2 + " = " + ComplexNumber.sum(z1,z2));
		System.out.println(z1 + " - " + z2 + " = " + 
                          ComplexNumber.difference(z1,z2));
		System.out.println(z1 + " * " + z2 + " = " + ComplexNumber.product(z1,z2));
		System.out.println(z1 + " / " + z2 + " = " + 
                          ComplexNumber.quotient(z1,z2));
		System.out.println(z1.plus(z2).times(z3).minus(z4).dividedBy(z5));
		System.out.println("-----------");
		System.out.println(z1);
		System.out.println(z2);
		System.out.println(z3);
		System.out.println(z4);
		System.out.println("-----------");
		z1.add(z4);
		z2.subtract(z4);
		z2.multiplyByScalar(5);
		z3.multiply(z4);
		z4.divide(z4);
		System.out.println(z1);
		System.out.println(z2);
		System.out.println(z3);
		System.out.println(z4);	
	}

}

Output of ComplexNumberFun class above:

4.0
3.0
(3.0 - 4.0i)
5.0
3.141592653589793
-----------
5 * (3.0 + 4.0i) = (15.0 + 20.0i)
(3.0 + 4.0i) + (-5.0 + 7.0i) = (-2.0 + 11.0i)
(3.0 + 4.0i) - (-5.0 + 7.0i) = (8.0 - 3.0i)
(3.0 + 4.0i) * (-5.0 + 7.0i) = (-43.0 + 1.0i)
(3.0 + 4.0i) / (-5.0 + 7.0i) = (0.17567567567567569 - 0.5540540540540541i)
(34.21539030917348 - 107.26279441628827i)
-----------
(3.0 + 4.0i)
(-5.0 + 7.0i)
(9.0 - 4.0i)
(2.0 - 3.0i)
-----------
(5.0 + 1.0i)
(-35.0 + 50.0i)
(6.0 - 35.0i)
(1.0 + 0.0i)