home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.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
-
you only refer to fields inside getters and setters;
all other places should call the setter and getter.
-
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.
- 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?
- 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?
- 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),
…)
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs