## SWE/CS 332 In Class Exercise # 20

Name(s):
Consider a mutable complex number class:
```public class MComplex {
double re; protected double im;

public MComplex (double re, double im) { this.re = re; this.im = im; }

public double getReal()      { return re; }
public double getImaginary() { return im; }

public void setReal(double re)      { this.re = re; }
public void setImaginary(double im) { this.im = im; }

public void add (MComplex c) { re += c.re; im += c.im; }

public void subtract (MComplex c) { re -= c.re; im -= c.im; }

public void multiply (MComplex c) {
double r = re * c.re - im * c.im;
double i = re * c.im + im * c.re;
re = r; im = i;
}

public void divide (MComplex c) {
double den = c.re * c.re + c.im * c.im;
double r = (re * c.re - im * c.im) / den;
double i = (re * c.im + im * c.re) / den;
re = r; im = i;
}

@Override public boolean equals (Object o) {
if (o == this)               return true;
if (!(o instanceof MComplex)) return false;
MComplex c = (MComplex) o;

// See Bloch page 43 to find out why to use compare() instead of ==
return Double.compare(re, c.re) == 0 &&
Double.compare(im, c.im) == 0;
}

@Override public int hashCode () {
int result = 17 + hashDouble(re);
result = 31 * result + hashDouble(im);
return result;
}

private int hashDouble (double val) {
long longBits = Double.doubleToLongBits(val);
return (int) (longBits ^ (longBits >>>32));
}

@Override public String toString() { return "(" + re + " + " + im + "i)"; }
}
```

Before we get to immutability, consider the method contracts. Where do the various contracts "come from", and is there anything in the (missing) JavaDoc that might require a bit of research?

Apply each of Bloch's 5 rules:

1. Don't provide any methods that modify the object's state. How do you handle the mutators?

2. Ensure that no methods can be overridden.

Why is this a problem? Show me!

Fix the problem:

• Change the class declaration, or

• Change the method declarations, or

• Change the constructor visibility.

3. Make all fields final.

4. Make all fields private.

Is there a significant difference in visibility between `re` and `im`?