TUTORIAL 3 QUESTIONS

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?
  1. uppity Green
  2. uppity up; up.
    kermit height: (uppity height)
  3. kermit right; right.
    uppity position: (kermitposition)
  4. kermit (colour printString)
  5. tempcelsius := 15.
    tempfarenheit :=32 + tempcelsius * 9/5
  6. "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.

  1. 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

  2. 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.
  1. Define a class method maxRight which accesses the value of the MaxRight class variable and returns this value.
  2. 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.
  3. 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.

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: 4th April, 2000