DataSet.java
/**
Computes the average of a set of data values.
*/
public class DataSet
{
private double sum;
private Measurable maximum;
private int count;
. . .
/**
Constructs an empty data set.
*/
public DataSet()
{
sum = 0;
count = 0;
maximum = null;
. . .
}
/**
Adds a data value to the data set
@param x a data value
*/
public void add(Measurable x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
maximum = x;
count++;
. . .
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public Measurable getMaximum()
{
return maximum;
}
/**
Gets the smallest of the added data.
@return the minimum or 0 if no data has been added
*/
public Measurable getMinimum()
{
. . .
}
}
Use the following file:
DataSetTester.java
/**
This program tests the DataSet class.
*/
public class DataSetTester
{
public static void main(String[] args)
{
DataSet bankData = new DataSet();
bankData.add(new BankAccount(1000));
bankData.add(new BankAccount(10000));
bankData.add(new BankAccount(1000));
System.out.println("Average balance: "
+ bankData.getAverage());
System.out.println("Expected: 4000");
Measurable min = bankData.getMinimum();
System.out.println("Lowest balance: "
+ min.getMeasure());
System.out.println("Expected: 1000");
DataSet coinData = new DataSet();
coinData.add(new Coin(0.25, "quarter"));
coinData.add(new Coin(0.1, "dime"));
coinData.add(new Coin(0.05, "nickel"));
System.out.println("Average coin value: "
+ coinData.getAverage());
System.out.println("Expected: 0.133333");
min = coinData.getMinimum();
System.out.println("Lowest coin value: "
+ min.getMeasure());
System.out.println("Expected: 0.05");
}
}