1 /**
2 Computes the average of a set of data values.
3 */
4 public class DataSet
5 {
6 private double sum;
7 private Object maximum;
8 private int count;
9 private Measurer measurer;
10
11 /**
12 Constructs an empty data set with a given measurer.
13 @param aMeasurer the measurer that is used to measure data values
14 */
15 public DataSet(Measurer aMeasurer)
16 {
17 sum = 0;
18 count = 0;
19 maximum = null;
20 measurer = aMeasurer;
21 }
22
23 /**
24 Adds a data value to the data set.
25 @param x a data value
26 */
27 public void add(Object x)
28 {
29 sum = sum + measurer.measure(x);
30 if (count == 0 || measurer.measure(maximum) < measurer.measure(x))
31 maximum = x;
32 count++;
33 }
34
35 /**
36 Gets the average of the added data.
37 @return the average or 0 if no data has been added
38 */
39 public double getAverage()
40 {
41 if (count == 0) return 0;
42 else return sum / count;
43 }
44
45 /**
46 Gets the largest of the added data.
47 @return the maximum or 0 if no data has been added
48 */
49 public Object getMaximum()
50 {
51 return maximum;
52 }
53 }