RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab05b
finishing fields; previewing new
lab05b

Previewing new

We can actually create instances without using any pop-up menus: we can use Java's new operator. That is:

int n;                   // Declare a variable local to the code pad. 
n = 17;                  // Initialize the variable.
n+3

PizzaServer bo;          // Declare a variable local to the code pad.
bo = new PizzaServer();  // Initialize the variable.
bo.pizzaArea(20);

Note: PizzaServer is a type, just like int and String! Java has a handful of “primitive” types which aren't objects: int, double, boolean, char (that's a type we haven't talked about yet). All other types are classes (which is why they start with a capital letter): String, PizzaServer, EmCee, …

Finishing fields

Finish off the last problems from Wednesday's lab, including the use of a named-constant as a field.

Make sure

  1. you only refer to fields inside getters and setters; all other places should call the setter and getter.
  2. your code for tip, work, and spend should each be one short call to addToBalance. (Note that you can add a negative amount to the balance, to represent spending!)

Some advantages of the above two approaches.

  1. Suppose that every time the bank balance is looked at, you need to call the function logAuditEntry(). How many places need to be updated, in your code?
  2. Suppose that every time a deposit of more than $5000 is made, the CIA needs to be notified (by calling a method notifyCIA()). How many places need to be updated, in your code?
  3. Suppose that every time the bank account balance is set to be less than zero, an additional $5.00 fee is immediately assessed. How many places need to be updated, in your code?
If your answer wasn't “one” to all of these questions, something is wrong! This should be true even if you've written three hundred more PizzaServer methods (like receiveAnnualBonus, getCostOfLivingRaise(double), stealFromTill(double), giveMoneyToFriend(PizzaServer, double), …)

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme