Java Reference
In-Depth Information
public class TestSort {
static double[] testData = {
0.3, 1.3e-2, 7.9, 3.17
};
public static void main(String[] args) {
SortDouble bsort = new SimpleSortDouble();
SortMetrics metrics = bsort.sort(testData);
System.out.println("Metrics: " + metrics);
for (int i = 0; i < testData.length; i++)
System.out.println("\t" + testData[i]);
}
}
The
main
method shows how code that drives a test works: It creates
an object of a class extended from
SortDouble
, provides it with the data
to be sorted, and invokes
sort
. The
sort
method stores the data, initial-
izes the metrics, and then invokes the abstract method
doSort
. Each ex-
tended class implements
doSort
to do its sorting, invoking
getdataLength
,
compare
, and
swap
when it needs to. When
doSort
returns, the counts re-
flect the number of each operation performed. To test a different al-
gorithm, you can simply change the class name after the
new
. Here is
what one run of
TestSort
looks like:
Metrics: 0 probes 6 compares 2 swaps
0.013
0.3
3.17
7.9
Now with these classes as examples, let us return to the issue of design-
ing a class to be extended. We carefully designed the protected interface
of
SortDouble
to allow extended classes more intimate access to the data
in the object but only to things we
want
them to manipulate. The access
for each part of the class design has been carefully chosen: