Lecture 28, CS 302-7 and 8, March 26

  1. Various things
    1. Program 3

                                                               i.     How to get started – read the JavaDocs!

  1. Review:
    1. Constructors

                                                               i.     Default constructors

                                                             ii.     Overloading constructors/methods

1.    what makes a method ‘unique’

    1. Tester Classes
    2. When you do and don’t need to use this
  1. Testing reminders:
    1. Incremental coding/unit testing
    2. Author’s six steps for implementing a class:

                                                               i.     Get an informal list of the responsibilities of your objects

                                                             ii.     Specify the public interface

                                                            iii.     Document the public interface

                                                           iv.     Determine instance variables

                                                             v.     Implement constructors and methods

                                                           vi.     Test your class

    1. Note – this is too formal.  You have to do them all at some point, but don’t need to think of them so formally/serially

                                                               i.     Basically: Plan, execute, verify

                                                             ii.     My approach – public interface, document, code, test. Substitute code for comments.

                                                            iii.     In general – approach will depend on complexity of problem

  1. Object oriented thinking
    1. Up until this point, have considered simple examples.
    2. We’ve had one class that represents an object, and we’ve tested that class with another tester class.

                                                               i.     But, we haven’t really dealt with complex problems/class structures yet.

    1. Given a problem, how break it down into classes?
    2. Nouns and verbs

                                                               i.     Nouns=classes

                                                             ii.     Verbs=methods

    1. Try to conceptualize an object as an actual thing

                                                               i.     An object is something you can draw, do something with, etc…

  1. Aggregation relationships between classes

                                            i.Object from class A contains object from class B.

1.      A ‘has a’ B

                                           ii.My example – Car and Driver

1.      Car and Driver are blueprints.

2.      Each car object contains zero or one driver objects

3.      Now, consider a Passenger class

a.       Each car can contain 0-3 passengers

                                         iii.Example from book – Quiz contains questions.

1.      Quiz is a class that is a blueprint for quizzes.

2.      Objects of the quiz class represent one particular quiz

3.      Question is a class that is a blueprint for questions.

4.      Objects of the question class represent one particular question.

b.      Think about how they relate to each other.

                                            i.First example (car/driver)

public class Car {

private Driver theDriver;

//private Passenger[] thePassengers;

}

                                           ii.Note – we can use arrays of objects we created

                                         iii.Book example (quiz/question)

public class Quiz {

private ArrayList<Question> questions;

}

                                         iv.Note – we can use ArrayLists of objects we created

1.      Specify our class as the generic type: <OurClass>

  1. Parallel Arrays
    1. Example – Suppose we own a grocery store.  We have a database that includes a bunch of info about our items.  This includes Name, Price, Description, expiration date, nutrition facts, etc.  How do we store this information?
    2. Naïve approach:

                                                               i.     Separate arrays for each attribute.

                                                             ii.     So, in order to add a new item, we need to update each array, etc.  Whenever we want to access info, need to access a bunch of arrays.

                                                            iii.     Gets messy…

    1. Better approach:

                                                               i.     Start with a class representing our database.

1.    Say, GroceryStore

                                                             ii.     Represent each item as an object, and store one array of those objects.

                                                            iii.     This makes more conceptual and programming sense.

                                                           iv.     So, something like a FoodItem class.  The constructor allows you to set all this info, and we have getter and setter methods for each item.

    1. Example – GroceryStore.java, FoodItem.java
  1. Overriding toString method
    1. In our previous example, we had to call each getter method (accessor) to retrieve the necessary info and print it out.
    2. It would be easier if we just had one method to call for doing this.
    3. ‘Override’ toString method by defining a new method called toString in the FoodItem class.  We can define it so that it prints out something we’re happy with
    4. Example – GroceryStore.java, FoodItem.java
  2. overloading vs overriding
    1. overloading – same method name, different params (in same class)
    2. overriding – same method name as inherited instance method, same params
  3. Helper instance methods
    1. Sometimes, we might have a fairly complicated method contained in one of the classes we create.  We might want to break it down into more than one method, for the same reasons that we originally broke our programs down into multiple methods.
    2. When we want to do this, we can define helper instance methods.
    3. How to declare:

private <return type> <name>(<params>){

}

    1. Why private?  Because it is only called by other methods within the same class.
    2. Example – GroceryStore.java, FoodItem.java
    3. Note – this means it cannot be called from other classes, like GroceryStore
  1. Sanity check:
    1. http://xkcd.com/974/
    2. Moral: don’t make the problem harder than it has to be.
    3. With object-oriented programming it is possible/easy to add a lot of structure to your program.

                                                               i.     But, don’t forget what the original purpose is.  Don’t add structure just for the sake of adding structure.

                                                             ii.     Trade-off between accessibility and generality.

    1. This is one trap a lot of ‘real’ programmers fall into, especially with languages like java.