Comparable Interface

Whenever we want to sort a collection of data, we need a way of being able to compare any two pieces of items from that collection. If we are sorting in ascending order, then smaller items should be before larger items, and vice-versa when sorting in descending order.

In some programming languages (e.g., Python), we can use the < operator to determine if one item is smaller than another. In Java, this operator only works for numbers and not for Strings or other types of objects.

Instead, we can define how to compare an object of a class with other data in Java using the Comparable<T> interface. This interface asks the class to define a special method public int compareTo(T other) that:

  • returns a negative number if this is less than other
  • returns 0 if this is equal to other
  • returns a positive number if this is greater than other

The method should be consistent so that if the roles of other and this are flipped (by calling the compareTo method on the variable storing the other object), the opposite values are returned (or 0 if they are equal).

Comparing Two Contacts

Recall the Contact class we used in our application in Lab 3 that stored the name of a faculty or staff member on campus, along with their office location. In that application, we sorted the list of Contacts by the name of the person so that we could quickly find their locations using the binary search algorithm. Both sorting and search required us to first retrieve the name of the person from the Contact object, then use the compareTo method of the String class (that implements the Comparable interface!) so that we could compare it to another String (either another Contact’s name while sorting or a name given by the user to find in our list of Contacts).

An alternative approach that would have required less code would be to implement the Comparable interface within the Contact class – then we can directly compare two Contact objects (without having to first retrieve their names), and we can even use the Collections.sort method to sort a List of Contact objects (instead of implementing a sorting algorithm ourselves like we did with the Selection Sort algorithm in Lab 3).

In Visual Studio Code, open the Contact.java file. We have added some new code since Lab 3:

  1. The Contact class now implements Comparable<Contact> at the top of the file
  2. We have provided a skeleton of the public int compareTo(Contact other) method required by the Comparable interface
  3. We have provided a public static void main(String[] args) function with code for investigating how the compareTo method might be used.

Your first task in this warmup exercise is to fill in the contents of the compareTo method. Similar to Lab 3, the method should:

  • return a negative number if this.name is earlier in the alphabet than other.name
  • return 0 if this.name is the same as other.name
  • return a positive number if this.name is later in the alphabet than other.name

Hint

You could consider using the String class’s compareTo method here to implement your solution.

To test whether your solution has worked, you can uncomment the first six lines of actual code in the main function (by deleting the first two comment lines that include [REMOVE THIS LINE TO TEST]). You should check whether negative, 0, or positive numbers are printed for the three print statements for the two Contact objects.

Your second task in this warmup exercise is to predict what will happen if you uncomment the remaining code in the main function (by deleting the last two comment lines that include [REMOVE THIS LINE TO TEST]). In particular, you should predict what you think will be printed and write your prediction in a new text file called contact_prediction.txt. Then run the code and see if your prediction came true. If not, try to think about what caused the result instead.

Don’t forget to add/commit/push your changes to the Contact.java and contact_prediction.txt files.