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!
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
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
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
i. We haven’t really dealt with complex
problems/class structures yet.
i. Nouns==classes
ii. Verbs==methods
i. An object is something you can draw, play
with, etc…
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
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
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
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.
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
private <return type> <name>(<params>){
}
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
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.