Question

A Bag is similar to a Set; however, each entry may occur any number of times. One way to implement the class Bag would be to use a dictionary, similar to the way a List was used to implement the class Set in Figure 4.3. The value contained in the dictionary for a given entry would represent the number of times the entry occurs in the bag. The framework for such an implementation is shown below. Change the name of the class from Bag to MyBag, and complete the implementation of class Bag. Class Bag :Collection dict count | [ new dict $\leftarrow$ Dictionary new several missing methods ... first (count $\leftarrow$ dict first) isNil ifTrue: [ $\uparrow$ nil ]. count $\leftarrow$ count -1 . $\uparrow$ dict currentKey next [count notNil]whileTrue: $[($ count $>0)$ ifTrue: [count $\leftarrow$ count $-1 . \uparrow$ dict currentKey] iffalse: [count $\leftarrow$ dict next]]. $\uparrow$ nil ] Keep in mind that instances of Dictionary respond to first and next with values and not keys. However, the current key is accessible if you use the message currentKey.

   A Bag is similar to a Set; however, each entry may occur any number of times. One way to implement the class Bag would be to use a dictionary, similar to the way a List was used to implement the class Set in Figure 4.3. The value contained in the dictionary for a given entry would represent the number of times the entry occurs in the bag. The framework for such an implementation is shown below. Change the name of the class from Bag to MyBag, and complete the implementation of class Bag.
Class Bag :Collection
dict count |
[
new
dict $\leftarrow$ Dictionary new
several missing methods
...
first
(count $\leftarrow$ dict first) isNil ifTrue: [ $\uparrow$ nil ].
count $\leftarrow$ count -1 .
$\uparrow$ dict currentKey
next
[count notNil]whileTrue:
$[($ count $>0)$
ifTrue: [count $\leftarrow$ count $-1 . \uparrow$ dict currentKey]
iffalse: [count $\leftarrow$ dict next]].
$\uparrow$ nil
]
Keep in mind that instances of Dictionary respond to first and next with values and not keys. However, the current key is accessible if you use the message currentKey.
Show more…
A little Smalltalk
A little Smalltalk
Timothy Budd 1st Edition
Chapter 4, Problem 1 ↓

Instant Answer

verified

Step 1

We need to implement a class `MyBag` using a dictionary to keep track of the number of occurrences of each entry. The dictionary's keys will be the entries, and the values will be the counts of those entries.  Show more…

Show all steps

lock
AceChat toggle button
Close icon
Ace pointing down

Please give Ace some feedback

Your feedback will help us improve your experience

Thumb up icon Thumb down icon
Thanks for your feedback!
Profile picture
A Bag is similar to a Set; however, each entry may occur any number of times. One way to implement the class Bag would be to use a dictionary, similar to the way a List was used to implement the class Set in Figure 4.3. The value contained in the dictionary for a given entry would represent the number of times the entry occurs in the bag. The framework for such an implementation is shown below. Change the name of the class from Bag to MyBag, and complete the implementation of class Bag. Class Bag :Collection dict count | [ new dict $\leftarrow$ Dictionary new several missing methods ... first (count $\leftarrow$ dict first) isNil ifTrue: [ $\uparrow$ nil ]. count $\leftarrow$ count -1 . $\uparrow$ dict currentKey next [count notNil]whileTrue: $[($ count $>0)$ ifTrue: [count $\leftarrow$ count $-1 . \uparrow$ dict currentKey] iffalse: [count $\leftarrow$ dict next]]. $\uparrow$ nil ] Keep in mind that instances of Dictionary respond to first and next with values and not keys. However, the current key is accessible if you use the message currentKey.
Close icon
Play audio
Feedback
Powered by NumerAI
Need help? Use Ace
Ace is your personal tutor. It breaks down any question with clear steps so you can learn.
Start Using Ace
Ace is your personal tutor for learning
Step-by-step explanations
Instant summaries
Summarize YouTube videos
Understand textbook images or PDFs
Study tools like quizzes and flashcards
Listen to your notes as a podcast
Continue solving this problem
Create a free account to:
  • View full step-by-step solution
  • Ask follow-up questions with Ace AI
  • Save progress and study later
Continue Free
Numerade

Get step-by-step video solution
from top educators

Continue with Clever
or



By creating an account, you agree to the Terms of Service and Privacy Policy
Already have an account? Log In

A free answer
just for you

Watch the video solution with this free unlock.

Numerade

Log in to watch this video
...and 100,000,000 more!


EMAIL

PASSWORD

OR
Continue with Clever