|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
(Time spent discussing exam problems/solutions,
and learning style results.)
Note that an Excel spreadsheet itself is a programming language;
we have functions, inputs, outputs, and (un)named constants.
(Local variables are just extra cells.
Excel makes it particularly easy to
map a function to an entire list of inputs.)
Objects are: state + behaviors.
(Book reading:
Section 5.1)
(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.
When you compile your class file (the blueprint), you don't actually create objects. The only way to create objects is by calling their constructor method. A constructor is a slightly odd method; it's called not by walking up to an object and asking it something (after all, there aren't any objects in existence when you start!), but rather by the keyword new:
new PizzaServer() new Emcee() |
We could add yet another field for PizzaServers, one which told which high school they graduated from. (High school graduation is a requirement for Krust-eaze Pizza Ink.) The problem is, what initial value should be given this field?
class PizzaServer { double balance = 0; String almaMater = /* ??? */; // ...other fields, and methods, go here ... } |
One approach might be to say “well, set a PizzaServer's
alma mater to "unknown", and then after creating
a new PizzaServer,
we'll hope that the person immediately calls setAlmaMater
to be the correct value.”
But that's begging for trouble.
A better solution: Whenever somebody uses new to create a new object, force them to provide the high school at that moment. We do this by writing a constructor ourselves, which takes an argument:
PizzaServer( String _almaMater ) { this.setAlmaMater( _almaMater + " High School" ); } |
We already know that to call the constructor, we use new:
new PizzaServer( "South" ) PizzaServer jo; jo = new PizzaServer( "North" ); |
You might be wondering, why we have been calling new all semester long, passing it zero arguments, without ever writing a constructor. It turns out, if you don't write a constructor explicitly, then Java secretly writes a “zero-argument constructor” for you. We've been relying on that fact up 'til now, with PizzaServer and all our other classes.
There are two unusual things to remember, about the constructor's signature:
A constructor is a method whose purpose is to make sure all fields get initialized.(The code inside the constructor runs immediately after fields are initialized.)
Something sneaky: When you write "hello", that's actually shorthand for new String("hello"). That's why we can call methods on Strings, and it's how we made Strings that had different states from each other.
Type each of the following in Code Pad. Explain what each line is doing.
PizzaServer jo; jo = new PizzaServer("West"); jo.pizzaArea(20); // We can have an object, even if we don't // use a local variable to remember it for later: (new PizzaServer("Skyline")).pizzaArea(20); // Fine, although we now have no way to ask // that particular PizzaServer any more questions. String greeting; greeting = new String("Howdy!"); greeting.length() (new String("hi")).length() "bye".length() // Odd, but otherwise just like above. // Relies on the fact that behind our backs, // Java re-writes `"bye"` to be `new String("bye")`. |
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Feb.27 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |