/* Chris Poon
   Complex Numbers Class example (converted from Arup's C++ example)
	7-6-04
   Edited: 7/6/04 by Arup Guha (added instance add and mult.)

*/

public class Complex  
{
	private	double	real;
	private double	img;

	public Complex(){
		// Default constructor sets object to 0+0i.
		real = 0;
		img = 0;
	}

	public Complex(Complex copy){
		// Copy Constructor copies each component of the Complex object.
		real = copy.real;
		img = copy.img;
	}

	public Complex(double x, double y){
		// Constructor that sets the real and img components to the two parameters x
		// and y respectively.
		real = x;
		img = y;
	}

	public Complex conj(){
		// Returns the complex conjugate of the current object.
		Complex answer=new Complex(real, -1.0*img);
		return answer;
	}

	public double absVal(){
		// Returns the magnitute of the current object.
		return Math.sqrt(power(real,2) + power(img, 2));
	}

	public String toString(){
		// Converts the object to a readable String (for output)
		if (img <= 0)
		  return (real + (img +"i"));
		else
		  return (real + "+"+img+"i");
	}

	public void multfactor(double f){
		// This methods takes in a real value f and multiplies the current object
		// by this factor.
		real = real*f;
		img = img*f;
	}

	public static Complex add(Complex num1, Complex num2){
	    // This methods takes in two Complex objects and returns a third
	    // Complex object containing their sum.
	    Complex answer=new Complex();
	    answer.real = num1.real + num2.real;
	    answer.img = num1.img + num2.img;
	    return answer;
	}

	public Complex add(Complex num2) {
	    // This methods takes in two Complex objects and returns a third
	    // Complex object containing their sum.
	    Complex answer=new Complex();
	    answer.real = real + num2.real;
	    answer.img = img + num2.img;
	    return answer;
	}

	public static Complex mult(Complex num1, Complex num2){
		// This methods takes in two Complex objects and returns a third
		// Complex object containing their product.
  	    Complex answer=new Complex();
	    answer.real = num1.real*num2.real - num1.img*num2.img;
	    answer.img = num1.real*num2.img + num1.img*num2.real;
	    return answer;
	}

	public Complex mult(Complex num2){
		// This methods takes in two Complex objects and returns a third
		// Complex object containing their product.
  	    Complex answer = new Complex();
	    answer.real = real*num2.real - img*num2.img;
	    answer.img = real*num2.img + img*num2.real;
	    return answer;
	}
	
	// Calculates base raised to the power exp, given that
	// exp is non-negative.
	public static double power(double base, int exp) {
		double ans = 1;
		
		for (int i=0; i<exp; i++) {
			ans = ans*base;
		}
		
		return ans;
	}

	public static void main(String args[]){

		// Create three Complex objects.
		Complex m=new Complex(3,4);
		Complex j=new Complex(1,-2);
		Complex n=new Complex(m.conj());

		// Print out information about each.
		System.out.println( "m = " + m);
		System.out.println( " |m| = " + m.absVal());
		System.out.println("");

		System.out.println( "j = " + j);
		System.out.println( " |j| = " + j.absVal());
		System.out.println("");

		System.out.println( "n = " + n);
		System.out.println( " |n| = " +n.absVal());
		System.out.println("");

		// Test static multiplication method.
		j = Complex.mult(m,j);
		System.out.println( "j = " + j);
		System.out.println( " |j| = " + j.absVal());
		System.out.println("");

		// Test instance multiplication method.
		j = j.mult(m);
		System.out.println( "j = " + j);
		System.out.println( " |j| = " + j.absVal());
		System.out.println("");

		n.multfactor(4);
		System.out.println( "n = " + n);
		System.out.println( " |n| = " + n.absVal());
		System.out.println("");
	}
}

/* Sample I/O:

C:\src\BHSCI\1>java Complex
m = 3.0+4.0i
 |m| = 5.0

j = 1.0-2.0i
 |j| = 2.23606797749979

n = 3.0-4.0i
 |n| = 5.0

j = 11.0-2.0i
 |j| = 11.180339887498949

j = 41.0+38.0i
 |j| = 55.90169943749474

n = 12.0-16.0i
 |n| = 20.0


*/
