Java Reference
In-Depth Information
Display 13.6
Sorting Arrays of Comparable (part 2 of 2)
Sample Dialogue
Before Sorting
10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0,
After sorting:
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0,
Before sorting;
dog, cat, cornish game hen,
After sorting:
cat, cornish game hen, dog,
Self-Test Exercises
These exercises are for the material on the Comparable interface.
5. The method interchange in Display 13.5 makes no use of the fact that its
second argument is an array with base type Comparable . Suppose we change
the parameter type Comparable[] to Object[] and change the type of the
variable temp to Object . Would the program in Display 13.6 produce the
same dialogue?
6. Is the following a suitable implementation of the Comparable interface?
public class Double2 implements Comparable
{
private double value;
public Double2( double theValue)
{
value = theValue;
}
public int compareTo(Object other)
{
return -1;
}
public double doubleValue()
{
return value;
}
}
You can think of the underlying “comes before” relationship as saying that for
any objects d1 and d2 , d1 comes before d2 .
 
Search WWH ::




Custom Search