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