//An interface used to compare two objects. //Must implement method compareTo() import java.lang.Comparable; /** * Class Name is used to store a first and last name * * @author William McDaniel Albritton */ public class Name implements Comparable { // data fields. // "protected" means that these data fields are visible in the subclass. // "protected" also means that these data fields are NOT visible outside the class // "protected" is halfway between "private" & "public". protected String first; protected String last; /** * Stores the first and last name of a person * NOTE: The Constructor is Used To Create EAch Object & Initialize DAta Fields. * * @param firstName is the first name. * @param lastName is the last name. */ public Name(String firstName, String lastName) { // This Code Initializes The Data Fields. // Syntax: dataField = parameter; first = firstName; last = lastName; } /** * Returns a String in format: LastName, FirstName * NOTE: A toString() method is Used to return a String with The Data Stored In EAch Object's DAta Field. * * @return a String in format: LastName, FirstName */ public String toString() { // Create a Local VAriable. String fullName = last + ", " + first; // Return the Local Variable. return fullName; } /** * Used to Display The Initials. * * @return The Initials For Someone's Name. */ public String initials() { // Create a Local VAriable For Initials. String theInitials = first.substring(0, 1) + ". " + last.substring(0, 1) + "."; return theInitials; } /** * This Is An "Accessor" Method - Used To Get A Data Field. * * @return the First Name */ public String getFirstName() { // Return the Data Field. return first; } /** * This Is An "Accessor" Method - Used To Get A Data Field. * * @return the Last Name */ public String getLastName() { // Return the Data Field. return last; } /** * This Is A "Mutator" Method - Used To Set A Data Field. * * @param firstNameParameter is the first name. */ public void setFirstName(String firstNameParameter) { // SEt the Data Field. first = firstNameParameter; } /** * This Is A "Mutator" Method - Used To Set A Data Field. * * @param lastNameParameter is the last name. */ public void setLastName(String lastNameParameter) { // set the Data Field. last = lastNameParameter; } /** * Compares the last name of two Name objects. * If the last name is the same, then it compares the first name of two Name objects. * * @param object2 is the second Name object to be compared (object #2). * @return If the two names are equal, a zero is returned. * If Name object #1 comes before Name object #2 alphabetically, * a negative integer is returned. * If Name object #1 comes before Name object #2 alphabetically, * a positive integer is returned. * @exception ClassCastException if object #2 is not class Name, then throws an exception */ public int compareTo(Object object2) throws ClassCastException { // get the string for last name to be compared (from this class - object #1) String lastName1 = last; // The parameter should be a Name, so we will cast it, // so that the compiler will let us call Name methods Name name2 = (Name) object2; // get the string for the 1st name to be compared (from the parameter - object #2) String lastName2 = name2.getLastName(); // use the compareTo method of String class int result = lastName1.compareTo(lastName2); // if the last names are the same, compare the first names if(result == 0){ result = first.compareTo(name2.getFirstName()); } return result; } }// End of Class.