Question: X 1 0 4 1 : Complete compareTo ( ) 2 The Person class below implements the Comparable interface. You must complete the compareTo method.

X1041: Complete compareTo()2
The Person class below implements the Comparable interface. You must complete the compareTo method.
public class Person implements Comparable {
//...
public Person (String f, String l){...}
public int compareTo(Person o){...}
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:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!