Chapter 6: Data set, coin and purse classes (non-generic)

/**
   This class computes the average of a set of data values.
*/
public class DataSet
{
   /**
      Constructs an empty data set.
   */
   public DataSet()
   {
      final int DATA_LENGTH = 100;
      data = new double[DATA_LENGTH];
      dataSize = 0;
   }

   /**
      Adds a data value to the data set
      @param x a data value
   */
   public void add(double x)
   {
      if (dataSize >= data.length)
      {  
         // make a new array of twice the size
         double[] newData = new double[2 * data.length];
         // copy over all elements from data to newData
         System.arraycopy(data, 0, newData, 0, data.length);
         // abandon the old array and store in data
         // a reference to the new array
         data = newData;
      }
      data[dataSize] = x;
      dataSize++;
   }

   /**
      Gets the average of the added data.
      @return the average or 0 if no data has been added
   */
   public double getAverage()
   {
      if (dataSize == 0) return 0;
      double sum = 0;
      for (int i = 0; i < dataSize; i++)
         sum = sum + data[i];
      return sum / dataSize;
   }

   private double[] data;
   private int dataSize;
}

import java.util.Random;

/**
   This program tests the DataSet class by adding 10,000 numbers
   to the data set and computing the average.
*/
public class DataSetTest
{
   public static void main(String[] args)
   {
      Random generator = new Random();
      DataSet data = new DataSet();
      final int COUNT = 10000;
      System.out.println("Adding " + COUNT + " random numbers.");
      for (int i = 0; i < COUNT; i++)
      {
         double x = generator.nextDouble();
         data.add(x);
      }
      double average = data.getAverage();
      System.out.println("average=" + average);      
   }
}
/**
   A coin with a monetary value.
*/
public class Coin
{
   /**
      Constructs a coin.
      @param aValue the monetary value of the coin.
      @param aName the name of the coin
   */
   public Coin(double aValue, String aName) 
   { 
      value = aValue; 
      name = aName;
   }

   /**
      Gets the coin value.
      @return the value
   */
   public double getValue() 
   {
      return value;
   }

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

   public boolean equals(Object otherObject)
   {  
      Coin other = (Coin)otherObject;
      return name.equals(other.name) 
         && value == other.value; 
   }

   private double value;
   private String name;
}

import java.util.ArrayList;

/**
   A purse holds a collection of coins.
*/
public class Purse
{
   /**
      Constructs an empty purse.
   */
   public Purse()
   {
      coins = new ArrayList();
   }

   /**
      Add a coin to the purse.
      @param aCoin the coin to add
   */
   public void add(Coin aCoin)
   {
      coins.add(aCoin);
   }

   /**
      Get the total value of the coins in the purse.
      @return the sum of all coin values
   */
   public double getTotal()
   {
      double total = 0;
      for (int i = 0; i < coins.size(); i++)
      {
         Coin aCoin = (Coin)coins.get(i);
         total = total + aCoin.getValue();       
      }
      return total;
   }

   private ArrayList coins;
}


import javax.swing.JOptionPane;

/**
   This class tests the Purse class by prompting the
   user to add coins into a purse and computing the total
   value of the purse.
*/
public class PurseTest
{
   public static void main(String[] args)
   {
      double NICKEL_VALUE = 0.05;
      double DIME_VALUE = 0.1;
      double QUARTER_VALUE = 0.25;

      Purse myPurse = new Purse();

      boolean done = false;
      while (!done)
      {
         String input 
            = JOptionPane.showInputDialog("Enter coin name or Cancel");
         if (input == null) 
            done = true;
         else
         {
            double value = 0;
            if (input.equals("nickel"))
               value = NICKEL_VALUE;
            else if (input.equals("dime"))
               value = DIME_VALUE;
            else if (input.equals("quarter"))
               value = QUARTER_VALUE;
            if (value != 0)
            {
               Coin c = new Coin(value, input);
               myPurse.add(c);
               double totalValue = myPurse.getTotal();
               System.out.println("The total is " + totalValue);
            }
         }
      }
      System.exit(0);
   }
}