Question
//Write an equals method and compareTo method as described public class Name implements Comparable { private String first; // First name private String last; //
//Write an equals method and compareTo method as described
public class Name implements Comparable
private String first; // First name
private String last; // Last name
public Name() {
this("", "");
} // end default constructor
public Name(String firstName, String lastName) {
first = firstName;
last = lastName;
} // end constructor
public void setName(String firstName, String lastName) {
setFirst(firstName);
setLast(lastName);
} // end setName
public String getName() {
return toString();
} // end getName
public void setFirst(String firstName) {
first = firstName;
} // end setFirst
public String getFirst() {
return first;
} // end getFirst
public void setLast(String lastName) {
last = lastName;
} // end setLast
public String getLast() {
return last;
} // end getLast
public void giveLastNameTo(Name aName) {
aName.setLast(last);
} // end giveLastNameTo
public String toString() {
return first + " " + last;
} // end toString
// TODO: Implement the equals() method. Recall: the equals() method
// should have Object as its parameter type (why?).
// equals() should test if the parameter is the correct type of object
// using the instanceof operator.
// If the object is the correct type, then equals() should check that
// the data matches. Note that the object will need to be cast as
// Name in order to access the data.
public boolean equals(Object other) {
return false;
}
// TODO: Implement the compareTo method by comparing last names. If
// last names are equal, then compare first names. So,
// "Jane Jones" should be before "Amy Smith" (because Jones is before
// Smith).
// "Beth Jones" should be before "Jane Jones" (because Beth is before Jane).
// You are writing the compareTo method for NAMES. You should make use of
// the
// String class compareTo method.
// As with any compareTo method, it should return:
// a negative integer if this is "smaller than" (or comes before) other
// a positive integer if this is "greater than" (or comes after) other
// zero if this is equal to other
public int compareTo(Name other) {
return 0;
} // end compareTo
} // end Name
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started