i. How to get started – read the JavaDocs!
i. Default constructors
ii. Overloading constructors/methods
1. what makes a method ‘unique’
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. Substitute code for comments.
iii. In general – approach will depend on
complexity of problem
i. But, 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, do
something 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.
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>
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…
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.
private <return type> <name>(<params>){
}
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.