wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Random question 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 "Random question" Watch "Random question" New topic
Author

Random question

Joe Li
Greenhorn

Joined: Nov 02, 2003
Posts: 5
The following is the code from Dan's Mock Exam in Hashcodes topic
Question 6
import java.util.Random;
class A {
private int i1;
public int hashCode() {
Random random = new Random(i1);
return random.nextInt();
}}
class B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}
Suppose that the equals method of classes A, B, C and D all make use of the value of the int variable, i1. Which classes have legal--but not necessarily good-- implementations of the hasCode method?
a. A
b. B
c. C
d. D
The answer is b,c,d
My question is is that if the variable, i1, has same value, then the Random seed is same, so the random sequence will be same.
So if i1 is same, then its hashcode is same too. the results consistent with the hashCode contract. The answer a. should be right.
Am I wrong?
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
In the explanation, Dan writes:

The hashCode contract requires that the hashCode method returns the same value whenever it is invoked on the same object more than once during an execution of a Java application provided no information used in the equals comparisons is modified. It is unlikely that the Random.nextInt method will provide results consistent with the hashCode contract.

I'd say, all of the 4 classes have a legal hashCode() method because legal means compiling without an error as per K&B. Appropriate is the legal one not violating hashCode() and equals() contracts, and efficient is an appropriate hashCode() method that allows for the objects to land most evenly in hash buckets and thus improves hashing speed. Having said that, b, c, and d are appropriate hashCode() implemetations as they consistently return the same integer. A is tricky - it's using a Random class to generate an integer for hashCode(). But, every time hashCode() is invoked, a new Random object is created with the same seed. Here's the constructor:

And this is the setSeed() method:

nextInt() calls method next(int bits) with argument 32. But, the initial seed is set to the same integer for every new object with private variable i1 value being equal! So, to me, class A has indeed a legal and even appropriate hashCode() implementation.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Thank you Joe and Vad for that correction. I must have tested it without the seed and then added it later. In the next version of the exam, I'll remove class A entirely. Removal of the seed would allow for changing hashcodes, but it would leave people wondering about the operation of the nextInt method and that is far beyond the scope of the exam.
I'll also update the terminology as suggested by Vad. To say that something that violates a contract is legal but not appropriate seems a little too kind in my opinion, but I'll accept it.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Dan,

I'll also update the terminology as suggested by Vad. To say that something that violates a contract is legal but not appropriate seems a little too kind in my opinion, but I'll accept it.

Here's a quote from K&B p. 444:

❑ A legal hashCode() override compiles and runs.
❑ An appropriate hashCode() override sticks to the contract.
❑ An efficient hashCode() override distributes keys randomly across
a wide range of buckets.

Hopefully, I haven't altered anything from there in my post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random question
 
Similar Threads
hashcode method
Hashcode contract
HashCode and equals
Question based on hashcode()
Dan's Collection