简体   繁体   English

实施可比接口作业

[英]Implementing the Comparable Interface homework

I am a beginner at computer programming and I am working with Java. 我是计算机编程的初学者,并且正在使用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. 您将创建一个Contact类,它允许获取和设置这些变量以及toString()方法和equals()方法。 The class Contact should implement the Comparable interface. Contact类应实现Comparable接口。

  3. You will create a class ArrayOperation with a static method that sorts uni-dimensional array of objects that implement the Comparable interface 您将使用静态方法创建一个ArrayOperation类,该方法对实现Comparable接口的对象的一维数组进行排序

  4. Next, a ContactBook class should be able to search, create and produce a String with all the sorted Contacts. 接下来,ContactBook类应该能够搜索,创建和生成具有所有排序后的Contacts的String。

  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. 添加时,将收集来自用户的输入,并且ContactBook.addContact(Contact c)方法将将该联系人存储在内存中。

  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. 如果用户正在搜索,程序将向用户询问所有联系人信息,并使用equals方法搜索所需的联系人。 The program quits when the user presses "q" 当用户按下“ q”时,程序退出

I am having trouble implementing the Comparable interface. 我在实现Comparable接口时遇到麻烦。 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. 另外,坦率地说,我不知道如何使用compareTo()方法。 I want to compare two instances of my Contact class but I am unable to use "this.Contact" in the compareTo method. 我想比较我的Contact类的两个实例,但是我不能在compareTo方法中使用“ this.Contact”。
  • Finally, I am confused about what to compare when returning -1 and 1 for the compareTo() method. 最后,我对为compareTo()方法返回-1和1时要比较的内容感到困惑。

You need to use a generic for Comparable in your class declaration to match the object that you are comparing in compareTo : 您需要在类声明中使用泛型Comparable来匹配在compareTo中进行比较的对象:

public class Contact implements Comparable<Contact> {

Also use String.compareTo() over String.equals() in your compareTo method. 还可以在compareTo方法中在String.equals()使用String.compareTo() There are many examples of this. 很多这样的例子

Your implementation of the compareTo doesn't make sense: 您对compareTo实现没有任何意义:

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 . 请记住,它返回一个int (可以是-1、0或1),而不是boolean Use .compareTo instead of .equals there also. 也使用.compareTo而不是.equals .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). .equals方法用于相等性检查(表示真或假),而不用于比较(表示小于,等于或大于)。

The purpose of implementing the Comparable interface is so that you can sort the objects, that's not going to sort your objects properly. 实现Comparable接口的目的是使您可以对对象进行排序,而不会对对象进行正确的排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM