This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Detecting and rejecting duplicate grades using equals and hashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Detecting and rejecting duplicate grades using equals and hashCode" Watch "Detecting and rejecting duplicate grades using equals and hashCode" New topic
Author

Detecting and rejecting duplicate grades using equals and hashCode

pamela jones
Greenhorn

Joined: Apr 26, 2003
Posts: 5
I have written equals and hashCode methods to a class. It is suppose to detect and reject duplicate grades entered into the JPanel. When you press the View button it should show up only once but it shows up twice. I am bewildered. I have included the code I wrote. Please advise.
public boolean equals(Object o){
if (this == o){
return true;
}
if (! (o instanceof Gradeable)){
return false;
}

Gradeable other = (Gradeable) o;
return this.maxPoints == other.maxPoints
&& this.earnedPoints == other.earnedPoints
&& this.description.equals(other.description)
&& this.date.equals(other.date);
}

/**
* This method overrides the Object hashCode method.
* @return the hash code for the Object
*/
public int hashCode() {
int result = 17;

result = 37 * result + maxPoints;
result = 37 * result + earnedPoints;
result = 37 * result + description.hashCode();
result = 37 * result + date.hashCode();
return result;
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Your code here looks good. It's hard to say where the problem is; it's probly somewhere in the code you haven't shown here though. Try finding (or creating) some specific Gradeable objects which you believe should be equal, and insert a line of code to evaluate whether obj1.equals(obj2) and print the result. And print out the values of all the fields being compared. One way to do this is to just insert some code into the equals() method and look at the output:

Hopefully this will give some cluse about what the problem is.


"I'm not back." - Bill Harding, Twister
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Detecting and rejecting duplicate grades using equals and hashCode
 
It's not a secret anymore!