TUTORIAL 3 QUESTIONS WITH ANSWERS
CHAPTERS 17, 20, 21, 22
Q1 Each of the following series of message expressions for the Amphibian class and its subclasses has a number of errors. kermit has been created as a Frog instance and uppity as a HoverFrog instance. Identify the errors and classify each as: syntax, semantic or logical. If you entered the expressions into the workspace and attempted to evaluate them, how would the system respond? How could you correct the errors?
- uppity Green
- uppity up; up.
kermit height: (uppity height)
- kermit right; right.
uppity position: (kermitposition)
- kermit (colour printString)
- tempcelsius := 15.
tempfarenheit :=32 + tempcelsius * 9/5
- "Expression series to sum the integers 1 to 10 and return the total"
num := 0.
sum := 0.
[num > 10] whileFalse: [num := num + 1. sum := sum+num].
sum1to10 := sum.
^sum1to10
Q2 How would you use the debugger to detect and correct the error in f.?
Q3 Rewrite the expression series in f. to include a dialogue warning just before sum := sum + num to produce a dialogue box like the following:
What would the effect of this be and how would you use it to detect the error?
Q4 I wish to create a method in the Frog class called highestSquare which returns the highest square number which is less than or equal to the position of the receiver object eg if the Frog is on stone 10, the method will return the number 9. If position is less than 1, then the method returns 0. The method name and comments are as below. Complete the method body.
highestSquare
"Calculate the highest square number that is less than or equal to the position of the receiver. Answer the square number. If position is less than 1, return the number 0"
Q5 We wish to create a new concrete subclass of the abstract Amphibian class called Newt. This will have an additional instance variable named length, which is used to refer to its body length in millimetres. When a new Newt object is created, we wish to initialise the instance variables to position the object on stone 2, set its body length to 50 and its colour to brown. It moves in steps of half a stone.
- Bearing in mind that each amphibian subclass has different instance variables, starting positions, initial colours and left and right movements, for each of the following instance methods, state which must be defined in the Newt subclass and which need not be defined there and can be taken from the Amphibian superclass:
home, brown, initialize, position, left, colour
- In setting up the Newt subclass, we forget to create an instance method right (though we have correctly created all the other methods). We further create another method doubleRight as follows:
doubleRight
"Move right twice (ie by one whole stone). Answer the receiver"
self right; right
When the message nigel doubleRight is sent, after an instance of the class called nigel has been created, we get the following System Error message:

Choosing debug, we then get the following screen display:
Explain for each of the lower three messages in the message stack:
- precisely what message is evaluated?
- which class or subclass the instance method is in?
Q6 In the Newt class, we create a class variable MaxRight which records the furthest right position ever reached by a member of this class.
- Define a class method maxRight which accesses the value of the MaxRight class variable and returns this value.
- Define a class method maxRight: which sets the class variable MaxRight to a specified value eg maxRight: 6 sets the variable to 6 and returns the receiver.
- Write an instance method right which moves the Newt object half a stone to the right and updates the value of the class variable MaxRight if necessary. To do this it must access this variable, check if the object's position is greater than the value of MaxRight and if so set the value of MaxRight to the value of the object's position variable.
ANSWERS
Q1
- syntax error - Green should be spelled green
- semantic error - kermit being a Frog does not have an instance variable height

- this will be treated as a syntax error since the system will assume you are trying to use a variable name kermitposition and will ask you to create or correct it:

correcting it to kermit position will solve the problem
- syntax error - the expression will not evaluate and you will get a "nothing more expected" message after kermit in the evaluation pane
- logical error - this will evaluate as:
tempfarenheit :=(32 + tempcelsius) * 9/5
whereas, with parentheses in the right place, you want it to evaluate as:
tempfarenheit :=32 + (tempcelsius * 9/5)
- logical error - the expression to the right of whileFalse: will be evaluated 11 times, with values of num from 1 to 11, whereas you want it to stop after 10 iterations
Q2 You could use the debugger by inserting a self halt message immediately before num := 0. Then you could step through the evaluation of the expression series, noting that there were 11 iterations not 10.
Q3 "Expression series to sum the integers 1 to 10 and return the total"
num := 0.
sum := 0.
[num > 10] whileFalse:
[num := num + 1.
(Dialog warn: ('The value of num is ', num printString)).
sum := sum+num].
sum1to10 := sum.
^sum1to10
This is an alternative to using the debugger (and perhaps more useful since it explicitly displays the value of a key variable). This will produce the warning box at each iteration and you will be able to spot that at the final iteration num has a value of 11, one more than intended.
The simplest correction is to change num > 10 to num >= 10.
Q4
highestSquare
"Calculate the highest square number that is less than or equal to the position of the receiver. Answer the square number. If position is less than 1, return the number 0"
| num |
num := 1.
[(num*num <= self position)] whileTrue: [num := num + 1].
num := num - 1.
^(num*num)
Q5
home, initialize, left must be defined for the Newt class - all the others can be taken from the Amphibian superclass
from the bottom up: doubleRight (from the Newt subclass); right (from the Amphibian superclass) as part of the doubleRight instance method ; subclassResponsibility which is from the Object superclass
The displays as each are highlighted are as follows (with the words highlighted on the screen shown underlined):
textEditorEvaluation
"Generated method for text editor evaluations."
nigel doubleRight
"Move right twice (ie by one stone). Answer the receiver"
self right; right
right
^self subclassResponsibility
Q6
- maxRight
"Returns the value of class variable MaxRight"
^MaxRight
- maxRight: aValue
"Sets the class variable MaxRight to aValue"
MaxRight := aValue
- right
"Move newt object half a stone to the right. If value of position variable is greater than class MaxRight variable set the latter to the value of position variable"
|max|
self position: (self position + 0.5).
max := (Newt maxRight).
(self position > max) ifTrue: [Newt maxRight: (self position)]
NOTE - The conversion to HTML has lost some of the indentation which is considered normal good practice.
Return to the OU Home Page
Most recent revision:
24th April, 2001