home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lab05a
fields
lab05a
Work through today's lab with a partner.
(You are encouraged to be brave,
and work with somebody you haven't worked with before.)
Starting with the changes made last week to
paste it into BlueJ, as your starting point.
-
Copy/paste
PizzaServer.java
(or, modify your previous version)
with the version we saw in Lecture: fields salary
and isManager,
along with “getter” methods
getSalary,
getIsManager
plus “setter” methods
setSalary,
setIsManager.
-
Using BlueJ's pop-up menus:
create two PizzaServer instances (creating them with the menu item
new PizzaServer(), and then (when BlueJ prompts you) give the name
of local variables to refer to these new servers.
-
Right-click on the red objects, and choose Inspect.
-
Call the getSalary on one of them to confirm they make
minimum wage,
and then set one of their salaries to something different.
Confirm that the salary of that PizzaServer changes,
while the other salary is unchanged.
-
What is a java expression which corresponds to the sum of the
salaries of the two PizzaServers you created above?
Type it in, in the Code Pad.
(All this so far, we've already seen in lecture (some recent, some old).)
-
In addition to a salary, suppose that PizzaServers also
have a bank account balance.
Add this field to the class, and add methods
getBalance and setBalance.
-
Add a behavior void tip(double amt),
which represents somebody a direct under-the-table tip which goes
straight into the server's balance.
Note that no value should be returned from this function1.
First write a stub version which compiles (but does nothing).
Then record a test case (where you call the function tip at least twice,
interspersed with calls to getBalance to see if it's working.
In order to assert what the expected answer should be, you'll need to
be calling getBalance by right-clicking the
object on the BlueJ bench.)
After creating the test cases, write the method.
Note that you should use
this.getBalance() and this.setBalance(double),
but not refer to the fields this.salary
or this.balance directly.
Principle: Only the getters and setters will refer to the field directly.
-
Add a behavior void work(double hrs), which
takes a number of hours worked and changes
the PizzaServer's
balance by the appropriate amount.
Record a test method (which calls work at least twice,
interspersed with calls to getBalance to confirm it worked.).
At the start of Friday's lab, have the above functions
ready to check off.
When checking off your program,
we will look for
good indentation,
meaningful variable names,
javadoc,
test cases
(recommended to have a test function as discussed,
though if you tested by hand as you went, that's okay too),
and
lack of repeated code.
-
Do you have repeated code in the above two methods?
Factor out the common code by adding a method addToBalance;
then go back and modify tip and work
so that they call addToBalance.
You'll want to record some new tests for addToBalance by itself,
but you won't have to re-record any tests for tip
or work, since you already recorded test cases for that.
(Of course, you should re-run those tests, to confirm the functions still work.)
-
Write a method spend, which is given a number
representing how much the server spends,
and adjusts the balance accordingly.
(Note that conceivably,
our PizzaServers might spend so much they go into debt.)
Extend your previous test function with some tests for this.
-
Revise setSalary so that it's consistent with federal law:
you can't set the salary to be lower than the minimum wage.
If setSalary is called with a lower value,
then the salary is set to the minimum.
Observe that even if setSalary was called by other functions
(say, “demote”), we now get compliance
with federal law, by simply making a change in one place.
If other places in the code had not used the setter,
then we would have to make a bunch of changes to comply with law.
-
Now we have an ugliness though:
The constant 5.15 occurs twice in our program.
And there might be other parts of code which need to refer to the minimum wage,
for other reasons.
And if the minimum wage changes, then we'll have to pore over2 our entire program, deciding
which versions of 5.15 need to be updated, and which 5.15's aren't referring
to a wage (perhaps, it's also coincidentally the price of tea in China).
Solution:
Make a named constant, but:
rather than have a named constant local to a method,
we'll make it a field!
That way, the constant can be used in multiple places:
both when initializing the salary,
and when running setSalary.
1
If you really want, you can program PizzaServers
to be polite and always return "Thank you!" when tipped. ↩
2Yep,
“to read extremely closely” is “pore over”,
not “pour over”. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs