Control Flow Messaging

Exercise 5.1

| divisors |
divisors := [:x | (1 to: x) select: [:d | x \\ d = 0] ].
divisors value: 60.
⇒ #(1 2 3 4 5 6 10 12 15 20 30 60)
divisors value: 45
⇒ #(1 3 5 9 15 45)

Exercise 5.2

Check the implementations in Boolean, True and False.

Exercise 5.3

Once the method is edited and saved, in the Method pane select its name teleport: then do ...right click → more...change category...events...

Exercise 5.4

In the Method pane, select one uncategorized control method, then do ...right click → more...change categorynew... key-in control.

To categorized the remaining uncategorized control methods, repeat but select control at the last step as this category now exists.

Exercise 5.5

We do not need an iterator to detect a collision between two ships. However we use an iterator to take action on each ship when a collision is detected.

SpaceWar>>collisionsShips
| positionA position B |
   positionA := ships first morphPosition.
   positionB := ships second  morphPosition.
   (positionA dist: positionB) < 25 ifTrue: [
      ships do: [:each |
         each flashWith: Color red.
         self teleport: each]
   ]

Local variables only used to ease the code source formatting in printed book.

Exercise 5.6

You just need to pick the appropriate code snippets from the referenced exercise and examples.

SpaceWar>>collisionsTorpedoesStar
| position |
   position := centralStar morphPosition.
   torpedoes do: [:each | 
      (each morphPosition dist: position) < 8 ifTrue: [
         each flashWith: Color orange.
         self destroyTorpedo: each]]