Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Write an equals method and compareTo method as described public class Name imp

ID: 3700174 • Letter: #

Question

//Write an equals method and compareTo method as described

public class Name implements Comparable<Name> {

   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

Explanation / Answer

Name.java

public class Name implements Comparable<Name> {

private String first;

private String last;

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

@Override

public boolean equals(Object other) {

if (this == other)

return true;

if (other == null)

return false;

if (getClass() != other.getClass())

return false;

Name obj = (Name) other;

if (first == null) {

if (obj.first != null)

return false;

} else if (!first.equals(obj.first))

return false;

if (last == null) {

if (obj.last != null)

return false;

} else if (!last.equals(obj.last))

return false;

return true;

}

@Override

public int compareTo(Name other) {

if(this.last.compareTo(other.getLast())<0)

{

return -1;

}

else if(this.last.equalsIgnoreCase(other.getLast()))

{

if(this.first.compareTo(other.getFirst())<0)

{

return -1;

}

else if(this.first.compareTo(other.getFirst())>0)

{

return 1;

}

else

return 0;

}

else

return 1;

}

  

  

}

_____________________

Test.java

public class Test {

public static void main(String[] args) {

  

Name n1=new Name("Jane", "Jones");

Name n2=new Name("Amy", "Smith");

Name n3=new Name("Beth", "Jones");

if(n1.compareTo(n2)==-1)

{

System.out.println(n1+" comes before "+n2);

}

else if(n1.compareTo(n2)==1)

{

System.out.println(n1+" comes after "+n2);

}

else

{

System.out.println(n1+" and "+n2+" are same");

}

if(n3.compareTo(n1)==-1)

{

System.out.println(n3+" comes before "+n1);

}

else if(n3.compareTo(n1)==1)

{

System.out.println(n3+" comes after "+n1);

}

else

{

System.out.println(n3+" and "+n1+" are same");

}

}

}

____________________

Output:

Jane Jones comes before Amy Smith
Beth Jones comes before Jane Jones

_____________Thank You