AP Computer Science --- Haas --- BankAccount

Below are two classes. One defines a bank account, the other uses it to create an account. Copy the two classes into BlueJ.

Complete the BankAccount class by adding the following methods:



/** >>>>>>>>>>>>>>>>>>> CLASS BankAccount <<<<<<<<<<<<<<<<<<
 * A bank account has a balance that can be changed by 
 * deposits and withdrawals.
 */
public class BankAccount
{  
   private double balance;        // instance variables are usually private
   private double interestRate;
        
   /**
    *  Constructor (0 parameters): Constructs a bank account with a zero balance
    *  >>> method overloading: more that 1 method with same name
    */
   public BankAccount()
   {   
      balance = 0;
      interestRate = 0;
   }

   /**
    *  Constructor (1 parameter): Constructs a bank account with a given balance
    */
   
      // <<<<<<------ COMPLETE THE CODE ------>>>>>>
    
  
 
   /** 
    * Constructor (2 parameter): both the initial balance 
    * and interest rate are set when a new account is created
    */
   
      // <<<<<<------ COMPLETE THE CODE ------>>>>>>
    

   
   /** 
    *  deposit - Method to deposit money into the bank account.
    *  Adds the amount given to the account balance
    */

        // <<<<<<------ COMPLETE THE CODE ------>>>>>>



   /**
    *  withdraw - Method to withdraw money from the bank account.
    *  Subtracts the amount given from the account.
    */

       // <<<<<<------ COMPLETE THE CODE ------>>>>>>



   /**
    *  Gets the current balance of the bank account.
    */
   public double getBalance()
   {   
      return balance;
   }

   /**
    * closeAccount - sets the balance to zero and returns the amount of 
    * money which was in the account before it closed
    */
   
       // <<<<<<------ COMPLETE THE CODE ------>>>>>>



   /**
    * setInterestRate - takes a decimal value as a parameter and sets a 
    * variable called interestRate to the value input. 
    */ 
   
       // <<<<<<------ COMPLETE THE CODE ------>>>>>>


    
    /**
     * getInterestRate - returns the current interest rate as a decimal value 
     */
    
       // <<<<<<------ COMPLETE THE CODE ------>>>>>>



     /**
      * addInterest - Method to add Interest to the account balance
      */

         // <<<<<<------ COMPLETE THE CODE ------>>>>>>

}


/**
   A class to test the BankAccount class.
*/
public class BankAccountTest
{
   public static void main(String[] args)
   {
      BankAccount mrHaasBigMoney; // haasBigMoney is a object variable,
			                      // it references an object
      mrHaasBigMoney = new BankAccount(); // initialize to new bankaccount
      System.out.println("Mr Haas >> " + mrHaasBigMoney.getBalance());

      mrHaasBigMoney.deposit(2000);
      System.out.println("Mr Haas >> " + mrHaasBigMoney.getBalance());
            
      mrHaasBigMoney.withdraw(500);
      System.out.println("Mr Haas >> " + mrHaasBigMoney.getBalance());
      
      mrHaasBigMoney.setInterestRate(0.02);
      mrHaasBigMoney.addInterest();
      System.out.println("Mr Haas >> " + mrHaasBigMoney.getBalance());
      
      BankAccount mrsHaasBigMoney;  // create another bank account
      mrsHaasBigMoney = mrHaasBigMoney;
      System.out.println("Mrs Haas >> " + mrsHaasBigMoney.getBalance());

      mrsHaasBigMoney.withdraw(1000);
      System.out.println("Mrs Haas >> " + mrsHaasBigMoney.getBalance());
      System.out.println("Mr Haas >> " + mrHaasBigMoney.getBalance());
     
   }
}