IMPORTANT. Make sure you attend the lab session to which you registered, show the result(s) of each exercise to your TA, and submit the .java files that you wrote and AnswersLab5.txt through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.
Create a new project for the classes BankAccount
and CheckingAccount
.
Create a new SavingsAccount
class in your project,
which extends the class BankAccount
(how should you
declare this class?). Besides the instance variables and methods inherited from class
BankAccount
, this class must contain one
instance variable of type double
called interestRate
. We might need to
create several objects of the new class SavingsAccount
, each one with its
own balance and interest rate. Should instance variable interestRate
be declared
as static
. Write your answer in AnswersLab5.txt; you must explain your answer.
public SavingsAccount(double initialAmount, double
rate)
. This constructor must initialize the interestRate
instance variable
with the value of rate
and it must also initialize the instance variable
balance
of class
BankAccount
with the value of initialAmount
(if you do not know how to do this, please review the lecture notes
on inheritance. If you still need additional help, please see class CheckingAccount
to see how to do this). getInterestRate()
that returns the value of
interestRate
.calculateInterest()
, which calculates one month's interest
and adds this amount to the balance; the interest is computed by multiplying
interestRate
and the balance. How can you get the balance from BankAccount
? How do you add the
interest to the balance of BankAccount
? (If you are unsure about how to do this,
please read class BankAccont
.)toString()
, which returns a string representation of the
instance variables; this string must be of the form "SavingsAccount: balance $123
, interest rate 0.12
".SavingsAccount
class and
run the program. Write the output in AnswersLab5.txt.
Download the file
TestBankAccounts.java and add it to your project. This class has a main method that
creates several bank account objects of the three types
BankAccount
, CheckingAccount
,
and SavingsAccount
.
In each step of this exercise, you will be adding statements to the main method, running it after each addition. Answer the questions at each step and write your answers in AnswersLab5. If there is a compilation error, make sure you comment out the offending statement before advancing to the next step of this exercise.
CheckingAccount
demonstrate overriding? Which
demonstrate overloading?
TestBankAccounts
(code must be
added near the end of the main
method, after the last comment line):
bacc0 = chacc1
; which makes the
BankAccount variable bacc0
reference the CheckingAccount
object referenced by chacc1
. Was this
legal? Why?
System.out.println(bacc0.toString());
and run. Which toString
method did it use, the one from the
BankAccount
class or the one from the CheckingAccount
class? Why?
chacc1 = bacc1
.
Was this legal? Why not?
BankAccount bacc2 = new CheckingAccount(200.0);
then add the
statement chacc1 = bacc2;
. Fix this last statement using casting so that it
compiles and runs.
deductFees()
, without parameters, on all variables
bacc1
,
chacc1
, and sacc1
. On which type of objects was this legal? Why?
((SavingsAccount)variable).deductFees()
. Which compilation error(s) could be fixed
this way? Which one(s) could not be fixed this way? Why?
chacc1.deposit(100.0);
Your program should compile and run.CheckingAccount.java
: in the code for the
deposit method, change
super.deposit(amount);
to deposit(amount);
Compile and run.
What was the runtime error?
TestBanckAccounts
at the line chacc1.deposit(100.0);
(review Lab 3 if you do not remember how to do this).deposit
invoked?getTransactionCount()
and the other when invoking getInterestRate
.
Explain why the compiler issues these two errors. Write your answer in AnswersLab5.
String accountInfo = newAcc.toString();
just by looking at the code can you tell whether
the method toString
being invoked is the one from the class
CheckingAccount
or the one form the class SavingsAccount
? Explain
your answer and write it in AnswersLab5.
newAcc.getTransactionCount()
is
invoked only if newAcc
references an object of the class
CheckingAccount
, and newAcc.getInterestRate()
is invoked
only if newAcc
references an object of the class SavingsAccount
.
Run the program and verify that it works correctly (if you run the program and type either 'c'
or 's' the program must print a message indicting that an account has been created and
it also prints information about the account including either the interest rate or the number of transactions).
Please try to answer the above question on your own. If you do not see how to modify the code
as required, you can read these hints.