1

According to this answer, roughly, if we had a Classroom object array of student objects, class[index] != student1. I believe this is the mistake I am making in implementing my equals method to compare the array[index] object to another object. I believed the array[index] and the object I am comparing against to be the same.

The code below shows my getNumStudents method in which I try to count the number of times a student id shows up in a class. ID represents brand shoes he or she likes (practice exercise out of lecture). This method is in my classroom object class which implements an interface.

@Override
public int getNumStudents(T anEntry) {
    int count = 0;
    for (int index = 0; index < numberOfEntries; index++) {

       if (roster[index].equals(anEntry)) )
        {
            counter++;
        } 
    } 

    return count;
}

My equals method is as such and is implemented in the student class:

public boolean equals(Student student) {
    if (this == student)
    {
        return true;
    }
    if (student == null)
    {
        return false;
    }
    if (this.getID() != student.getID())
    {
        return false;
    }

    return true;
}

I don't know if I properly did the hashCode override but here it is (in Student class):

   @Override
    public int hashCode() {
    int result = 17;
    result = 31 * result + studentID;
    return result;
  }

I've narrowed down where the bug is to most likely here:

   if (roster[index].equals(anEntry)) )

specifically

roster[index].equals(anEntry))

What should I call or how should I adjust my getNumStudents(T anEntry) method to properly return the number of students with a certain ID (representing a shoe type) within a Classroom object array?

3
  • Why dont you just check roster[index].getId() == anEntry.getId() if getId is int. Commented Sep 17, 2018 at 16:03
  • @AshraffAliWahab I didn't post the entire code but its because i'm working with generics in my Classroom class Commented Sep 17, 2018 at 16:12
  • @AshraffAliWahab Also, roster[index] and anEntry are two different things according to my IDE. Not the same types. Commented Sep 17, 2018 at 16:18

1 Answer 1

2

Your equals signature is wrong.

The correct signature of equals method must be as follows.

public boolean equals(Object other)

Then inside the method you should check if it is of comparable type and if you really need it to be of type Student, you have to check for this and return false otherwise.

In your case that would be a minimal change required for your implementation:

public boolean equals(Object other)
{
    if (this == other)
    {
        return true;
    }

    // This also works if `other` is `null`
    if (!(other instanceof Student))
    {
        return false;
    }

    // Now we cast it to `Student`
    final Student student = (Student) other;

    if (this.getID() != student.getID())
    {
        return false;
    }

    return true;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why is it public boolean equals(Object obj)? I actually ran tests with equals inside my main using two student classes and it responded correctly. Does Object obj basically allow me to handle my array[index] references in the getNumStudents method?
Wow thanks, it actually works! Can you explain the code below 'we cast it to 'Student'?
@mobcityzkore Not sure what to explain. Before this line you have a generic Object, so you cannot call Student methods on it. In order for it to act as Student you have to cast it. It is unsafe to cast it before the call to instanceof as it can throw ClassCastException, and by equals contract it MUST accept any kind of object (e.g. Teacher), and including null, and return true or false without throwing exceptions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.