CMPU102 - February 20th - Lab 4
Part 1: Write a BankAccount class
First, create
an abstract class called BankAccount, that contains the following
members:
- protected double balance that holds the current
balance of the account
- two constructors:
- a zero-parameter constructor that creates a new account with a 0.00 balance
- a one-parameter constructor that takes an initialAmount as
a parameter and uses that to set the value of balance.
- The BankAccount.java class should have 4 public methods:
Part 2: Create 2 subclasses of BankAccount
Implement 2 subclasses of BankAccount called SavingsAccount.java and
CheckingAccount.java (use the extends keyword in the class declaration
line of these 2 subclasses).
Remember that subclasses
inherit all public methods and all protected data fields
of the BankAccount superclass.
Subclass 1:SavingsAccount.java:
In
addition to the functions associated with a bank account (defined in
the parent class BankAccount), a savings account
pays periodic interest, so the savings account
implementation will need a private double instance variable interestRate
of .005.
SavingsAccount should
have the following constructor and public methods:
- a one-parameter constructor that takes an initialAmount and sends it in a
call to the one-parameter constructor of superclass BankAccount.
- public
void addPeriodicInterest(): Uses balance (a variable
in the superclass) and interestRate to
calculate the interest amount (assume interestRate is specified as decimal number .005 so the amount to deposit
can be calculated by just multiplying interestRate by balance) and deposit that amount using
the superclass deposit method.
- public String toString(): prepends the String "Savings "
onto a call to the superclass toString method. This is known as
partial overriding.
public String toString(){
return "Savings "+super.toString();
}
Subclass 2: CheckingAccount.java:
In
addition to the functions associated with a bank account (defined
in the parent class), a checking account
has a penalty for a balance below $5000, so the checking account
implementation will need a double instance variable penalty set to
$5.00. The MINIMUM_BALANCE should be declared as a static final double set equal to 5000.
CheckingAccount should
have the following constructor and methods:
- a one-parameter constructor
that takes an initialAmount and sends initialAmount in a
call to the one-parameter constructor of superclass BankAccount -- super(initialAmount).
- public
void subtractPenalty(): If the balance in the account is less
than $5000, subtract the penalty from the balance.
- public String toString(): prepends the String "Checking "
onto a call to the superclass toString method:
public String toString(){
return "Checking "+super.toString();
}
Part 3: Test your classes
Download the file AccountTest.java from the course website
and use it to test your class
definitions.
After compiling, run AccountTest in the interactions
window. You should get output as shown below (ask a coach if you get
a syntax error).
> run AccountTest
Savings Account balance: 8000.00
Savings Account balance: 2000.00
Checking Account balance: 4500.00
Uncomment the 3 commented-out lines in the main method
and the processAccount method of the AccountTest class and
recompile. You should get a syntax error this time.
The error occurs because the method processAccount
takes an object of the supertype as a parameter and calls a
method defined in a subtype (but not defined in the supertype) on that object.
What can you do to convert an object from a
supertype to a subtype? Type cast the object as its instantiated
subtype!
For example, if a method was defined to take a supertype
parameter Object (the ancestor of all Java class types)
and you wanted to call the length method on the
parameter (because you're assuming the parameter is a String), you could cast the
Object parameter as type String and call the length method in the same
line as follows:
public void printObjectLength(Object obj)
{
System.out.println(((String)obj).length());
}
or
public void printObjectLength(Object obj)
{
String localString = ((String)obj); // change obj type first
System.out.println(localString.length()); // call length on String
}
Notice that the cast operation in the examples
is enclosed along with the Object name
inside a set of parentheses. Both examples accomplish the same thing.
If you want to make sure that obj is a String before casting it as
a String, you could use the instanceof operator as follows:
public void printObjectLength(Object obj)
{
if (obj instanceof String)
System.out.println(((String)obj).length());
}
Fixing the errors in the AccountTest class will involve casting
objects that are declared as a supertype into the subtype that defines
the method being called.
After
fixing the errors, the output should look like this:
> java AccountTest
Savings Account balance: 8040.00
Savings Account balance: 2020.00
Checking Account balance: 4495.00
Demonstrate this program for a coach or your professor to get credit for this lab and be
sure to keep a copy for yourself.