Question

A useful control structure in many programming languages is a multiway switch statement, which permits the selection of one out of several possibilities based on the value of some selection key. Using cascades and blocks we can implement a multiway switch in Smalltalk. The class Switch will use the message new: both as a creation message and to assign the switch selection key. The message case:do: will compare the first argument to the selection key, and, if equal, the second argument (a block) will be evaluated. The message test:do: uses blocks for both arguments. The first argument, a one parameter block, is evaluated using the selection key; if it returns true, the second argument is evaluated. A flag is maintained by each instance of Switch indicating whether any condition has been satisfied. The message default: executes the argument, a block, if no previous condition has been met. For example, the following statement uses a variable suit representing the suit of a card from a deck of cards. It places into color a string representing the color of the card. Switch new: suit : case: #spade do: [ color $\left.\leftarrow^{\prime} b_{l a c k}{ }^{\prime}\right]$; case: #club do: [ color $\leftarrow$ 'black' ] ; test: $\left[: x \mid\left(x=\right.\right.$ #diamond) or: $[x=$ #heart $]$ do: [ color $\leftarrow^{\prime}$ red' $]$; default: [ self error: 'unknown suit ', suit ] Provide a class description for Switch.

   A useful control structure in many programming languages is a multiway switch statement, which permits the selection of one out of several possibilities based on the value of some selection key. Using cascades and blocks we can implement a multiway switch in Smalltalk. The class Switch will use the message new: both as a creation message and to assign the switch selection key. The message case:do: will compare the first argument to the selection key, and, if equal, the second argument (a block) will be evaluated. The message test:do: uses blocks for both arguments. The first argument, a one parameter block, is evaluated using the selection key; if it returns true, the second argument is evaluated. A flag is maintained by each instance of Switch indicating whether any condition has been satisfied. The message default: executes the argument, a block, if no previous condition has been met.
For example, the following statement uses a variable suit representing the suit of a card from a deck of cards. It places into color a string representing the color of the card.
Switch new: suit :
case: #spade do: [ color $\left.\leftarrow^{\prime} b_{l a c k}{ }^{\prime}\right]$;
case: #club do: [ color $\leftarrow$ 'black' ] ;
test: $\left[: x \mid\left(x=\right.\right.$ #diamond) or: $[x=$ #heart $]$ do: [ color $\leftarrow^{\prime}$ red' $]$;
default: [ self error: 'unknown suit ', suit ]
Provide a class description for Switch.
Show more…
A little Smalltalk
A little Smalltalk
Timothy Budd 1st Edition
Chapter 6, Problem 8 ↓

Instant Answer

verified

Step 1

Based on the description given, we know that each instance of Switch needs to maintain a flag indicating whether any condition has been satisfied. So, we can define an instance variable called "conditionSatisfied" of type Boolean.  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 useful control structure in many programming languages is a multiway switch statement, which permits the selection of one out of several possibilities based on the value of some selection key. Using cascades and blocks we can implement a multiway switch in Smalltalk. The class Switch will use the message new: both as a creation message and to assign the switch selection key. The message case:do: will compare the first argument to the selection key, and, if equal, the second argument (a block) will be evaluated. The message test:do: uses blocks for both arguments. The first argument, a one parameter block, is evaluated using the selection key; if it returns true, the second argument is evaluated. A flag is maintained by each instance of Switch indicating whether any condition has been satisfied. The message default: executes the argument, a block, if no previous condition has been met. For example, the following statement uses a variable suit representing the suit of a card from a deck of cards. It places into color a string representing the color of the card. Switch new: suit : case: #spade do: [ color $\left.\leftarrow^{\prime} b_{l a c k}{ }^{\prime}\right]$; case: #club do: [ color $\leftarrow$ 'black' ] ; test: $\left[: x \mid\left(x=\right.\right.$ #diamond) or: $[x=$ #heart $]$ do: [ color $\leftarrow^{\prime}$ red' $]$; default: [ self error: 'unknown suit ', suit ] Provide a class description for Switch.
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