Radford University ITEC

ITEC120

lab05:
Adding to Classes

Today will be individual work, which will be checked off either before you leave lab today, or within the first 10min of Friday's lab.

You will need to follow the javadoc guidelines, as reviewed in lab04: start comments with /**, then a description, followed by @param and @return lines, before finishing the comment with */.

Starting with the changes made last week to PizzaServer.java, paste it into BlueJ, as your starting point.


Sep.22 (Fri)

(psst -- happy imminent autumnal equinox, tonight at 24:03)

Today we'll finish writing the above functions (and get checked off on all but the last). But, we actually have a significant improvement we can make to our code. Let's pause a moment.

The method void giveAll( PizzaServer aFriend ) is a bit odd, but reasonable: Suppose we ourselves are a the giveAll expert which lives inside a PizzaServer. When called on to do our work, we are handed a different PizzaServer, to whom we'll give our money. (We choose to call that other server “aFriend”.) If we want to ask what aFriend's salary is, we can of course just walk up to them and ask: aFriend.getSalary(). We can call any of aFriend's other methods, as well.

But what happens if, in the middle of this method, we want to call our own getSalary method? The answer is a bit odd: for once, we don' put any object-reference and dot (.) ; instead we just write getSalary(). In fact, we've seen an instance calling one of its own methods from day 1 -- crustArea just calls pieArea, without walking up to any other object!

So to access our own getSalary() method, we just call it directly.

Rule: From inside a method, if an instance just calls a method without a preceding object-refence and dot (.), it is are calling its own method.2

In fact, you should do this from within most of your own methods above: rather than assign to balance directly, call getBalance and setBalance!
Why? As a single point-of-control. Imagine that the pizzeria's auditors suddenly decide that every time a server's balance is set, there needs to be a log entry printed off to some file. That's easy to do, if setBalance is the only place in the code which sets that field. All other parts of the code should call that method, rather and assign to balance directly. Similary, getBalance should be the only way for any other method to request the balance.

Your task today (Fri): Besides getBalance and setBalance, make sure no method accesses balance directly; instead use the getter and setter methods. (Similarly for the field salary.)
As you are working on this, we'll check off your code for last wednesday's portion with a √-, √, or √+ (For extreme cases, √-- and √++), according to these guidelines.

If you finish this, go ahead an try your hand at writing giveAll. (You don't need to check this off additionally, though.) If you finish that, you're free to leave!


1If you prefer, you can write the function stealFrom, which works in the opposite manner.      back

2This is a lie. What actually happens when we write just a method name without an explicit reference, like “getSalary()”, Java re-writes this to “this.getSalary()”. The special variable this always exists, and it means “the object whose method we are currently in”. We'll talk more about this later.      back


©2006, Ian Barland, Radford University
Last modified 2006.Sep.22 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme