Use only some Instance Variables

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

See Below

Use only some Instance Variables

The compareTo() method takes a reference to another Monster as a parameter.

class Monster implements Comparable<Monster>
{

  . . . 
  
  public int compareTo( Monster other )
  {
  
     .... more goes here ...
  }
}

Typically, only some of the instance variables of a complex object are used by compareTo(). Say that you want monsters to be listed in ascending order of hit points. Monsters with the same number of hit points are ordered by ascending strength. Here is a partially completed method that does that:

  public int compareTo( Monster other )
  {
    int hitDifference = getHitPoints()-other.getHitPoints();
    
    if (  )
      return hitDifference;
      
    else
      return  ;
      
  }

It is easy to get this backwards. What you want is for compareTo(Monster other) to return a negative int when the monster that owns the method is less than the other monster.

QUESTION 20:

Fill in the blanks.