• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 76862
366
  • 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: 76862
366
  • 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.
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic