Question: Does the hashCode method in class Robot below obey the equals/hashCode contract? If yes, what is bad about the hashCode method? If no, why does
Does the hashCode method in class Robot below obey the equals/hashCode contract?
If yes, what is bad about the hashCode method?
If no, why does the hashCode method violate the contract?
Also in about 1 sentence, describe an easy way to write a hashCode method for almost any class
class Robot
{
private int serialNumber;
private String name;
public boolean equals(Object x)
{
Robot that = (Robot) x;
return this.serialNumber == that.serialNumber && this.name.equals(that.name);
}
public int hashCode()
{ return 1234; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
