• 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 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

equls/hashCode in MasterExam possible error

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
In the MasterExam shipped with the K&B 5.0 book there is a question concerning the equals() and hashCode() contract:



Answer is C & D.

I'm fine with C but D??
Think about it. Say that you have two SortOf objects a & b, initialized as follows:

a.equals(b) would then return true (as 3*2*2 == 4*1*3). According to the contract a.hashCode() and b.hashCode() must therefore return the same value. But they don't: a.hashCode() is 4*1=4 and b.hashCode() is 3*2=6!!

It would be nice if someone could verify this and tell me if I'm right or if I've overlooked something.
Thanks.
[ March 15, 2006: Message edited by: Daniel Svensson ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure they are trying to multiply "*" them together.

How do you multiply an int with a String.

Are you sure it isn't "+"?

Mark
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tend to agree with Daniel. I think the problem in the original statement is the author might not have taken into account that some integers can be written as a product in different ways.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dang

Okay, let's turn this into a "learning opportunity"

I'm not thrilled with this, but how about if d). was something like this:

return Long.toString(((SortOf)o).code.length() * ((SortOf)o).bal) + ((SortOf(o).rate == blah,blah...

Got any better ideas?

Bert
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the issue bowls down to the following propositions.

Proposition 1: If a,b,c, and a1,b1,c1 are integers and a*b*c = a1*b1*c1, then a*b=a1*b1.

This proposition is shown to be false with the sets of numbers 2,3,2 and 4,1,3.

Proposition 2: If a,b,c and a1,b1,c1 are integers and a*b+c = a1*b1+c1,
then a*b=a1*b1.

This proposition can also be shown to be false with the sets of numbers 2,4,1, and 2,3,3.

[ March 15, 2006: Message edited by: Keith Lynn ]
[ March 15, 2006: Message edited by: Keith Lynn ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[BB]: Got any better ideas?

1: Just make the answer C only.

2: Replace D with a more traditional equals method like

or

or even


3: Replace D with the nontraditional but valid


----

I'd also note that in fact, none of these methods are truly valid except "return this == other;". The reason is that they all can throw ClassCastException or NullPointerException if reference o is the wrong class, or null. The correct response would be to simply return false. This could be fixed with an instanceof check, e.g.:

[ March 15, 2006: Message edited by: Jim Yingst ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so far i like jim's option 3 the most...
 
Daniel Svensson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input. It's good to know that I wasn't on the wrong track after all.

Daniel
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic