| Author |
Doubt on a K & B LearnKey CD question
|
Jisha Anand
Ranch Hand
Joined: Apr 02, 2009
Posts: 62
|
|
Dear All,
This is a question from LearnKey CD provided along with K & B SCJP Guide:
Given:
Which of the following fulfill the equals() & hashCode() contract for this class? (Choose all that apply)
Correct Answer given are:
return ((SortOf)o).code.length() * ((SortOf)o).bal == this.code.length() * this.bal;
return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate == this.code.length() * this.bal * this.rate;
The explanation given is that the equals method must be atleast as precise as the hashCode() method.
The first option ofcourse is correct; but I don't understand how the second option is correct. I mean doesn't that break the hashCode - equals contract for cases in which code.length() of both objects compared are equal & the values of bal & rate for both are interchanged? In such cases this.hashCode() != o.hashCode(), but this.equals(o) will be true right? Confused!
Please help!
|
SCJP, SCWCD, SCBCD
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Please read http://www.coderanch.com/t/253306/Programmer-Certification-SCJP/certification/equls-hashCode-MasterExam-possible-error
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jisha Anand
Ranch Hand
Joined: Apr 02, 2009
Posts: 62
|
|
Thanks Rob!
Good to know I also am not on the wrong side
There are more errors in the CD questions, which I don't know whom to mail/message to; but this one I thought it's something which I overlooked.
Thanks again!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
SCJP questions and errors are usually discussed on another forum. Moving.
|
 |
 |
|
|
subject: Doubt on a K & B LearnKey CD question
|
|
|