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

# Need Explanation

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Hi All,

[b]Please explain how the answers are applicable:[/b]

Given:
2. class Chilis {
3. Chilis(String c, int h) { color = c; hotness = h; }
4. String color;
5. private int hotness;
6. public boolean equals(Object o) {
7. Chilis c = (Chilis)o;
8. if(color.equals(c.color) && (hotness == c.hotness)) return true;
9. return false;
10. }
11. // insert code here
12. }
Which, inserted independently at line 11, fulfill the equals() and hashCode() contract for
Chilis? (Choose all that apply.)
A. public int hashCode() { return 7; }
B. public int hashCode() { return hotness; }
C. public int hashCode() { return color.length(); }
D. public int hashCode() { return (int)(Math.random() * 200); }
E. public int hashCode() { return (color.length() + hotness); }

A, B, C, and E are correct.

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Got this question from SCJP practical exam PDF written by Katty Siera and Bates

Rancher
Posts: 1090
14
• Number of slices to send:
Optional 'thank-you' note:

subpadma karthik wrote:Hi All,

[b]Please explain how the answers are applicable:[/b]

Given:
2. class Chilis {
3. Chilis(String c, int h) { color = c; hotness = h; }
4. String color;
5. private int hotness;
6. public boolean equals(Object o) {
7. Chilis c = (Chilis)o;
8. if(color.equals(c.color) && (hotness == c.hotness)) return true;
9. return false;
10. }
11. // insert code here
12. }
Which, inserted independently at line 11, fulfill the equals() and hashCode() contract for
Chilis? (Choose all that apply.)
A. public int hashCode() { return 7; }
B. public int hashCode() { return hotness; }
C. public int hashCode() { return color.length(); }
D. public int hashCode() { return (int)(Math.random() * 200); }
E. public int hashCode() { return (color.length() + hotness); }

A, B, C, and E are correct.

What's your take at it? What it is that you don't understand?

Chan.

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Can you explain how all the options A,B,C,E are correct?

A is returning 7, B is returning hotness which is not getting any value in the method..
Similarily C and E has got different values.. By this how come the hashcode contract gets satisfied?

Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
According to the hashcode contract, every call must return the same value when invoked on the same object

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode

With math.random() there is no guarantee that multiple calls would return the same value.

Henry Wong
author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

And after you read the link provided by Joe. Can you review your statements?