1
|
- Announcements
- hw6 due tonight
- pass/fail, withdraw deadline April
8
- Agenda
- Questions
- Better BankAccount (Inheritance)
- Overriding, class Object
- toString
- Trees
|
2
|
- Several kinds of BankAccounts
- regular
- checking
- fee
- savings
- Simulated time: every month
- fee accounts are charged a fee
- savings accounts accrue interest
|
3
|
|
4
|
- What all BankAccounts share
- fields balance, transactionCount, issuingBank
- methods withdraw, deposit, incrementBalance, countTransaction, getters
and setters for fields
- Changes from BankAccount.java (4):
- abstract class (16): never new BankAccount()
- protected constructor (33): invoked by children only
- abstract method newMonth: (185) what each kind of BankAccount does on
first day of month,
implemented in each child class, called from Bank’s report and
newMonth methods
|
5
|
- Inherit from abstract BankAccount
- Write as little as possible – take all the defaults
- Constructor:
- public RegularAccount( int initialBalance, Bank issuingBank )
- {
- super(initialBalance,
issuingBank);
- }
- implement abstract method newMonth
- public void newMonth()
- {
- }
|
6
|
- Behavior: like RegularAccount, but can write checks too. Checks cost
money.
- Design
- inherit from BankAccount (not from RegularAccount!)
- write empty newMonth method
- write honorCheck method
- public int honorCheck( int amount )
- {
- incrementBalance( - checkFee
);
- return withdraw( amount );
- }
|
7
|
- Behavior: like RegularAccount, but each transaction costs money, and
there’s a monthly fee
- Design
- inherit from BankAccount
- write newMonth method to charge monthly fee
- override countTransaction method!
subtract fee, then do whatever else is customary
- public void countTransaction()
- {
- incrementBalance(
-transactionFee );
- super.countTransaction();
- }
|
8
|
- Behavior:
- 5% annual interest credited monthly
- only three free transactions per month, then each costs $1
- Design
- inherit from BankAccount
- write newMonth method to credit interest
- figure out how to keep track of number of transactions in a month,
override countTransaction method to charge fee when appropriate
- Write it for hw7
|
9
|
- Send aMessage to an object of class SomeClass
- Java looks in SomeClass for method matching aMessage with the right
signature
- If not found, looks in parent class of SomeClass (there always is a
parent, except for class Object)
- If child and parent both have the method
- child’s method overrides parent’s
- if you really want the parent’s method, use super
|
10
|
- Put features common to different kinds of objects in an abstract
superclass, often with abstract methods
- Make a (small) change in the behavior of some object by extending its
class and overriding a method
|
11
|
- Suppose
- SomeClass foo = new SomeClass( )
- Then these two expressions do the same thing:
- System.out.println( foo.toString() );
- System.out.println( foo );
- Every object knows how to respond to a toString message since there’s a
toString in class Object
- For “foo” etymology, see the full online dictionary of computer science
at http://foldoc.doc.ic.ac.uk
|
12
|
- It’s often nice to override toString, to provide an informative String
describing your particular kind of object
- NamedObject overrides toString (71-74)
- Create NamedObject instances named by command line arguments (33, 40)
- println …
- 34 nobj.toString()
- 35 nobj itself implicit
toString message
- 36 toString from class Object
weird
|
13
|
- NamedObject@206fdf64
- Not very informative
- (class name)@(weird number)
- weird number is actually base 16 (hexadecimal)
- weird number may change when program runs again
|
14
|
- Wrapper class for primitive type boolean
- From file Boolean.java in library:
- private boolean value; // field
- public String toString() {
- return value ? "true"
: "false";
- }
- Sun’s brace convention differs from ours
- test ? x : y expression on next
slide
|
15
|
- Has value x if test is true, else has value y
- same as
|
16
|
- TreeMapDemo.java
- 108 terminal.println(map.toString());
- produces output
- {one=1, three=3, two=1}
- “{ (key.toString()=value.toString(),
… }”
- ArrayList toString produces
- “[ 0th item toString, 1st item toString … ]”
- Very useful for debugging
|
17
|
- Common in computer science:
- Java class hierarchy (shows inheritance)
- Windows tree for files and
directories (folders)
- Vocabulary: Tree, hierarchy
- Root (often drawn at the top!)
- Child, parent, branch, leaf, node
- Draw with arrows, or in outline form
|
18
|
|
19
|
- folder: place where Windows keeps information
- For historical reasons, we use “directory” as a synonym for “folder”
- A directory can contain
- other directories (called subdirectories, subfolders)
- files
- Every directory is a subdirectory of its parent
- The world starts at the only directory (on the disk drive) with no
parent: the root directory,
called “\”
|
20
|
|