• Home
  • Textbooks
  • A little Smalltalk
  • Primitives, Cascades and Coercions

A little Smalltalk

Timothy Budd

Chapter 6

Primitives, Cascades and Coercions - all with Video Answers

Educators


Chapter Questions

Problem 1

One advantage cited for cascaded expressions was that several Smalltalk statements could be combined together into one expression. For example the initialized Bag discussed in the text could be used as an argument to another object as follows:
anObject foo: (Bag new; add: 0; add 1)
How else might this be done in a single expression without using cascading? You can use temporary variables, if you wish.

Check back soon!
01:20

Problem 2

What will the result of typing the following expression be? Explain why.
$$
2+(3 \text { print }) ;+4
$$

Julie Silva
Julie Silva
Numerade Educator
01:31

Problem 3

Examine the class descriptions for the classes Object, Magnitude, Number, Integer, and Float. Explain in detail how radians respond to the messages $<=$ and $>=$.

Jeremy Stubbs
Jeremy Stubbs
Numerade Educator
00:29

Problem 4

Rewrite the methods for $=$ and $<$ in class Radian so that radians can be compared only to other radians.

Elizabeth Xu
Elizabeth Xu
Numerade Educator
01:14

Problem 5

Does the class Integer need to provide a method for coerce:? Why or why not? How about the class Number?

SS
Sarvesh Somasundaram
Numerade Educator

Problem 6

An alternative to having instances of class Radian maintain their value in an instance variable would be to make the class a subclass of class Float. Discuss the advantages and disadvantages of this approach.

Check back soon!
17:21

Problem 7

Recall that in the hierarchy of number classes all other classes have a higher ranking then any of Integer or Float. Assume that the class Number contains the following method
$\uparrow$ Complex new ; imagpart: self
That is, in response to the message $\mathrm{i}$, a number will create a new instance of the type Complex and initialize it with the current object.
Using this method, define the class Complex used to manipulate complex numbers. Your class should implement methods for the following messages:
new Set both the imaginary and real portions of the complex number to zero.
realpart: Define the real part of the current number to be the argument.
imagpart: Define the imaginary part of the current number to be the argument.
coerce: Coerce a non-complex, returning an equivalent complex number with zero imaginary part.
$+\quad$ Complex addition.
* Complex multiplication.
printString produce a printable representation of the number.
Test your class description by typing several example expressions involving complex numbers.

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator

Problem 8

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.

Check back soon!