Lecture 28, CS 302-6, November 7

  1. Various things
    1. Program 3

                                                               i.     Highway Driving

                                                             ii.     Another game – goal – navigate car through highway with traffic, collect items, avoid crashing

                                                            iii.     How to get started – read the JavaDocs!

    1. Exam 2 11/17
    2. Interesting NYT article:

                                                               i.     http://www.nytimes.com/2011/11/06/education/edlife/why-science-majors-change-their-mind-its-just-so-darn-hard.html?_r=2&pagewanted=all

  1. Review:
    1. Tester Classes
    2. Constructors

                                                               i.     Default constructors

                                                             ii.     Overloading constructors/methods

1.    Whether or not a method with the same name is ‘unique’ is based upon the number, order, and type of parameters

    1. 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. These are really more formal than they have to be.  You will probably do them all at some point, but you don’t need to think of them so formally/serially

                                                               i.     Basically: Plan, execute, verify

                                                             ii.     My approach – public interface, document, code, test.  When I’m implementing my methods, I’ll sort of just replace my comments with code

                                                            iii.     In general – the ‘correct’ approach will depend on the complexity of the problem that you are working on

  1. Thinking like an object-oriented programmer
    1. Up until this point, we have mostly considered simple examples.
    2. One class that represents an object, and we’ve tested that class with another tester class.

                                                               i.     We haven’t really dealt with complex problems/class structures yet.

    1. Given a problem, how do we break it down into the classes/objects we will need to solve it?
    2. Think of the problem in terms of 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, play 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.      Also, consider a Passenger class

a.       Let’s say that 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.      Each object of the quiz class represents one particular quiz.

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

4.      Objects of the question class represent specific questions.

b.      Think about how they relate to each other

                                            i.Cars/Drivers/Passengers

public class Car {

private Driver theDriver;

private Passenger[] thePassengers;

}

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

    1. Quizzes/Questions

public class Quiz {

private ArrayList<Question> questions;

}

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

1.    This is because ArrayLists take a generic type

  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, etc.  How do we store this information?
    2. Naïve approach:

                                                               i.     Store 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…lots of code, easy to make mistakes

    1. Better approach:

                                                               i.     Start with a class representing our database.

1.    Say, GroceryStore

                                                             ii.     Represent each item as an object, and store an 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. Overriding the 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. What about toString?

                                                               i.     (This method can be called from any object)

                                                             ii.     But, it just prints out a reference to our object, not the info from it

    1. But…we can do something about this!
    2. We can ‘Override’ the toString method by defining a new method called toString in the FoodItem class.
    3. We can define it so that it prints out something we’re happy with.
    4. This is a method that takes no parameters, and returns a String
  1. Helper instance methods
    1. Sometimes, we might have a fairly complicated method contained in one of the classes we create.
    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. Note private methods cannot be called from other classes.

                                                               i.     In our example, the private helper method can only be called by other methods within FoodItem.java, and cannot be called directly from GroceryStore.java

  1. Object-oriented programming sanity check:
    1. http://xkcd.com/974/
    2. Moral: don’t make your solution to a 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 was.  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 – they spend more time making their program ‘perfect’ than they do making sure it works.
  1.  HW – for Friday – Add a concept of Aisles to our grocery store.  A grocery store contains some number of aisles.  An aisle contains some number of food items.  Also, we know how many aisles are in our store ahead of time, but we don’t know how many items are in each aisle.  Have fun with it – there are many different solutions.
    1. Pay particular attention to how you use toString, constructors, getters and setters at each level.
    2. Note - GroceryStore is like the tester class here
    3. Try to code this up – we’ll go over one solution on Wednesday