In: Computer Science
Consider the following incomplete declaration of a Name class for 2a-2c.
public class Name something missing
{
private String first;
private String last;
public Name(String firstName, String lastName)
{
first = firstName;
last = lastName;
}
//additional methods
}
2a) In the first two parts, you will modify the Name class so that it implements the Comparable interface. Show here what should go in place of something missing in the first line of the class declaration.
2b) In this part, implement the compareTo method. The standard ordering of names is to be used: alphabetical order by last name, and if the two last names are the same, alphabetical order by first name. Two names are considered the same if and only if both the first and last names are the same.
2c) Implement a toString method in the Name class, which takes no parameters and returns a String containing the full name: first name then last name. For example, if the first name is “John” and the last name is “Doe”, then toString should return “John Doe”.
1. If wnats to implement Comparable Interface then something missing should be replaced with
Comparable<Name>
and needs to implement method compareTo(Name name) method
Full Program code as below:
Create a java class with name Name.java
import java.util.ArrayList;
import java.util.Collections;
public class Name implements Comparable<Name> {
private String first;
private String last;
public Name(String firstName, String lastName) {
this.first = firstName;
this.last = lastName;
}
/*
In this part, implement the compareTo method. The standard ordering
of names is to be used:
alphabetical order by last name, and if the two last names are the
same, alphabetical order by first name.
Two names are considered the same if and only if both the first and
last names are the same.
*/
@Override
public int compareTo(Name name) {
/*
* Sorting by last name. compareTo should return < 0 if
this(keyword)
* is supposed to be less than name, > 0 if this is supposed to
be
* greater than object name and 0 if they are supposed to be
equal.
*/
int index = this.last.compareTo(name.last);
//Sorting by first name if last name is same d
return index == 0 ? this.first.compareTo(name.first) : index;
}
/*
Implement a toString method in the Name class,
which takes no parameters and returns a String containing
the full name: first name then last name. For example,
if the first name is “John” and the last name is “Doe”,
then toString should return “John Doe”.
*/
@Override
public String toString() {
return this.first + " " + this.last;
}
//driver method
public static void main(String[] args) {
//create arraylist to check output of the program
ArrayList<Name> list = new ArrayList<Name>();
//add firstName and lastName
list.add(new Name("Henry", "Miller"));
list.add(new Name("Nalo", "Hopkinson"));
list.add(new Name("Frank", "Miller"));
list.add(new Name("Deborah", "Hopkinson"));
list.add(new Name("George", "Martin"));
//needs to sort arraylist using Collection.sort(Object o)
Collections.sort(list);
//print the arraylist after sorting
for (Name name : list) {
System.out.println(name.toString());
}
}
}
__________________________________________________________________________________________
OUTPUT
Deborah Hopkinson
Nalo Hopkinson
George Martin
Frank Miller
Henry Miller
___________________________________________________________________________________________