// Arup Guha
// 10/27/2018
// Solution to Complex Number Exercise for Junior Knights

import java.util.*;

public class ComplexNumber {
	
	private double real;
	private double img;
	
	// Default constructor.
	public ComplexNumber() {
		real = 0;
		img = 0;
	}
	
	// Constructor for a real value.
	public ComplexNumber(double r) {
		real = r;
		img = 0;
	}
	
	// Takes in both a real and imaginary part.
	public ComplexNumber(double r, double i) {
		real = r;
		img = i;
	}
	
	// Returns this ComplexNumber plus other.
	public ComplexNumber add(ComplexNumber other) {
		double newreal = this.real + other.real;
		double newimg = this.img + other.img;
		return new ComplexNumber(newreal, newimg);
	}
	
	// Returns this ComplexNumber plus other.
	public ComplexNumber subtract(ComplexNumber other) {
		// Fill this in.
	}	

	// Returns the magnitude of this ComplexNumber.
	public double magnitude() {
		return Math.sqrt(real*real+img*img);
	}
	
	// Creates and returns the complex conjugate of this number.
	public ComplexNumber conjugate() {
		return new ComplexNumber(real, -img);
	}
	
	// Divides this by realval and returns the result.
	public ComplexNumber divide(double realval) {
		return new ComplexNumber(real/realval, img/realval);
	}
	
	// If other is zero, returns null, otherwise returns 
	// this divided by other.
	public ComplexNumber divide(ComplexNumber other) {
		
		if (other.magnitude() < 1e-9)
			return null;
		
		// We have to multiply the fraction by the complex conjugate 
		// of the denominator, the new denominator is a real number
		// so we multiply and then divide by the appropriate real number.
		ComplexNumber othernum = other.conjugate();
		ComplexNumber res = this.multiply(othernum);
		
		// Here is the divide.
		return res.divide(other.magnitude());
	}
	
	// Returns the product of this ComplexNumber and other.
	public ComplexNumber multiply(ComplexNumber other) {
		// Fill this in.
	}
	
	// Returns a String representation of this object.
	public String toString() {
		return "("+real + " + " + img +"i)";
	}

}
