Answer:
public int compareTo( Monster other )
{
int hitDifference = getHitPoints()-other.getHitPoints();
if ( hitDifference != 0 )
return hitDifference;
else
return getStrength()-other.getStrength();
}
End of Chapter
For a tiny (and somewhat silly) game that uses the Monster class,
see the exercises.
You have reached the end of the chapter. You may wish to extend your knowledge by examining the following.
- The compareTo() method.
- natural order
- lexicographic order
- collating sequence
-
ordered array
and the
sort()method -
sort()method with reference values. - binary search