Comparable Interface
Objects that have an ordering are compared using the compareTo() method.
23. End of Chapter
Answer:
public int compareTo( Monster other )
{
int hitDifference = getHitPoints()-other.getHitPoints();
if ( hitDifference != 0 )
return hitDifference;
else
return getStrength()-other.getStrength();
}
End of Chapter
You have reached the end of the chapter. You may wish to search this list for something that interests you.
- The compareTo() compareTo() method.
- natural order natural order
- lexicographic order lexicographic order
- collating sequence collating sequence
- ordered array ordered array and the
sort()
method - sort(), objects
sort()
method with reference values. - binary search binary search