You are on page 1of 2

Answers for Lab 2

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

EXERCISE 1

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


No, the instance variable interestRate should not be declared as static because
with each SavingsAccount object, it will have its own interest rate. If the
variable is static, the interest rate
value would not be able to change and remain the same for every object of the
class.

2. Write the output from the test harness for class SavingsAccount.
SavingsAccount: balance $115.0, interest rate 0.15

EXERCISE 2

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


methods demonstrate overloading?
The deposit method and withdraw method demonstrate overriding. There are no methods
demonstrating overloading.

4. Is bacc0 = chacc1 legal? Why?


Yes, this is legal because a parent class (BankAccount) variable is allowed to
reference a subclass (CheckingAccount).

5. Is chacc1 = bacc1 legal? Why?


No, this is not legal because a subclass cannot reference a parent class.

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


deductFees() valid? Explain.
On variable chacc1, the invocation to method deductFees() is valid because it is
the only class with the method deductFees(). The other variables bacc1 and sacc1 do
not have this method.

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


could not be fixed? Why?
bacc1 is fixed through casting because casting works through the inheritance
hierarchy. But sacc1 cannot be fixed through casting because it does not fit into
the same inheritance hierarchy as chacc1.

8. Does the program run after deleting the line causing the compilation
error? If the program crashed, why did it crash?
Yes it does, and the program crashed because bacc1 references a BankAccount object
not a CheckingAccount object.

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


to deposit(amount) in class CheckingAccount.java?
It is a stackoverflowerror which happens when their is no terminating condition on
a recursive call.

10. How many times is method deposit invoked?


The method deposit will be invoked infinitely because within the method it has
deposit(amount). This is calling its own method deposit which is recursion and will
invoke the method infinitely.
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()?
The compiler looks at the variable type of newAcc which is BankAccount not the
specific type since that would be dependent on the if statement.

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.
The invocation would be dependent on whether the user chooses c (CheckingAccount)
or s (SavingsAccount)
If the user chooses c, then the method toString() will be invoked from class
CheckingAccount
If the user chooses s, then the method toString() will be invoked from class
SavingsAccount

You might also like