JAVA program about compareTo methodConsoder the following incomplete declaration of a Name class.public class Name something missing{private String first;private String last;public Name(String firstName,String lastName){first=firstName;last=lastName;

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/16 02:34:34
JAVAprogramaboutcompareTomethodConsoderthefollowingincompletedeclarationofaNameclass.publicclassName

JAVA program about compareTo methodConsoder the following incomplete declaration of a Name class.public class Name something missing{private String first;private String last;public Name(String firstName,String lastName){first=firstName;last=lastName;
JAVA program about compareTo method
Consoder the following incomplete declaration of a Name class.
public class Name something missing
{
private String first;
private String last;
public Name(String firstName,String lastName)
{
first=firstName;
last=lastName;
}
//additional methods
}
a 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.
b In this part implement the compareTo method.The standard ordering of names is to be used:alphabetical order by last name,and if 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.
c Implement a toString method in the Name class,which takes no parameters and returns a String containing the full name:first name then the last name.For example,if the first name is "John " and the last name is "Doe",then toString should return "John Doe"

JAVA program about compareTo methodConsoder the following incomplete declaration of a Name class.public class Name something missing{private String first;private String last;public Name(String firstName,String lastName){first=firstName;last=lastName;
well, you should do your own homework, shouldn't you?
to get you started, take a look at interface java.lang.Comparable
task a: let you understand how to implement an interface.
public class Name implements java.lang.Comparable
task b: in task a, you declared class Name is going to implement the Comparable interface, so you need to really implement the method declared in the interface:
// this is method to implement: compare two Name instances.
// take a look at the contract in the Java API doc.
public int Comparable(Name anotherName) {
if(anotherName == null) {
return 1;
}
if(this.last.compareTo(anotherName.getLast()) > 0) {
return 1;
}
if(this.last.compareTo(anotherName.getLast()) < 0) {
return -1;
}
// last names are equal, compare first names
if(this.last.compareTo(anotherName.getLast()) == 0) {
if(this.first.compareTo(anotherName.getFirst()) > 0) {
return 1;
}
if(this.first.compareTo(anotherName.getFirst()) < 0) {
return -1;
}
}
// they equal
return 0;
}
task c: overriding toString() from java.lang.Object
public String toString() {
return this.first + " " + this.last;
}