Java Fundamentals  «Prev  Next»

Overloading Java Methods - Exercise

Objective: Overload a method and develop a test program to invoke the overloaded 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.

Exercise instructions

  1. Write the source code for a class Account that models an investment account. This account should accrue compound interest on an investment balance. Interest will be compounded on a monthly basis. The Account class should include instance variables balance and rate to represent the account balance and annual interest rate. Also, create two overloaded compound() methods that can be used to calculate a new balance based on compounded interest.
    The first compound() method will return no value and have no parameters. This method will simply add one month of interest to the balance of the account. Here's a code fragment that will compute the new balance of the account after 1 month:
    balance = balance * (1.0 + rate / 12.0);
    

    The second compound() method will also return no value but will take a single parameter that represents the number of months over which to compound interest. Here's a code fragment that will compute the new balance of the account after n months:
    balance = balance * Math.pow(1.0 + rate / 12.0, n);
    
  2. Write a test program called AccountTest that creates an Account object and sets the values of the instance variables balance and rate to 1000.0 and 0.09 respectively. Your object now represents an investment account with a balance of $1,000 and an annual interest rate of 9%. Invoke the first compound() method and then display the new balance. Next, invoke the second compound() method using 59 months and then display the new balance. This value represents the value of the account after 5 years (60 months). The balance should be $1,565.68. Feel free to play around with this test program to help plan for your retirement.

Coding 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 and AccountTest. Click Submit to submit your source code.