home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
lect04b
practice with fields
Review homework
In looking at the incoming homework, it's worth noting:
-
good indentation.
-
what are interesting test cases (result of drive(200) when
there isn't enough gas)
-
the constructor takes one argument, so it's signature should include that
What's going on
If we haven't already covered it, we'll show:
-
Make multiple BankAccount instances
(in the code pad, and/or our test method).
Now we can see what is meant by:
“a class is a template,
from which multiple objects (instances) are created”.
-
Talk about the flow of control: how information
is passed into methods via parameters
[local variables which get initialized for you],
and returned via the return statement.
We'll walk through an example, including
pictures of the memory diagrams on the board.
Adding behaviors to BankAccounts
If the previous material had been previously covered,
we'll add an method field to BankAccount:
-
add a getTotDeposits to our test cases.
(What information do we provide when calling the method (inputs),
and what information does it return (output)?)
Note that we hit an interesting question already,
before we start writing code:
What should the method return for a brand new BankAccount( 50.0 )?
-
Add a method-stub to our class, so it compiles.
-
Now, we actually implement this behavior,
which requires a few steps.
-
What additional state does a BankAccount have to keep track of?
Add a field.
-
Initialize that field.
(Where do we initialize fields, again?
And, how many constructors do we have?)
-
Now fill in our method-stub for getTotDeposits.
Do we pass our test cases?
-
Ah, okay —
update our existing code, so that our latest field is updated correctly.
Now we should pass our test cases!
-
Let's add another behavior,
doMonthlyMaintenance:
calling this method causes the total-amount-deposited to be reset to zero.
(In the future it might also do other things,
such as add interest to an account,
calculate any penalties,
and
cause a statement to be printed and mailed.)
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs