OU Courese Banner ADBH Web - M206 TMA3 Question 2
ADBH Web 05-Sep-2008

M206 - TMA3 Question 2

(a) The first error in take UpYourSidePositions: aFrog is the line of code (aFrog position = 11) just after the ifTrue statment. This will cause the following whileFalse: statement as (aFrog position = 11) to be in error as the syntax for this should be a Boolean block statement (e.g. [……..]). Therefore this is a syntax error as it breaks Smalltalk language rules

The corrected line of code is: [aFrog position = 11]

1) The error page. (Sorry about the quality of this next series of diagrams - scanned from TMA's not longer have in electronic from)

Screen capture

2) The Receivers page showing the state of the dancerl instance variable and aNumber argument;

Screen capture 2

3) The Message page shows the message expression where the error was detected.

Screen capture 3

(c)This second mistake causes a semantic error in the compiler because the second whileFalse statement is expecting a Boolean true or false from the condition block [aFrog position: 11] but the condition block is stating move aFrog to position 11. The actual error message produced in the debugger message screen states "non-Boolean - proceed for truth" receiver. This is all caused by a logical error position:11 which itself is neither semantically (run-time) or syntactically wrong. This, in turn causes the semantic error - whileFalse: sent to aFrog rather than Boolean.

(d)Corrected version:

takeUpYourSidePosition: aFrog
"Cause the frog object identified by aFrog to hop to the nearest side from its current location."

  • (aFrog position < 6)
  • ifTrue: [
  •   [aFrog position = 1]
  •   whileFalse:[aFrog jump; left]]
  • ifFalse: [
  •   [aFrog position =11]
  •   whileFalse: [aFrog jump; right]]

(e) Replace the first whileFalse: message in the method takeUpYourSidePosition: by a timesRepeat: message, without changing the meaning of the method.

takeUpYourSidePosition: aFrog

"Cause the frog object identified by aFrog to hop to the nearest side from its current location."

  • (aFrog position < 6)
  • ifTrue: [
  •   ((aFrog position)-1)
  •   timesRepeat:[aFrog jump; left]]
  • ifFalse:[
  •   [aFrog position = 11]
  •   whileFalse:[aFrog jump; right]]

(f) whileFalse: receiver is a Boolean message block; argument is Block

timesRepeat: receiver is anIntegerObject; Block (containing an expression series)

(ii) Instance method danceToMeet of class BarnDanceCaller, and the instance method moveToCentre of class Frog.

danceToMeet

"Cause the frog objects identified by dancer1 and dancer2 to dance to their central stones. Dancerl and dancer2 must be at their side positions position 1 or 11) for this to happen, it doesn't matter if both are at the same side."

  • ((dancer1 position = 1 or:[dancer1 position 11]) and: [(dancer2 position = 1 or:[dancer2 position = 11])])
  • ifTrue:[[dancer1 position =6]
  • whileFalse:[dancer1 jump; jump; moveToCentre.
  •   dancer2 jump; jump; moveToCentre]].

moveToCentre

"Cause the frog object to move one stone nearer to its central stone."

  • (self position 6)
  • ifFalse:[(self position <6)
  •   ifTrue:[self right]
  •   ifFalse:[self left]].

 

Question 1 - Question 3

Top of Page

 

 

 

Page built in 0.0006 seconds