AP Computer Science --- Haas --- MeasureableInterface

Below the complete code for the Measureable Interface, and classes DataSet, BankAccount, Coin, and DataSetTest.

Run it! Make sure you understand how it works!



/**
 *   Describes any class whose objects can be measured.
 */
public interface Measurable
{
  /**
   *  Computes the measure of the object.
   */
   double getMeasure();
}


/**
*   Keeps track of OBJECT with greatest value
*/
public class DataSet
{
   private Measurable maximum;

  /**
   *  Constructs an empty data set.
   */
   public DataSet() {
      maximum = null;
   }

   /**
   *    Checks for new object of greater value
   */
   public void add(Measurable x)  {
       if (maximum == null || maximum.getMeasure() < x.getMeasure())
         maximum = x;
   }

  /**
   *  Gets the largest of the added data.
   */
   public Measurable getMaximum()   {
      return maximum;
   }
}


/**
 *   A bank account has a balance that can be changed by 
 *   deposits and withdrawals.
 */
public class BankAccount implements Measurable
{  
   private double balance;
   private static int nextAccountNumber=0;
   private int accountNumber;

  /**
   *   Constructs a bank account with a zero balance
   */
   public BankAccount()  {   
      balance = 0;
      nextAccountNumber++;
      accountNumber = nextAccountNumber;
   }

  /**
   *   Constructs a bank account with a given balance
   */
   public BankAccount(double initialBalance)    {   
      balance = initialBalance;
      nextAccountNumber++;
      accountNumber = nextAccountNumber;
   }

  /**
   *   Deposits money into the bank account.
   */
   public void deposit(double amount)   {  
      double newBalance = balance + amount;
      balance = newBalance;
   }

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

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

  /**
   * Return the account number
   */
   public int getAccountNumber() {
       return accountNumber;
   }
}


/**
 *  A coin with a monetary value.
 */
public class Coin implements Measurable
{
   private double value;
   private String name;

  /**
   *   Constructs a coin.
   */
   public Coin(double aValue, String aName)    { 
      value = aValue; 
      name = aName;
   }

  /**
   *   Gets the coin name.
   */
   public String getName()    {
      return name;
   }

  /**
   *  Gets coin value.
   */
   public double getMeasure()    {
      return value;
   }
}


/**
*   This program tests the DataSet class.
*/
public class DataSetTest
{
   public static void main(String[] args)
   {
      /*** new coins ***/
      Coin coin1 = new Coin(0.25, "quarter");
      Coin coin2 = new Coin(0.10, "dime");
      Coin coin3 = new Coin(0.01, "penny");
      Coin coin4 = new Coin(0.05, "nickel");
       
      DataSet coinData = new DataSet();

      coinData.add(coin1);
      coinData.add(coin2);
      coinData.add(coin3);
      coinData.add(coin4);

      Coin maxCoin = (Coin) coinData.getMaximum();
      System.out.println("Highest coin value = " + maxCoin.getMeasure());
      System.out.println("Highest coin name = " + maxCoin.getName());
     
      /*** new bank acconts ***/
      BankAccount b1 = new BankAccount(25);
      BankAccount b2 = new BankAccount(10000);
      BankAccount b3 = new BankAccount(9000);

      DataSet bankData = new DataSet();
    
      bankData.add(b1);
      bankData.add(b2);
      bankData.add(b3);

      BankAccount maxAccount = (BankAccount) bankData.getMaximum();
      System.out.println("Highest account value = " + maxAccount.getMeasure());
      System.out.println("Highest account value number = " + maxAccount.getAccountNumber());
   }
}