CS II Lesson 10

Professor Abdul-Quader

Designing Classes

Demos

  • We’ll start with demos for project 1.

Plato and Aristotle in The School of Athens (Raphael)

Object Oriented Design

  • Classes are templates for data types
  • Represent data and behavior
  • Break your problem up into components: the nouns of your problem.
  • Implement each component in its own class
  • Think about the data required to represnt that component
  • Think about the actions the object should be able to do.

The Time project is much easier to implement if we have already implemented the Time component!

Design discussion

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?

OOP

Question: What are the nouns of this problem?

  • Real parts and imaginary parts of each GaussianInteger?
  • Just the GaussianIntegers?

Object-orientation

How should we design the add method? Option 1:

public void add(int otherReal, int otherImag)

Option 2:

public void add(GaussianInteger other)

Which is more “object-oriented”?

Implementation

What data is required to represent a Gaussian Integer? What are the actions it should be able to do?

Exercise (Part 1)

  • Create a GaussianInteger class.
  • Declare your instance variables.
  • Create a constructor for that class
  • Create the toString method.
  • In a separate class, make a main method that instantiates a GaussianInteger object and prints it out.

Exercise (Part 2)

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:

GaussianInteger g1 = new GaussianInteger(2, 3); // 2 + 3i
GaussianInteger g2 = new GaussianInteger(3, -5); // 3 + 5i
System.out.println(g1);
System.out.println(g2);
g1.add(g2); // Now g1 should be 5 + -2i
System.out.println(g1);

Exercise (Part 3)

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\)

// test it out in main:
GaussianInteger g = new GaussianInteger(1, 2); // 1 + 2i
GaussianInteger x = new GaussianInteger(3, 4); // 3 + 4i
g.multiply(x); // g should now be -5 + 10i
System.out.println(g);

Aside: Debugging

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.

Aside: Debugging

This video shows the IntelliJ debugger.

Immutability

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.)

Our GaussianInteger

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?
  • Hope: it should output “3 + 4 i”. Will that always happen?
  • Compare to what happens with integer parameters.

Benefits of mutable objects:

  • Flexibility.
  • Easier to design / reason about
  • Sometimes more efficient?

Benefits of immutable objects:

  • Safety! The object can be shared without worrying about what other methods / threads do to it.
  • Easier to reason about.
  • Other benefits require more material that we haven’t covered (yet): handles errors better, easier to parallelize, better to use with Map and Set types, \(\ldots\)

Immutable GaussianInteger

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.
  • Instead of making them void, have them return new GaussianInteger objects.
  • Now we don’t change our original object!

Changing add method

public GaussianInteger add(GaussianInteger other) {
    GaussianInteger gi = new GaussianInteger(...); // fill this in
    return gi;
}

Exercise

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 + 1i

Project 2: Library

Due Monday, March 30 at 11:59 PM.

  • Library
  • A Library contains a list of Books.
  • Each Book has a name, author, and a publication year.
  • For a library:
    • add a book to the library
    • list all books in the library
    • find a book (by title)
    • find all books by a given author
  • Main: loop around, give user the above choices, or the option to quit the program.

Design?

What are the nouns here?

Exercise: Book class

  • Implement the Book class.
  • First think about the data needed to represent it.
    • Declare the instance variables.
    • Create a constructor for the class
    • Create a toString method for it so that a Book is outputted in the format: Author, “Title” (year)

Caveat

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.

Library class discussion

  • What data (instance variables) should the Library class have?
  • What instance methods should the Library class have?

Upcoming

  • Project 2 due March 30. Full description on GHC tomorrow.
  • Quiz 3 (moved to next week).
  • (Early) Pi Day Lecture March 11 10 AM in 3001 Nat Sci
    • Dr. Alfred S. Posamentier (City Tech, CUNY)
    • Refreshments (pies?).
// reveal.js plugins