Extending our interest calculator

exercise No. 100


Our current Account class does not handle negative balances accordingly. Typically banks will charge a different interest rate whenever an account is in debt i.e. having a negative balance. In this case a second so called default interest rate (being significantly higher) will be applied.

Extend the current project by adding a new instance variable defaultInterestRate along with getter and setter methods. Then change the implementation of applyInterest() and applyInterest(int years) by using the correct interest value according to the account's balance being positive or negative.


Do not forget to change the Javadoc comments accordingly!


We introduce a new variable defaultInterestRate to cover negative balance values:

private static double
interestRate = 1.5,           // applied to positive balances
defaultInterestRate = 15.;    // applied to negative balances

We need the appropriate getter and setter methods in Account:

 * @return
 *     the current default interest rate value.
public static double getDefaultInterestRate() {
  return defaultInterestRate;

 * This interest rate will be applied to negative balances. In contrast
 * {{@link #setInterestRate(double)} will handle positive balance values.
 * @param defaultInterestRate
 *                         the desired default interest rate value.
public static void setDefaultInterestRate(double defaultInterestRate) {
  Account.defaultInterestRate = defaultInterestRate;

The computed interest depends on positive or negative balance values:

public void applyInterest(int years) {
  if (0 < balance) {
    balance = balance * Math.pow((1 + interestRate / 100), years) ;
  } else if (balance < 0){
    balance = balance * Math.pow((1 + defaultInterestRate / 100), years) ;