Dictionary Entry

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:

The class must implement Comparable.

Dictionary Entry

Say that you are writing a program that acts like a dictionary. A dictionary entry has two instance variables: the word that it defines and the definiton. Here is a skeleton of Entry:

class Entry implements Comparable<Entry>
{
  private String word;
  private String definition;
  
  public Entry ( String word, String definition )
  {
    this.word = word;
    this.definition = definition;
  }
  
  public String getWord()
  {
    return word;
  }

  public String getDefinition()
  {
    return definition;
  }
  
  public String toString()
  {
    return getWord() + "/t" + getDefinition();
  }
  
  public int compareTo( Entry other )
  {
   return  ;
  }
  
}

A class that implements Comparable<T> must implement the method

int compareTo( T other )

where T is the type of the class. Since our Entry implements the Comparable<Entry> interface, it must implement the method

int compareTo( Entry other )

QUESTION 15:

When the entries of a dictionary are put in order, does the order depend on the definition?

Should the compareTo() method depend on the definiton?

Fill in the blank to complete the compareTo() method.