• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode() and equals() exam question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there, on a master exam there is this code



Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all that apply.)


the exam correction says that this would be a good answer:

return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate == this.code.length() * this.bal * this.rate;

but I don't agree since I could have two SortOf, one with bal = 2 and rate = 3 and the other one with bal = 3 and rate = 2.

The hashCode value would not be the same while they would be considered meaningfully equivalent by that implementation of the equals() method.

Am I right to disagree or am I missing something?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right; first of there s/b be a getter for code, bal and rate ( the access modifier is package). Assuming we have this then the code should be

public boolean equals(Object o)
{
if ( o instanceof SortOf )
{
SortOf t = (SortOf) o;
return ( t.getCode().length() == code.length() &&
t.getBal() == bal &&
t.getRate() == rate );
}
else
return false;
}
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually you don't need the getter / setter in your example, but I do agree that the test on instanceOf is prefferable (but again, not required for the contract).
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree with the above answer:



To fulfill the hashCode contract the hashCode() method should always produce the same result for the same object.
I don't think the contract says anything about effectiveness.
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not about effectiveness,

the contract says that if A.equals(B) then A.hashCode()==B.hashCode

Given
A.code.length()==B.code.length() // let's assume that it's 1
A.rate == 2
A.bal == 3
B.rate == 3
B.bal == 2

then A.equals(B) == true since 1*2*3 == 1*3*2

But A.hashCode() == 1*3
and B.hashCode() == 1*2

so A.hashCode() != B.hashCode()

which does not respect the contract
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an error in the master exam. It has been pointed out before too...
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic