• Post Reply Bookmark Topic Watch Topic
  • New Topic

A design question  RSS feed

 
Thanh Cute
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56209
171
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56209
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant E to be short for element.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!