• 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

A design question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 3 is hard to understand. Could you please give me some hints ?

especially the part has "put (element,this)" does it mean "this == element" so it is the same as put (element,element) ?

http://www.cs.uvic.ca/~wkui/Courses/Seng330/Assignment2-Summer06.htm

thank you so much
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, why do you think it would? What it means is that you are adding the element as a Key to the Hashtable and that the value associated with that key is "this". In fact, it means the value associated with every Key is "this".

As usual the example is so utterly ridiculous it's hard to fathom just what answer they expect.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is an exam question; they are expecting us to find mistakes. What you are doing is overloading the put method, but they have swapped key and element round, so you are putting the E as the K and "this" as the E. The if(!containKey(element)) statement means it will not happen twice for the same "element."

If you pass a key and an element as arguments, you will get the expected behaviour.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
It is an exam question; they are expecting us to find mistakes.



I understand that, my point was the mistakes are so numerous and egriegous that I wouldn't have any idea where to start. Usually with a question worded in that manner they're getting at something in particular.

Originally posted by Campbell Ritchie:
What you are doing is overloading the put method, but they have swapped key and element round, so you are putting the E as the K and "this" as the E. The if(!containKey(element)) statement means it will not happen twice for the same "element."



There is no E in a Map, it's just a Key-Value pair and the Keys are a Set, they may not be duplicated. This is just an absurd implementation of Set where the Set is the Keys of a Map. They add "this" as the Value simply because they have to add something. The fact that you have a bunch of irrelevent Values exposed should be the first hint at some of the undesired behavior of a Map.

Originally posted by Campbell Ritchie:
If you pass a key and an element as arguments, you will get the expected behaviour.



What? Huh? No. In this case the key IS the element and the desired behavior of a Set cannot be achieved by subclassing HashMap without inheriting a whole lot of undesired behavior too.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant E to be short for element.
 
Please enjoy this holographic presentation of our apocalyptic dilemma right after this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic