I need help creating an equals method.
The method is: boolean equals(Zombie other)
The description specifically says: "Accepts another Zombie object as an argument and returns true if the zombie has the same name and same degree of infection, weapon(s) and brains as the other Zombie."
I do not understand how I would separate the Zombie other to separate parts to compare to the instance variables then how I would compare all the pieces.
Asked
Active
Viewed 1,133 times
2

JessNicole27
- 63
- 1
- 7
1 Answers
3
This would be correct implementation of equals() in your case.
@Override
public boolean equals(Object obj) {
if (obj instanceof Zombie){
Zombie zombiObj= (Zombie) obj;
if(zombiObj.getName().equals(this.getName()) &&
zombiObj.getInfection().equals(this.getInfection())&&
zombiObj.getWeapon().equals(this.getWeapon()) &&
zombiObj.getBrain()== this.getBrain() ) {
return true;
}else{
return false;
}
} else{
return false;
}
}

Paul Samsotha
- 205,037
- 37
- 486
- 720

Dark Knight
- 8,218
- 4
- 39
- 58
-
@JessNicole27 , hope this solves your problem.. – Dark Knight Nov 11 '13 at 07:01
-
It did but all for one part, she wants us to compare the brains as well and it is an integer. jGrasp said I cant deference that so how would I do that – JessNicole27 Nov 11 '13 at 07:12
-
Refer to updated post, added brain equality condition for type as int. – Dark Knight Nov 11 '13 at 07:15
-
Please note that you also have to override hashCode if you override equals! (Two objects that are equal according to equals() must return the same hashCode) – isnot2bad Nov 11 '13 at 08:08
-
@isnot2bad : here we are comparing objects using it's contents. Here HashMap/ Map is not used. And hence implementing hashCode() here is not mandatory. Please correct me if i misunderstood your comment. – Dark Knight Nov 11 '13 at 08:10
-
@DarkKnight doesn't matter. Not overriding hashCode breaks the contract of java.lang.Object. It might have no effect right now, but it's still wrong. It's almost safe to say that some day someone WILL put zombies into a HashMap/-Set. Then please don't come back here and ask why it doesn't work/behave strange... ;). See also http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java – isnot2bad Nov 11 '13 at 08:16
-
@isnot2bad As current problem definition does not include reference to map's, haven't included hashcode(). Also to override hashcode() we need to have a clear idea about what all Zombie fields won't change and can be used here. Al tough i agree with your point. Thanks – Dark Knight Nov 11 '13 at 08:24
-
@DarkKnight You're right, changing fields are problematic. But only if the object is already inside a HashMap/-Set. The contract of hashCode does not prohibit to use fields that may change. Just don't put objects into a HashSet and then modify them. I suggest to simply override hashCode correctly (is no big deal and does no harm). – isnot2bad Nov 11 '13 at 09:01
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40937/discussion-between-isnot2bad-and-dark-knight) – isnot2bad Nov 11 '13 at 09:05
-
If other zombie has no weapon (`zombiObj.getWeapon()` returns null, results in NullPointerException – cdalxndr Feb 26 '21 at 09:15