Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:44:47+00:00 2026-06-14T05:44:47+00:00

I am a beginner at computer programming and I am working with Java. For

  • 0

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;
  }
}
  • 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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:44:49+00:00Added an answer on June 14, 2026 at 5:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here is my first ultra beginner computer programming question in C. I need
I'm a beginner in game development and game programming. I have experience in computer
I am a beginner in programming Java Desktop Application interacting with databases. My goal
Level: Beginner I'm doing my first steps in Object Oriented programming. The code is
I'm a beginner in java programming. Now I'm doing a game called mastermind. It
I'm a total beginner when it comes to computer languages, and was asked for
Well, I'm a beginner, it's my year as a computer science major. I'm trying
Beginner here trying to get a pipeline working in bash. If somebody can see
Beginner in Android development. My code crashes. I have made a simple Java method
Beginner help needed :) I am doign an example form a php book which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.