• Home
  • Textbooks
  • A little Smalltalk
  • A Simulation

A little Smalltalk

Timothy Budd

Chapter 7

A Simulation - all with Video Answers

Educators


Chapter Questions

Problem 1

Deciding when to use subclassing and when to use an instance variable is not always easy. An argument can be made that DiscreteProbability should really be a subclass of Random, such as the following:
Class DiscreteProbability :Random | weights max | defineWeights: anArray
weights $\leftarrow$ anArray.
max $\leftarrow$ anArray inject: 0 into: $[: x: y \mid x+y]$
next $[$ index value $\mid$
value $\leftarrow$ super randinteger: max.
index $\leftarrow 1$.
[value $>$ (weights at: index)]
whileTrue: [value $\leftarrow$ value - (weights at: index).
index $\leftarrow$ index +1$].$
$\uparrow$ index
defineWeights: anArray
weights $\leftarrow$ anArray.
max $\leftarrow$ anArray inject: 0 into: [:x:y|x+y]
next | index value |
value $\leftarrow$ super randinteger: max.
index $\leftarrow 1$.
[value $>$ (weights at: index)]
whileTrue: [value $\leftarrow$ value - (weights at: index).
index $\leftarrow$ index +1$]$.
$\uparrow$ index
]
Look at the class description for Random, in particular the response to the message randInteger:; and then describe why this class description will not produce the desired result.

Check back soon!
00:57

Problem 2

An alternative method of defining a discrete probability is to provide the actual sample space in a collection. For example, suppose a group of boys are observed to have heights represented by the array \#(60 54 606250 ). We can then ask for the height of a randomly selected boy. The following shows how a class SampleSpace might be used for this purpose:
sample $\leftarrow$ SampleSpace new ; define: \#(60 546062 50) sample first
60
Produce a class description for SampleSpace.

Joe Lesueur
Joe Lesueur
Numerade Educator
02:17

Problem 3

If written in a manner analogous to DiscreteProbability, the class SampleSpace defined in the last exercise is said to provide "random selection with replacement." The alternative, random selection without replacement, is frequently more useful. For example, a sample space might represent a deck of cards, and a random selection, the choosing of a card. This card should then be thought of as being removed from the deck and not available for return in subsequent selections. Describe how to modify the class SampleSpace to provide random selection without replacement.

Harsh Gadhiya
Harsh Gadhiya
Numerade Educator