Table of Contents
This section presents two ordinary statistical algorithms: mean and standard deviation. In one sense, this section could be treated as optional. Instead of having the simulator present useful summary statistics, raw data can be put into a spreadsheet for creating the statistics.
However, the process of designing these statistical processing classes is very interesting. This chapter examines some ways to add features to the built-in collection classes.
Currently, the Simulator class collects
two Lists. One has the length of a
session, the other has the maximum stake during a session. We need to
create some descriptive statistics to summarize these stakes and
session lengths.
We will design a Statistics class with
responsibility to retain and summarize a list of numbers and produce
the average (also known as the mean) and standard deviation. The
Simulator can then use this this
Statistics class to get an average of the
maximum stakes from the list of session-level measures. The
Simulator can also apply this
Statistics class to the list of session
durations to see an average length of game play before going
broke.
We can encapsulate this statistical processing in two ways: we
could extend an existing class or we could delegate the statistical
functions to a separate class. If we extend the library
java.util.List, we can add the needed
statistical summary features; given this new class, we can replace the
original List of sample values with a
StatisticalList that both saves the values and
computes descriptive statistics. In addition to delegation, we have
two choices for implementation of this extended version of
List, giving us three alternative
designs.
Extend LinkedList. In this case, we
are simply adding two new methods to the existing
LinkedList implementation. However, our new
methods would not apply to ArrayList or
Vector.
Extend AbstractList. However, in
doing this we would need to provide the implementation details of
the list structure itself. In effect, we would reimplement
LinkedList or
ArrayList.
Create a separate class that computes statistics on a
List given as an argument to the
methods of the statistical class. In this case, our statistical
methods will work with any subclass that implements the interface
List. This can be applied to any of
LinkedList,
ArrayList or
Vector.
Alternative three -- a separate statistical class -- has the
most reuse potential. We'll create a simple class with two methods:
mean and stdev to
compute the mean and standard deviation of a
List of Integer
objects. This can be used in a variety of contexts, and allows us the
freedom to switch List implementation
classes without any other changes.
The detailed algorithms for mean and standard deviation are provided in Statistical Algorithms.
Since our processes are relative simple, stateless algorithms,
we don't need any instance variables. Consequently, we'll never need
to create an instance of this class. As with
Java.lang.Math, all of these methods can be
declared as static, making them features of the class itself.