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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!