Designing and Implementing a Class

Step 1

 Determine how clients will use your class. To do this, consider writing a test class:

public class AccountTester {
   public static void main(String[] args) {
      Account savings = new Account(500.25);
      Account checking = new Account();
      savings.withdraw(200);
      checking.deposit(200);
      double bal = savings.getbalance();
      // this should print $300.25:
      System.out.println("savings = $" + bal);
   }
}

Step 2

Design the public interface for your class:

public class Account {
   public Account() {

   }
   public Account(double initBalance) {

   }
   public void withdraw(double amt) {

   }
   public void deposit(double amt) {

   }
   public double getbalance() {
      return 0; // for now
   }
} // end class Account

Step 3

Add javadoc comments:

/**
 * Instances of this class represent bank accounts
 *
 * @author Pearce
 * @version 1.0
 */
public class Account {
   /**
    * Creates an account with balance = $0
    */
   public Account() {

   }
   /**
    * Creates an account with a specified balance
    *
    * @param initBalance the initial balance
    */
   public Account(double initBalance) {

   }
   /**
    * Reduces the balance by the specified amount
    *
    * @param amt the amount to be withdrawn
    */
   public void withdraw(double amt) {

   }
   /**
    * Increments the balance by the specified amount
    *
    * @param amt the amount to be deposited
    */
   public void deposit(double amt) {

   }
   /**
    * returns the current balance
    *
    * @returns the current balance
    */
   public double getbalance() {
      return 0; // for now
   }
} // end class Account

Step 4

Add the fields:

/**
 * Instances of this class represent bank accounts
 *
 * @author Pearce
 * @version 1.0
 */
public class Account {
   private double balance;
   /**
    * Creates an account with balance = $0
    */
   public Account() {

   }
   /**
    * Creates an account with a specified balance
    *
    * @param initBalance the initial balance
    */
   public Account(double initBalance) {

   }
   /**
    * Reduces the balance by the specified amount
    *
    * @param amt the amount to be withdrawn
    */
   public void withdraw(double amt) {

   }
   /**
    * Increments the balance by the specified amount
    *
    * @param amt the amount to be deposited
    */
   public void deposit(double amt) {

   }
   /**
    * returns the current balance
    *
    * @returns the current balance
    */
   public double getbalance() {
      return 0; // for now
   }
} // end class Account

Step 5

Implement the constructors and methods:

/**
 * Instances of this class represent bank accounts
 *
 * @author Pearce
 * @version 1.0
 */
public class Account {
   private double balance;
   /**
    * Creates an account with balance = $0
    */
   public Account() {
      balance = 0;
   }
   /**
    * Creates an account with a specified balance
    *
    * @param initBalance the initial balance
    */
   public Account(double initBalance) {
      balance = initBalance;
   }
   /**
    * Reduces the balance by the specified amount
    *
    * @param amt the amount to be withdrawn
    */
   public void withdraw(double amt) {
      // if funds available
      balance = balance – amt;
   }
   /**
    * Increments the balance by the specified amount
    *
    * @param amt the amount to be deposited
    */
   public void deposit(double amt) {
      // if 0 <= amt
      balance = balance + amt;
   }
   /**
    * returns the current balance
    *
    * @returns the current balance
    */
   public double getbalance() {
      return balance;
   }
} // end class Account

Step 6

Test, test, test