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