• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Need Explanation

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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); }

Answer provided:

A, B, C, and E are correct.
 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
 
subpadma karthik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got this question from SCJP practical exam PDF written by Katty Siera and Bates
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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); }

Answer provided:

A, B, C, and E are correct.



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

Chan.
 
subpadma karthik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

subpadma karthik wrote:
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?



Hopefully, you will understand why your arguments are incorrect -- and both C and E are correct, even though the two algorithms are using different values.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!