The SavingsAccount class given below is similar to that of Chapter 9. It should be illegal to construct savings accounts with a negative balance or interest rate. Modify the constructor so that an IllegalArgumentException is thrown in these cases.

Complete the following file:

SavingsAccount.java

// TODO: Modify the constructor to throw an IllegalArgumentException // if the initial balance or interest rate is < 0 /** An account that earns interest at a fixed rate. */ public class SavingsAccount extends BankAccount { /** Constructs a bank account with a given interest rate. @balance initialBalance the initial balance @param rate the interest rate */ private double interestRate; public SavingsAccount(double initialBalance, double rate) { super(initialBalance); interestRate = rate; } /** Adds the earned interest to the account balance. */ public void addInterest() { double interest = getBalance() * interestRate / 100; deposit(interest); } // This method checks your work. public static String check(double initialBalance, double rate) { try { SavingsAccount account = new SavingsAccount(initialBalance, rate); return "constructed"; } catch (IllegalArgumentException ex) { return "illegal argument"; } catch (Exception ex) { return "another error"; } } }

Use the following file:

BankAccount.java

/**
   A bank account has a balance that can be changed by 
   deposits and withdrawals.
*/
public class BankAccount
{  
   private double balance;
   
   /**
      Constructs a bank account with a zero balance.
   */
   public BankAccount()
   {  
      balance = 0;
   }

   /**
      Constructs a bank account with a given balance.
      @param initialBalance the initial balance
   */
 
   public BankAccount(double initialBalance)
   {  
      balance = initialBalance;
   }
 
   /**
      Deposits money into the bank account.
      @param amount the amount to deposit
   */
   public void deposit(double amount) 
   {  
      balance = balance + amount;
   }

   /**
      Withdraws money from the bank account.
      @param amount the amount to withdraw
   */
   public void withdraw(double amount) 
   {  
      balance = balance - amount;
   }

   /**
      Gets the current balance of the bank account.
      @return the current balance
   */
   public double getBalance()
   {  
      return balance; 
   }
   
   /**
      Transfers money from the bank account to another account
      @param amount the amount to transfer
      @param other the other account
   */
   public void transfer(double amount, BankAccount other)
   {  
      withdraw(amount);
      other.deposit(amount);
   }
 
}