Given the class?

Given the class?

public class Name implements Comparable<Name> {

String first, last;

public Name(String first, String last) {

this.first = first;

this.last = last;

}

public int compareTo (Name n) {

int cmpLast = last.compareTo(n.last);

return cmpLast != 0 ? cmpLast : first.compareTo(n.first);

}

public String toString() {

return first + ” ” + last;

}

}

and the code fragment:

ArrayList<Name> list = new ArrayList<Name>();

list.add (new Name(“Joe”,”Shmoe”));

list.add (new Name(“John”,”Doe”));

list.add (new Name(“Jane”,”Doe”));

Collections.sort(list);

for (Name n : list) {

System.out.println(n);

}

What is the result?

Given the class?

public class Name implements Comparable<Name> {

String first, last;

public Name(String first, String last) {

this.first = first;

this.last = last;

}

public int compareTo (Name n) {

int cmpLast = last.compareTo(n.last);

return cmpLast != 0 ? cmpLast : first.compareTo(n.first);

}

public String toString() {

return first + ” ” + last;

}

}

and the code fragment:

ArrayList<Name> list = new ArrayList<Name>();

list.add (new Name(“Joe”,”Shmoe”));

list.add (new Name(“John”,”Doe”));

list.add (new Name(“Jane”,”Doe”));

Collections.sort(list);

for (Name n : list) {

System.out.println(n);

}

What is the result?

A.
Jane Doe
John Doe
Joe Shmoe

B.
John Doe
Jane Doe
Joe Shmoe

C.
Joe Shmoe
John Doe
Jane Doe

D.
Joe Shmoe
Jane Doe
John Doe

E.
Jane Doe
Joe Shmoe
John Doe

F.
John Doe
Joe Shmoe
Jane Doe

Explanation:
The list will be sortedalpabethically.
Output will be:
Jane Doe
John Doe
Joe Shmoe



Leave a Reply 1

Your email address will not be published. Required fields are marked *