public class Name
{
private String firstName;
private String lastName;
public Name(String first, String last) //constructor
{
firstName = first;
lastName = last;
}
public String toString()
{ return firstName + " " + lastName; }
public boolean equals(Object obj)
{
Name n = (Name)obj;
return n.firstName.equals(firstName) &&
n.lastName.equals(lastName);
}
public int hashCode()
{ /* implementation not shown */ }
public int compareTo(Name n)
{
/* more code */
}
}
The compareTomethod implements the standard name-ordering algorithm where last names take precedence over first names. Lexicographic or dictionary ordering of Strings is used. For example, the name Scott Dentes comes before Nick Elser, and Adam Cooper comes before Sara Cooper.
Which of the following is a correct replacement for /* more code */?
I) int lastComp = lastName.compareTo(n.lastName);
if (lastComp != 0)
return lastComp;
else
return firstName.compareTo(n.firstName);
II) if (lastName.equals(n.lastName))
return firstName.compareTo(n.firstName);
else
return 0;
III) if (!(lastName.equals(n.lastName)))
return firstName.compareTo(n.firstName);
else
return lastName.compareTo(n.lastName);
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III