X1041: Complete compareTo() 2

The Person class below implements the Comparable interface. You must complete the compareTo method.

  public class Person implements Comparable<Person> {
    // ...
    public Person (String f, String l) { ... }
    public String getFirst() { ... }
    public String getLast() { ... }
    @Override
    public String toString() { return getLast()+", "+getFirst(); }
    public int compareTo(Person other) {
      // to be completed
    }
  }

Complete the compareTo() method in the class above as described below. The method takes as a parameter another Person, named other.

  • If other is null, return -1.
  • If the last name in this object is less than the last name in other, return -1.
  • If the last name in this object is greater than the last name in other, return 1.
  • If the last name in this object is equal to the last name in other, then:
    • if first name in this object is less than the first name in other, return -1.
    • if first name in this object is equal to the first name in other, return 0.
    • if first name in this object is greater than the first name in other, return 1.

Your Answer:

Feedback

Your feedback will appear here when you check your answer.