Question
I need help with this java project. Member class: Modification 1: Make the class implement the Comparable interface. Modification 2: Provide an implementation for the
I need help with this java project.
Member class:
Modification 1: Make the class implement the Comparable
Modification 2: Provide an implementation for the compareTo method such that it compares the value of the lastName instance variable of the two objects. If the lastName variable of the calling object is greater, it returns a positive number, if its smaller it returns a negative number, and if they both have the same value, the method then compares the first name. If they have the same value for the firstName variable, it returns 0, if the firstName variable of the calling object is greater, it returns a positive number, else it returns a negative number.
Notes:
Use the compareTo method of the String class when comparing the lastName and firstName fields.
Include the javadoc comment for the method.
Member class modifications.
getMemberInfo method in the Library class that has a String parameter.
The following is the method signature:
public int compareTo(Member member)
{
// provide implementation
}
Code need to modification:
public class Member { // instance variables private int id; private String firstName; private String lastName; private String phone;
public Member(int id, String firstName, String lastName, String phone) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.phone = phone; } public Member(Member memberObj)
{ if( memberObj != null ) { id = memberObj.id; firstName = memberObj.firstName; lastName = memberObj.lastName; phone = memberObj.phone; } }
public int getID() { return id; }
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String getPhone()
{ return phone; }
/** * The setID method stores a value in the id field. * @param id the value to store in id. */ public void setID(int id) { this.id = id; }
/** * The setFirstName method stores a value in the firstName field. * @param firstName the value to store in firstName. */ public void setFirstName(String firstName) { this.firstName = firstName; }
/** * The setLastName method stores a value in the lastName field. * @param lastName the value to store in lastName. */ public void setLastName(String lastName) { this.lastName = lastName; }
/** * The setPhone method stores a value in the phone field. * @param phone the value to store in phone. */ public void setPhone(String phone) { this.phone = phone; }
@Override public String toString() { // create a string representing the object String output = String.format(" %5s %-24s %s %5s %-24s %s %5s %-24s %s %5s %-24s %s", "", "Member ID:", id, "", "First Name:", firstName, "", "Last Name:", lastName, "", "Phone:", phone); return output; }
@Override public boolean equals(Object obj) { if( !(obj instanceof Member)) return false; Member member = (Member) obj; return firstName.equals(member.firstName) && lastName.equals(member.lastName) && phone.equals(member.phone); }
}
Library class
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