|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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.
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.
We won't check off this lab (due to the exam), but by Friday's lab, you'll be expected to have completed and understood the above tasks!
Now we have an ugliness though: The constant 5.75 occurs twice in our program. Make it a named constant.
What good are named constants, again? Well, 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.75 need to be updated, and which 5.75's aren't referring to a wage (perhaps, it's also coincidentally the price of tea in China).
As a named constant, (not local to any particular field), 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
©2007, Ian Barland, Radford University Last modified 2007.Sep.19 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |