Java Reference
In-Depth Information
Consider the DataSet class of Chapter 6 . We used that class to compute the average
and maximum of a set of input values. However, the class was suitable only for
computing the average of a set of numbers. If we wanted to process bank accounts to
find the bank account with the highest balance, we would have to modify the class,
like this:
public class DataSet // Modified for BankAccount objects
{
. . .
public void add( BankAccount x)
{
sum = sum + x .getBalance() ;
if (count == 0 || maximum .getBalance() <
x .getBalance() )
maximum = x;
count ++;
}
public BankAccount getMaximum()
{
return maximum;
}
private double sum;
private BankAccount maximum;
private int count;
}
388
389
Or suppose we wanted to find the coin with the highest value among a set of coins.
We would need to modify the DataSet class again.
public class DataSet // Modified for Coin objects
{
. . .
public void add( Coin x)
{
sum = sum + x .getValue() ;
if (count == 0 || maximum .getValue() <
x .getValue() )
maximum = x;
count++;
}
public Coin getMaximum()
{
return maximum;
Search WWH ::




Custom Search