Class, model of Communicating Entities

Exercise 3.1

When the Float is selected, the wide text pane prints: “class definition for Float ° 92 instance methods ° 34 class methods ° 1280 total lines of code”

Exercise 3.2

0 to: Float twoPi by: 1/10 do: [:i |
   Transcript show: i cos; cr]

Exercise 3.3

1024 is not a random number. It is 210 then written in base 2 : 10000000000, it is also 1 << 10:

2^10 ⇒ 1024
1024 printStringBase: 2 ⇒ '10000000000'
1 << 10 ⇒ 1024 

Therefore, to multiply an integer by 1024, we shift left of 10 its digits:

360 << 10 ⇒ 368640 
360 * 1024 ⇒ 368640 

Exercise 3.4

5.2 + 0.9  - 6.1
⇒ 8.881784197001252e-16

5.2 + 0.7 + 0.11
⇒ 6.010000000000001

1.2 * 3  - 3.6
⇒ -4.440892098500626e-16

Exercise 3.5

The system returns the error ZeroDivide, division by zero.

Exercise 3.6

(52/10) + (9/10)  - (61/10)
⇒ 0

(52/10) + (7/10)  + (11/100)
⇒ 601/100 soit 6.01

(12/10) * 3  - (36/10)
⇒ 0

Exercise 3.7

There are different options, with slightly different results:

'There are 12 apples' select: [:i |i isLetter].
⇒ 'Thereareapples' 

Not really satisfying. So another option:

'There are 12 apples' select: [:i |i isDigit not].
⇒ 'There are  apples'

Or even a shorter option with the #reject: message:

'There are 12 apples' reject: [:i |i isDigit].
⇒ 'There are  apples' 

Exercise 3.8

In String, search for the method category format, there you find the format: method:

'Joe bought {1} apples and {2} oranges' format: #(5 4)
⇒  'Joe bought 5 apples and 4 oranges' 

Exercise 3.9

The SpaceWar, CentralStar and SpaceShip definitions with their added instance variable should look like:

Object subclass: #SpaceWar
   instanceVariableNames: 'centralStar ships torpedoes'
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Spacewar!'

Object subclass: #CentralStar
   instanceVariableNames: 'mass'
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Spacewar!'

Object subclass: #SpaceShip
   instanceVariableNames: 'position heading velocity
      fuel torpedoes mass acceleration'
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Spacewar!'

Exercise 3.10

SpaceShip>>position
   ^ position

SpaceShip>>velocity
   ^ position

SpaceShip>>mass
   ^ mass

Exercise 3.11

SpaceShip>>position: aPoint
   position := aPoint

SpaceShip>>velocity: aPoint
   velocity := aPoint

Torpedo>>heading: aFloat
   heading := aFloat

Exercise 3.12

SpaceShip>>left
"Rotate the ship to its left"
   heading := heading + 0.1

SpaceShip>>right
"Rotate the ship to its right"
   heading := heading - 0.1

Exercise 3.13

SpaceShip>>push
"Init an acceleration boost"
   acceleration := 10

SpaceShip>>unpush
"Stop the acceleration boost"
   acceleration := 0

Exercise 3.14

CentralStar>>initialize
   super initialize.
   mass := 8000.