Java Reference
In-Depth Information
Listing 17-33. A ComparablePerson Class That Implements the Comparable Interface
// ComparablePerson.java
package com.jdojo.interfaces;
public class ComparablePerson implements Comparable<ComparablePerson> {
private String firstName;
private String lastName;
public ComparablePerson(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
// Compares two persons based on their last names. If last names are
// the same, use first names
public int compareTo(ComparablePerson anotherPerson) {
int diff = getLastName().compareTo(anotherPerson.getLastName());
if (diff == 0) {
diff = getFirstName().compareTo(anotherPerson.getFirstName());
}
return diff;
}
public String toString() {
return getLastName() + ", " + getFirstName();
}
}
Listing 17-34 contains the code that tests the ComparablePerson class by sorting their objects in an array. The
output shows that objects of the ComparablePerson class are sorted by last name and first name.
 
Search WWH ::




Custom Search