Professor Abdul-Quader
Designing Classes
The Time project is much easier to implement if we have already implemented the Time component!
Problem: Ask the user to input in two GaussianIntegers (their real parts and their imaginary parts). Create the two GaussianIntegers, output them, add them, and then output their sum.
Recall: A Gaussian integer is a complex number of the form \(a + bi\), where \(a\) and \(b\) are integers (\(i\) is the imaginary number representing a square root of \(-1\)). How would you design a Gaussian integer?
Question: What are the nouns of this problem?
How should we design the add method? Option 1:
Option 2:
Which is more “object-oriented”?
What data is required to represent a Gaussian Integer? What are the actions it should be able to do?
Create a method to add another GaussianInteger object. To add two GaussianIntegers, add their real parts and add their imaginary parts (where do you store them?). Test it out in your main method:
Now create a method to multiply another GaussianInteger object. Test it out in the main method.
Hint: If \(g1 = a + bi\), and \(g2 = c + di\), then \(g1 \times g2 = (a + bi)(c + di) = (ac - bd) + (ad + bc)i\)
Why doesn’t the below multiply work:
public void multiply(GaussianInteger other) {
// real part: ac - bd
real = real * other.real - imag * other.imag;
// imaginary part: ad + bc
imag = real * other.imag + imag * other.real;
}Let’s try out the debugger to see what happens.
This video shows the IntelliJ debugger.
Recall: an object is mutable if its data (instance variables) can be changed after the object was created. It is immutable otherwise. (Strings are immutable; ArrayLists are mutable.)
Was our implementation of GaussianInteger mutable or immutable? Consider the following code:
GaussianInteger gi = new GaussianInteger(3, 4);
int something = SomeLibrary.libraryMethod(gi);
System.out.println(gi); // what will this output?Benefits of mutable objects:
Benefits of immutable objects:
Map and Set types, \(\ldots\)To re-design GaussianInteger to be an immutable type, first we can try to ensure that the instance variables can’t be changed: e.g. make them all final! First try that.
Any errors? They tell us what we need to change:
add and multiply.void, have them return new GaussianInteger objects.Re-design your GaussianInteger class to be immutable. Test it out with the following main method:
GaussianInteger one = new GaussianInteger(1, 0);
GaussianInteger i = new GaussianInteger(0, 1);
System.out.println(one); // should be 1 + 0i
System.out.println(i); // should be 0 + 1i
System.out.println(one.add(i)); // should be 1 + 1i
System.out.println(one); // should still be 1 + 0i
System.out.println(i.multiply(i)); // should be -1 + 0i
System.out.println(i); // should be 0 + 1iDue Monday, March 30 at 11:59 PM.
What are the nouns here?
You will probably need to add to this class later, as you start implementing the methods in the Library class! For example, you may need a getAuthor() or getTitle() method.