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