reset password
Author Message
Anonymous
Posts: 166
Posted 21:41 Jan 18, 2014 |

For the homework problem E 3.3 may we use the following?

/**
   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)
   {  
      double newBalance = balance + amount;
      balance = newBalance;
   }

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

   /**
      Gets the current balance of the bank account.
      @return the current balance
   */
   public double getBalance()
   {   
      return balance;
   }
}
Anonymous
Posts: 166
Posted 21:46 Jan 18, 2014 |
/**
  2     A class to test the BankAccount class.
  3  */
  4  public class BankAccountTester
  5  {
  6     /**
  7        Tests the methods of the BankAccount class.
  8        @param args not used
  9     */
 10     public static void main(String[] args)
 11     {
 12        BankAccount harrysChecking = new BankAccount();
 13        harrysChecking.deposit(2000);
 14        harrysChecking.withdraw(500);
 15        System.out.println(harrysChecking.getBalance());
 16        System.out.println("Expected: 1500");      
 17     }
 18  }

I think we use this form?

khsu
Posts: 30
Posted 11:46 Jan 19, 2014 |

The exercise wanted us to write a BankAccount class, I assume we have to start from scratch.

You can use that as a sample and then write up a new one similar to it later.