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:
First, a contact is defined as the tuple: firstName, lastName, phoneNumber and email.
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.
You will create a class ArrayOperation with a static method that sorts uni-dimensional array of objects that implement the Comparable interface
Next, a ContactBook class should be able to search, create and produce a String with all the sorted Contacts.
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.
When adding, the input from the user is gathered and the method ContactBook.addContact(Contact c) will store that contact in memory.
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;
}
}
- Every time I compile the code, the compiler shows an error that says my class is not abstract even though it shouldn't have to be abstract.
- Also, I frankly do not know what to do with the compareTo() method. I want to compare two instances of my Contact class but I am unable to use "this.Contact" in the compareTo method.
- Finally, I am confused about what to compare when returning -1 and 1 for the compareTo() method.