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);