Exercise
1:Creating Bank Account Subclasses (Level 2)
In
this exercise, you will create two subclasses of the Account class in
the Banking project: SavingsAccount and CheckingAccount.
Task
1 - Modifying the Account
Class
Using
a text editor, modify the Account
class source file in the src/com/mybank/domain directory. This class must satisfy the
UML diagram in Figure 6-1 on page Lab 6-2; in particular, the balance
attribute and Account
class constructor are now protected
(indicated by the # character instead of the -
character ).
- Change the balance attribute from private to protected.
- Change the Account constructor from private to protected.
Task
2 - Creating the SavingsAccount Class
Using a
text editor, create the SavingsAccount
class source file in the src/com/mybank/domain/
directory. This class must satisfy the UML diagram in
Figure 6-1 on page Lab 6- 2 and the business rules defined in the
introduction to "Exercise 1: Creating Bank Account Subclasses (Level
1)" on page Lab 6-2.
Task
3 - Creating the CheckingAccount Class
Using a
text editor, create the CheckingAccount
class source file in the src/com/mybank/domain/
directory.
This class must satisfy the UML diagram in Figure 6-1 on page Lab 6- 2
and the business rules defined in the introduction to "Exercise 1:
Creating Bank Account Subclasses (Level 1)" on page Lab 6-2.
- Declare the CheckingAccount class in the com.mybank.domain package. The CheckingAccount class inherits from the Account class.
- Add the overdraftAmount atribute to the CheckingAccount class.
- Add a public constructor that takes two arguments: initBalance and overdraftAmount. Pass the initBalance parameter to the super constructor. Save the overdraftAmount parameter to the instance variable.
- Add a second public constructor that takes only one argument: initBalance. Call the first constructor with the initBalance parameter and use the default value 0.0 for the overdraftAmount parameter.
- Override the withdraw method to use the overdraftAmount variable. Here is the pseudo-code for the withdraw method:
if balance < amount
then
overdraftNeeded = amount -balance
if overdraftAmount < overdraftNeeded
then transaction fails
else
balance = 0
decrement overdraftAmount by overdraftNeeded
else
decrement balance by amount
Task
4 - Copying the TestBanking
Class
Copy the TestBanking.java
file file from the
resources/mod06_class1/ directory into the src/com/mybank/test
directory.
Task
5 - Compiling the TestBanking Program
On
the command line, use the javac
command to compile the test program.
Task
6 - Running the TestBanking Program
On
the command line, use the java
command to run the test program. You should see the output listed on page Lab 6-4.: