// Arup Guha
// 3/8/2026
// Solution to COP 3330 Quiz #3 Questions 1 - 6.

public class ComplexNumber {

    private double a;
    private double b;

    public ComplexNumber(double real) {
        a = real;
        b = 0;
    }

    public ComplexNumber(double real, double img) {
        a = real;
        b = img;
    }
	
    // Answer to question #1, returns this + other.
	public ComplexNumber add(ComplexNumber other) {
		return new ComplexNumber(a+other.a, b+other.b);
	}
	
    // Answer to question #2, returns this + other.
	public ComplexNumber subtract(ComplexNumber other) {
		return new ComplexNumber(a-other.a, b-other.b);
	}
	
	// Answer to question #3, returns this * other.
	public ComplexNumber multiply(ComplexNumber other) {

		// Real component is product of a's, imaginary component is
		// negated since i squared is -1.
		double real = a*other.a - b*other.b;
		
		// This is just what happens when you FOIL.
		double img = a*other.b + b*other.a;

		// Here is our result.
		return new ComplexNumber(real, img);
	}
	
	// Answer to question #4, ust do what this says!
	public ComplexNumber conjugate() {
		return new ComplexNumber(a, -b);
	}

	// Answer to question #5, returns this/other.
	// Returns this divided by other. This is the most challenging.
	public ComplexNumber divide(ComplexNumber other) {
	
		// This helps make the denominator real.
		ComplexNumber term = other.conjugate();
		
		// The numerator of our answer.
		ComplexNumber num = this.multiply(term);
		
		// The real valued denominator.
		double den = other.a*other.a + other.b*other.b;
		
		// This is the way we're forced to do this.
		return new ComplexNumber(num.a/den, num.b/den);
	}
	
	// Answer to question #6, returns this raised to the power exp.
	public ComplexNumber pow(int exp) {
	
		// Start with the identity.
		ComplexNumber res = new ComplexNumber(1, 0);
		
		// Multiply this in the correct number of times.
		for (int i=0; i<exp; i++)
			res = res.multiply(this);
		return res;
	}
	
	// For testing.
	public String toString() {
		return a+ " + "+b+"i";
	}
	
	public static void main(String[] args) {
	
		// Basic tests for each method.
		ComplexNumber c1 = new ComplexNumber(2, 3);
		ComplexNumber c2 = new ComplexNumber(-1, 1);
		System.out.println(c1.add(c2));
		System.out.println(c1.subtract(c2));
		System.out.println(c1.multiply(c2));
		System.out.println(c1.divide(c2));
		System.out.println(c2.pow(8));
	}
}
