KEMBAR78
Answers Lab 2 | PDF
0% found this document useful (0 votes)
292 views2 pages

Answers Lab 2

This document contains answers to exercises from a lab on bank accounts. The exercises covered topics like inheritance, overriding, overloading, and casting between base and derived classes. Key points included that instance variables shouldn't be static, CheckingAccount overrides deposit(), withdraw(), and toString() from BankAccount, and errors can occur from incompatible types between classes.

Uploaded by

Ling Ma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
292 views2 pages

Answers Lab 2

This document contains answers to exercises from a lab on bank accounts. The exercises covered topics like inheritance, overriding, overloading, and casting between base and derived classes. Key points included that instance variables shouldn't be static, CheckingAccount overrides deposit(), withdraw(), and toString() from BankAccount, and errors can occur from incompatible types between classes.

Uploaded by

Ling Ma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Answers for Lab 2

-----------------------------------------------

EXERCISE 1

1. Should instance variable interestRate be declared as static? Explain.

Answer:
No, the interest rate can change based on the savings account.

2. Write the output from the test harness for class SavingsAccount.

Answer:
SavingsAccount: balance $115.0, interest rate 0.15

EXERCISE 2

3. Which method(s) of the class CheckingAccount demonstrate overriding? Which


methods demonstrate overloading?

Answer:
The following demonstrate overriding:
1) deposit()
2) withdraw()
3) toString()

The following demonstrate overloading:


1) None.

4. Is bacc0 = chacc1 legal? Why?

Answer:
Yes, CheckingAccount extends BankAccount so it is legal to have a BankAccount
variable reference a CheckingAccount object.

5. Is chacc1 = bacc1 legal? Why?

Answer:
No, it is not legal to have a child class' variable reference a parent class'
object.

6. On which variables bacc1, chacc1, sacc1 was the invocation to method


deductFees() valid? Explain.

Answer:
Only valid for chacc1, as CheckingAccount is the only class that has the method
public void deductFees().

7. Which compilation error(s) could be fixed through casting? Which one(s)


could not be fixed? Why?

Answer:
bacc1 could be cast as a CheckingAccount to fix the compilation error. This is
because CheckingAccount extends from BankAcount and bacc1 is a variable of the
parent class.
sacc1 could not be cast as it is a sister class.

8. Does the program run after deleting the line causing the compilation
error? If the program crashed, why did it crash?

Answer:
The program crashes due to incompatible types: SavingsAccount cannot be converted
to CheckingAccount.

9. What was the runtime error obtained when changing super.deposit(amount)


to deposit(amount) in class CheckingAccount.java?

Answer:
The error was: "class BankAccount cannot be cast to class CheckingAccount
(BankAccount and CheckingAccount are in unnamed module of loader 'app') at
TestBankAccounts.main"

10. How many times is method deposit invoked?

Answer:
Twice?

EXERCISE 3

11. Why does the compiler issue an error message when invoking
newAcc.getTransactionCount()?
Why does the compiler issue an error message when invoking
newAcc.getInterestRate()?

Answer:
The referenced getter methods don't exist in BankAccount.java.

newAcc.getTransactionCount() and newAcc.getInterestRate() require


getTransactionCount() and getInterestRate() to be defined, public methods in
BankAccount - they aren't there.

12. Can you tell whether in the statement String accountInfo = newAcc.toString();
the
method toString() being invoked is from the class CheckingAccount or from the class
SavingsAccount? Explain your answer.

Answer:
No, both SavingsAccount.java and CheckingAccount.java have toString() methods.
There is no way to discern which one is being called from BankMachine.

You might also like