|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Recall that objects are: state + behaviors.
(What is the java term for “state”?
For “behaviors”?)
Some people like the equality “state = fields = nouns;
behaviors = methods = verbs”.
For each of the following, what fields might the class have?
What methods?
By the way, we'll see later that fields can be not just a primitive or built-in type (like int or String, resp.), but also any class we define. So a class FootballGame might contain two fields of type FootballTeam, where each FootballTeam contains (in turn) both a name and many fields of type FootballPlayer, where FootballPlayer is a class which would contain … and so on.
Using classes gives us a natural way to think about how to organize our data (namely: in a way which reflects how we humans conceive/organize the topic). Once we have the data well-organized, then writing the methods follows.
In lab, we introduced another field to PizzaServers, one to keep track of their piggy-bank balance. What were the getter and setter for this called? How would we write a method like work or tip, which would change that field? (Hint: don't access the field directly; only use the getter and setter.)
Discuss from lab: MINIMUM_WAGE. Where to put this, so it can be used by several methods? What are the advantages to naming this?
Looking ahead: This violates our previous statement that fields should only be information which is inherently part of the object -- information which separates one PizzaServer from another. We'll see next week, that named constants will be static fields: fields which actually belong to the class (blueprint) rather than having a copy within each individual object. We'll see that Math.PI and Integer.MAX_VALUE are such named constants.
Other constants in PizzaServer: CRUST_WIDTH, CENTS_PER_SQ_IN. Note that this last one (which was 3.0, and then later modified to 3.14) can now be clearly distinguished from Math.PI.
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Sep.16 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |