aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode " Watch "hashcode " New topic
Author

hashcode

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
source: Whizlabs

What will be the most appropriate implementation of hashcode() method assuming equals implemented correctly.

Ans is


I thought that answer would be


Please explain why the given answer is correct. Thanks.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Is the difinition of the equals method not given in the question??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
NO it just says that equals method has been implemented correctly but definition is not given.
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
frankly, very much close to real exam question.

you need not include area to find the hashcode because area is itself calculated using length and breadth.

hope that clears your doubt


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
but area is declared final. That means,
if an object has length = 4 and breadth = 2
then area is calculated as 8.
Later on when this object is changed, lets say, length is changed to 3 , area will still remain 8 because it is final.

That does not mean this object with values (3,2,8) equal to object with values (3,2,6).
Please clarify where i am wrong.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

But how will length and breadth change. They are both private and there is no setter (mutator) to change their values...
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Just in case there are getters and setters then what will be the answer. I just want to know. Thanks
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Someone please clarify
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Well I don't think you should seek for an answer after changing a question. Each question is designed with a strategy. If there were getter and setters for length and width, then they might have provided definition of the equals method or might have removed final from area. So don't go in too much depth for a question. I think this question is too ambiguous. The real exam is not ambiguous at all. So don't worry
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Don't we have risk of loss of precision here in this line??



I think the reason for not choosing this may be that there is still chance of loss of precision of the hash code being calculated as you are using long in the expression and also casting the value back to int.

HTH


My Blog SCJP 5 SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode