Use the Person
class below. Note that this class does NOT implement the Comparable
interface.
public class Person { // ... public Person...
Use the Person
class below. Note that this class
does NOT implement the Comparable
interface.
public class Person {
// ...
public Person (String f, String l) { ... }
public int compareTo(Person o) { ... }
public String getFirst() { ... }
public String getLast() { ... }
@Override
public String toString() { return getLast()+", "+getFirst(); }
}
For this problem you must complete the following class.
public class CompareTwoLastNames implements Comparator<Person>
{
public int compare(Person o1, Person o2) {
// your code goes here
}
}
This class should compare properly people with two last name, like "Perez Quinones." The easiest way to accomplish this comparison is to remove any spaces in the names and then compare them as if they had a single word last name. So, "Perez Quinones" becomes "PerezQuinones". Note that there might be more than one space and in more than one location, that is not all spaces are contigous. All of these names should match with "Perez Quinones": "Perez Quinones", "Pe rez Quino nes".
Complete the compare()
method in the class above as
described below. The method takes as a parameter
two objects of type Person
named o1
and o2
.
o1
and o2
are both null, return 0.o1
is null, return -1.o2
is null, return 1.o1
is less than
the last name in o2
, return -1.o1
is greater than
the last name in o2
, return 1.o1
is equal to the
last name in o2
, return 0.Your feedback will appear here when you check your answer.