Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

ISBN: 0764532545, 978-0764532542

More Books

Students also viewed these Databases questions

Question

Define exporting and importing.

Answered: 1 week ago

Question

What do you think of the MBO program developed by Drucker?

Answered: 1 week ago