File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from Mock Test about 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 "A question from Mock Test about hashCode" Watch "A question from Mock Test about hashCode" New topic
Author

A question from Mock Test about hashCode

Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
Please see the question below:

class SortOf{
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length()*bal);
}
public boolean equals(Object o) {
//insert code here
}
}

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

A. return ((SortOf)o).bal == this.bal;
B. return ((SortOf)o).code.length() == this.code.length();
C. return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length()*this.bal;
D. return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate == this.code.length()*this.bal*this.rate;



The correct answer is C and D.

Why D is correct?
I don't thnk that it can guarantee the contract between equal and hashCode.
jimmy halim
Greenhorn

Joined: Nov 28, 2006
Posts: 15
Because option D isn't violate the equals or hashcode contract.
It's not optimal but not violating the contract

with

You will get a lot 2 Object that not equal but has same hashcode, although it's maybe not optimal but it's not violating the contracts
Because every equals object will generate same hashcode return.

So creating stricter (more variable involved) equals than hascode will not violating the contract. But if you created stricter hashcode than equals you violating the contract :
"If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result."

I hope I explain it clearly
Pardon me if there're something wrong with my grammar or vocab


Knowledge is power<br />My Blog : <a href="http://jimmod.blogspot.com/" target="_blank" rel="nofollow">http://jimmod.blogspot.com/</a>
Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
OK.



The result is
true
false

I think that the answer from the Mock test is not correct.
Only C is the answer.
jimmy halim
Greenhorn

Joined: Nov 28, 2006
Posts: 15
You're right.
My mistake, I don't read the option well

It'll not violating the contract if the equals :


Using String's length and mathematic (*/+-) operation on equals is really not an equals
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

the contract is clearly violated in answer D.
If two objects have different rate, they are equal but (may) have a diffent hashCode.

Where is the question from?


Yours,
Bu.


all events occur in real time
Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
Hi,
It is from Mock Test of K&B.
 
Consider Paul's rocket mass heater.
 
subject: A question from Mock Test about hashCode