Preamble to question 1
Solution to question 1a
Solution to question 1b
Solution to question 1c
Solution to question 1d
Solution to question 1e
solution to question 2
1) When executing the statement
pooh_bear.blinch....
WOE! Hold it right there....! What the heck does this mean?
IMPORTANT POINT: A computer program is
a whole bunch of instructions that you give to the computer. How
does the computer know where to start reading your instructions?
Well, The computer knows that the instructions start at the top of
the ROOT ROUTINE (procedure) of the ROOT CLASS. All the instructions
follow sequentially from there. The computer knows that it has finished
reading your program when it reaches the END of the ROOT ROUTINE (procedure)
of the ROOT CLASS.
In this case, the root class is WORLD
and the root routine is make.
The computer starts reading your program at the line IMMEDIATELY
after the word do
in the make routine
of the class WORLD.
It stops reading your program as soon as it reads the last end
in the make routine.
Check out the program text for the class WORLD:
do
1st instruction:
!!pooh_bear.make(4,"of little brain")
2nd instruction:
!!tigger.make(1,"bouncy")
3rd instruction:
!!eeyore.make(12,"gloomy")
4th instruction:
pooh_bear.blinch
end
So, When the question starts with When executing
the statement
pooh_bear.blinch...
it means While executing the 4th instruction (having
already executed the first 3) ...
1a) How many objects of type
WORLD exist when the statement
pooh_bear.blinch is being executed?
Remember that WORLD
is the root class. Therefore there will be at least 1 object of type
WORLD
- the one that is (magically) created by the computer when it runs the
program. Are there any other objects of type WORLD
in existence? The WORLD class doesn't
create any.
There is only one other class -
INHABITANT. Check out the program
definition of the class INHABITANT - it's got
2 features (also known as attributes) called age
and nature. Neither of which are of type WORLD
(age is of type INTEGER
and nature is of type STRING).
So I conclude that there are no other objects of type WORLD
in existence at this time.
Thus, when executing the statement pooh_bear.blinch,
there is 1 object of
type WORLD
1b) How many objects exist of
type INHABITANT at this time?
INHABITANTs
doesn't create INHABITANT objects.
Check out the program definition of the class WORLD
- it's got 3 features (attributes) called pooh_bear,
tigger
and eeyore (All are of type INHABITANT).
Remember, the first 3 instructions of the program have already been executed.
The first instruction (!!pooh_bear.make(4,"of
little brain"))
creates the object pooh_bear (of type
INHABITANT),
and then runs
pooh_bear's make
routine while passing 4 and "of
little brain" to make as arguments
(parameters).
The second instruction (!!tigger.make(1,"bouncy"))
creates the
object tigger, and then runs tigger's
make
routine while passing 1 and "bouncy"
to make as arguments (parameters).
The third instruction (!!eeyore.make(12,"gloomy"))
creates the
object eeyore, and then runs eeyore's
make
routine while passing 12
and "gloomy" to make
as arguments (parameters).
Thus, when the program is executing the 4th instruction (having
already executed the first 3), there are 3
objects of type INHABITANT in existence (pooh_bear,
tigger,
and eeyore).
IMPORTANT POINT: A class defines some attributes
and/or some
routines. Each object of that class then
owns a copy of each
attribute and each routine defined in the class.
For example: The class INHABITANT defines
some attributes (age,
nature) and some routines (make,
blinch).
Each object of the class
INHABITANT (pooh_bear,
tigger,
eeyore) then owns a copy of each
attribute (age, nature)
and each routine (make, blinch)
defined in
INHABITANT.
Thus, pooh_bear has the attributes age
and nature and the routines
make and blinch.
tigger has the attributes age
and nature and the routines make
and
blinch.
eeyore has the attributes age
and nature and the routines make
and
blinch.
pooh_bear's age
may have a different value from tigger's age
which may be different from eeyore's age.
But they've all got an age! Same applies
to nature.
Check out the program definition of the blinch
routine in the class
INHABITANT. pooh_bear,
tigger
and eeyore all own a copy of this
routine (because they're all objects of type INHABITANT).
The blinch routine makes a reference to the
attribute age (there is a line in blinch
that looks like (i > age)).
Which (who's) age is it referring to?
Well, that depends! If the blinch routine
was invoked by this instruction:
pooh_bear.blinch
then we'd be referring to pooh_bear's age. If the blinch routine was invoked by this instruction:
tigger.blinch
then we'd be referring to tigger's age.
1c) What routines are being
executed, in which object?
Remember that WORLD is the root class
and make is the root procedure. When any program
is being run, the root procedure of the root class is ALWAYS running.
When the root procedure of the root class has finished, then the program
has finished. So we know that at least one routine is being executed,
make
in the WORLD object.
Are there any others? Well, the question specifies "When
executing
the statement pooh_bear.blinch..."
Thus we know that another routine is being executed: blinch
- in the object pooh_bear.
Any others? Check out the program definition of the blinch
routine
(in the INHABITANT class). We know
that this routine is currently
executing. Does it invoke any other routines? Yes!
These two
instructions:
io.put_string("worry ")
io.put_new_line
The first one executes the routine put_string
on the object io
The second one executes the routine put_new_line
on the object io
Thus, to answer the question: The routine make
is being executed on the WORLD object, the
routine blinch is being executed on the
pooh_bear object. Since we don't
know which part of blinch the
computer is currently executing, it may also be executing put_string
on the io object or put_new_line
on the io object.
1d) Draw a representation of
the object referenced by pooh_bear
Remember that pooh_bear has attributes
age
and nature and routines
make and blinch.
pooh_bear's
make routine has been executed
(instruction 1). What does this do? Check out the program
definition of the make routine in the class
INHABITANT.
The make routine sets pooh_bear's
age to k and
sets pooh_bear's
nature
to s. What does this mean? k
is the first argument (parameter) that was passed to the make
routine (in this case, its 4). s
is the second argument (parameter) that was passed to the make
routine (in this case, its "of little
brain"). So now pooh_bear's age
equals 4 and pooh_bear's nature
equals "of
little brain".
Therefore, a representation of the object pooh_bear
would look like:
--------------------------------
| INHABITANT
|
|-------------------------------|
| attributes:
|
| age = 4
|
| nature = "of little brain" |
|-------------------------------|
| routines:
|
| make
|
| blinch
|
--------------------------------
For those of you who like the absolute truth - the above diagram is
not quite correct. nature is in
fact a string object and thus nature
does not equal "of little brain", it equals a reference
to the string
"of little brain". A reference could be represented by an arrow
from
the equals sign to the string "of little brain".
1e) How many times is the loop
body in blinch executed?
Check out the blinch routine in the class
INHABITANT. The loop looks
like:
from
i := 1
until
i > age
loop
io.put_string
i := i + 1
end
So what the question asks - how many times is the bit in purple
executed?
IMPORTANT POINT: Here's how a loop works:
The instructions between the words from
and until are
called the loop initializer
(they usually initialize some attribute to a starting value). The
expression
between the words until
and loop is called
the exit condition.
The instructions between the words loop
and end are called
the loop body.
1) Firstly, the loop
initializer is executed.
2) Then the exit
condition is tested.
3a) If the exit
condition is True
then the computer ignores the loop
body and continues executing after the end of
the loop.
3b) If the exit
condition is False
then the computer executes the
instructions in the loop
body.
4) Once the computer has finished reading the
loop body, the exit
condition is tested
again (go back to step 3).
The loop body
will be executed over and over again until the exit
condition is True
when tested.
So, using the above loop as an example:
1) The local attribute i is set
equal to 1
2) The exit condition (i > age)
is tested (remember that pooh_bear's
age is 4)
3) The exit condition (i > age)
is False (since (1 > 4) is False).
Thus, the loop body is executed...
("worry " is printed on the screen, i gets i + 1 : thus i = 2)
4) Once the loop body is finished, the exit condition is tested again
3) The exit condition (i
> age) is False (since (2 > 4) is False).
Thus, the loop body is executed...
("worry " is printed on the screen, i gets i + 1 : thus i = 3)
4) Loop body finished, exit condition is tested again
3) The exit condition (i > age) is False
(since (3 > 4) is False).
Thus, the loop body is executed...
("worry " is printed, i gets i + 1 : thus i = 4)
4) Loop body finished, exit condition is tested again
3) The exit condition (i > age) is False
(since (4 > 4) is False).
Thus, the loop body is executed...
("worry " is printed, i gets i + 1 : thus i = 5)
4) Loop body finished, exit condition is tested again
3) The exit condition (i > age) is True
(since (5 > 4) is True).
Thus, the loop body is ignored.
The loop is now over and the computer
will start executing the instructions after the loop.
So - how many times has the loop body executed?
4
IMPORTANT POINT : What's the difference between
an expression and an
instruction?
An instruction is something that you tell the
computer to do. e.g.;
io.put_string("Hello!")
-- print "hello"
x := 100 + 257 --
set x to the value of 100 + 257
pooh_bear.blinch --
invoke the blinch routine on pooh_bear
An expression is (usually) just some computation
that has a result, e.g.;
100 + 257 --
has the result 357 (of type INTEGER because 357 is an INTEGER)
i > age --
has the result either True or False (of type BOOLEAN)
(4 = 6) or (5 = 5) --
has result True (since 5 equals 5 - type BOOLEAN)
x.in_range(3,9) --
has the result either True or False (of type BOOLEAN)
So note the difference between expressions and instructions. e.g.;
x.in_range(3,9) --
expression
y := x.in_range(3,9)
-- instruction
left.item(4) --
expression (assume left
is an array)
value := left.item(4)
-- instruction (assume left
is an array)
In brief summary of this - if you are telling the computer to do
something then its an instruction. If you're asking the computer
a
question then its an expression.
2) How many lines of output does
the program generate?
When the question says "output", it means
"stuff printed on the
screen". The only way to print stuff
on the screen is by using one of
the commands:
io.put_string(".....")
io.put_integer(..)
io.put_character(..)
io.put_boolean(..)
io.put_new_line_line
Check out the program - only the blinch
routine contains some of these
instructions. Within the loop, there is a line
io.put_string("worry ")
and after the loop there is the line
io.put_new_line
Since the loop body is executed 4 times (as shown before), the output
due to the loop body will be:
worry worry worry worry
and then a new line character (i.e., the character you get when you
press enter) is printed.
Thus the number of lines of output is 1.
Hope this helps. If you've got any questions, please feel free
to email me.
BEST OF
LUCK WITH
YOUR
ASSIGNMENTS!!!!
Fergus.
(s3098148@bohm.anu.edu.au)
Edited by Richard Walker (Richard.Walker@cs.anu.edu.au).