user1647102
user1647102

Reputation: 36

Implementing the Comparable Interface homework

I am a beginner at computer programming and I am working with Java. For my homework assignment I was instructed to create a contact book according to the following specifications:

  1. First, a contact is defined as the tuple: firstName, lastName, phoneNumber and email.

  2. You will create a class Contact that allows getting and setting of these variables as well as a toString() method and an equals() method. The class Contact should implement the Comparable interface.

  3. You will create a class ArrayOperation with a static method that sorts uni-dimensional array of objects that implement the Comparable interface

  4. Next, a ContactBook class should be able to search, create and produce a String with all the sorted Contacts.

  5. A main class (call it whatever you want) should offer a menu asking how many contacts to create and then offer the three options above.

  6. When adding, the input from the user is gathered and the method ContactBook.addContact(Contact c) will store that contact in memory.

  7. If the user is searching, the program asks the user for all of the contact information and using the equals method searches for the desired contact. The program quits when the user presses "q"

I am having trouble implementing the Comparable interface. This is what I have so far:

public class Contact implements Comparable
{
  private String firstName, lastName, phoneNumber, email;

  public void setFirstName(String fName){firstName = fName;}
  public void setLastName(String lName){lastName = lName;}
  public void setPhoneNumber(String num){phoneNumber = num;}
  public void setEmail(String email){this.email = email;}

  public String getFirstName(){return firstName;}
  public String getLastName(){return lastName;}
  public String getPhoneNumber(){return phoneNumber;}
  public String getEmail(){return email;}

  public String toString()
  {
    return "First Name: " + firstName +
           "\nLast Name: " + lastName +
           "\nPhone Number: " + phoneNumber +
           "\nEmail: " + email;
  }

  public boolean equals(Contact cont)
  {
    return this.firstName.equals(cont.firstName) &&
           this.lastName.equals(cont.lastName) &&  
           this.phoneNumber.equals(cont.phoneNumber) &&
           this.email.equals(cont.email);
  }

  public int compareTo(Contact cont)
  {
    if(this.firstName.equals(cont.firstName) &&
           this.lastName.equals(cont.lastName) &&  
           this.phoneNumber.equals(cont.phoneNumber) &&
           this.email.equals(cont.email))
      return 0;
    return 1;
  }
}

Upvotes: 0

Views: 854

Answers (2)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Your implementation of the compareTo doesn't make sense:

if(this.firstName.equals(cont.firstName) &&
       this.lastName.equals(cont.lastName) &&  
       this.phoneNumber.equals(cont.phoneNumber) &&
       this.email.equals(cont.email))
  return 0;
return 1;

Remember it returns an int (which could be -1, 0, or 1), not a boolean. Use .compareTo instead of .equals there also. .equals method is for equality check (which means either true or false), and not for comparison (which means less-than, equals-to, or greater-than).

The purpose of implementing the Comparable interface is so that you can sort the objects, that's not going to sort your objects properly.

Upvotes: 1

Reimeus
Reimeus

Reputation: 159754

You need to use a generic for Comparable in your class declaration to match the object that you are comparing in compareTo:

public class Contact implements Comparable<Contact> {

Also use String.compareTo() over String.equals() in your compareTo method. There are many examples of this.

Upvotes: 6

Related Questions