Java Fundamentals  «Prev  Next»

Overriding Methods - Exercise

Objective: Override a method and write a test program to invoke the overridden method.

Exercise Grading

The full credit for this exercise is 10 points. To receive full credit, you will need to successfully create the source code for the class and test program. You will submit your source code on this page.

Exercise Instructions

  1. Write the source code for a class AccountWithFee that is derived from the Account class which you built previously in this module. The AccountWithFee class models an investment account that includes a monthly management fee. You should add an instance variable named fee that reflects the monthly fee charged to the account. You should then override both of the compound() methods to subtract the monthly fee when calculating the new balance.
  2. Modify the test program, AccountTest, to create both an Account object and an AccountWithFee object. Set the values of the instance variables balance and rate to 1000.0 and 0.09 respectively in both objects. Set the fee instance variable of your AccountWithFee object to 5.0. Your objects now represent investment accounts with a balance of $1,000 and an annual interest rate of 9%. The second account also charges a $5 monthly management fee. Invoke the compound() method using 60 months for both accounts and display the new balances. The balances of the two accounts after 5 years should be $1565.68 and $1188.56 respectively. The seemingly insignificant monthly fee does not appear so insignificant after 5 years.

Hints

You do not need to worry about formatting your output or rounding the balance to the nearest cent.

What to submit

In the text box below, cut and paste the source code for Account, AccountWithFee and AccountTest. Click the Submit button to submit your source code.