package uk.ac.cam.rkh23.OOP.Complex;

/**
 * Immutable complex number. There is NO setter method, so 
 * users of the object have no way to change the state.  Additionally
 * we use final variables, which stops us from accidentally mutating the
 * stat ein an internal (private) function.
 * @author robert
 *
 */
public class ImmutableComplex {

	// Can set the state to final (must be set on initialisation
	// and can never then be changed
	private final double mImag;
	private final double mReal;
	
	/**
	 * This "constructor" is necessary to allow us to
	 * set the values on initialisation. We look more closely at constructors
	 * later on in the course
	 * @param i
	 * @param r
	 */
	public ImmutableComplex(double i, double r) {
		mImag=i;
		mReal=r;
	}
	
	/**
	 * Get imaginary part
	 * @return
	 */
	public double getImag() {
		return mImag;
	}
	
	/**
	 * Get real part
	 * @return
	 */
	public double getReal() {
		return mReal;
	}
	
	
	/**
	 * The add method cannot mutate the state so we have to make
	 * a new object and return that
	 * @param c
	 */
	public ImmutableComplex add(ImmutableComplex c) {
		ImmutableComplex ic = new ImmutableComplex(mReal + c.getReal(),
				mImag + c.getImag());
		return ic;
	}
	
	public static void main(String[] args) {
		// We create things using the constructor now
		ImmutableComplex a = new ImmutableComplex(1.0,1.0);
		ImmutableComplex b = new ImmutableComplex(1.0,1.0);
		
		// a and b will be unchanged by this call, but
		// a third ImmutableComplex will be created with their
		// complex sum
		ImmutableComplex sum = a.add(b);
	}
}
