S.J.S. tutorial 11: using references to link between different classes
SECTION(11, Tutorial 11)
m5_question(The following code presents example involving three
classes CLSS(Flea), CLSS(Dog) and CLSS(DogOwner) to represent the idea that
a EM(dog) has a EM(flea) and a EM(dog-owner) has a EM(dog). The class
CLSS(DogTest) is the driver class. The key concept of this tutorial is
that classes can have references of objects of another class in order
to set up a relationship between the two classes.)
m5_question(BO(Question 11.1) Study the following code and find the
two bugs in it. Fix the bugs and then compile and run it to verify
that it prints out STRI(TT("p=I am a flea called Pop")).
m4_begin_indent
NU()m4_include(sjs-tutorials/DogTest.m4)
m4_end_indent
)
m5_question(BO(Question 11.2:) In the MAIN function of the
CLSS(DogTest) class, write code to call the TT(toString) method for the
fleas referenced by TT(s) and TT(z).)
m5_question(BO(Question 11.3:) In the MAIN method of the
CLSS(DogTest) class, write code to construct three dogs called
TT("Fido"), TT("Jimbo") and TT("Rex"). For the purposes of the rest
of these questions, let the name of the references for Fido, Jimbo and
Rex be TT(f) TT(j) and TT(r). Note that the third parameter to the
CLSS(Dog) class is of type CLSS(Flea). Therefore you will need to supply
a CLSS(Flea) reference for each dog. Make it so that Fido has a flea
called Pop, Jimbo has a flea called Squeak, and Rex has a flea called
Zip.
PP
RED(HINT:) If the flea called Pop is referenced by the variable name
TT(p), then this reference should appear as the third argument in one
of the calls to the CLSS(Dog) constructor.)
m5_question(BO(Question 11.4:) Write a TT(toString) method in the
CLSS(Dog) class that works like the TT(toString) method in the CLSS(Flea)
class. Then call this method from the MAIN function to print out the
full statistics of the three dogs that you have just created in
Question QEST_FIDO_JIMBO_REX.)
m5_question(BO(Question 11.5:) By copying the pattern of the
CLSS(Flea) and CLSS(Dog) classes, write a class CLSS(DogOwner) that has
three non-private properties: TT(name), TT(salary) and TT(ownersDog).
Also write a three-parameter constructor for the CLSS(DogOwner) class
that sets these properties.)
m5_question(BO(Question 11.6:) Add some code into the MAIN function
to construct three dog owners called TT(Angus), TT(Brian) and
TT(Charles). Make it so that Angus has a dog called Rex, Brian has a
dog called Jimbo, and Charles has a dog called Fido. For the purposes
of the rest of these questions, let the name of the references for
Angus, Brian and Charles be (respectively) TT(a), TT(b) and TT(c).
Use the CLSS(Dog) references that you created in Question
QEST_FIDO_JIMBO_REX to achieve this. Make it so that Angus, Brian and
Charles have initial salaries of 10,000, 20,000 and 30,000.)
m5_question(BO(Question 11.7:) Without changing the call to the
CLSS(DogOwner) constructor, change the value of the TT(salary) property
of object referenced by TT(a) to 1,000,000. Note that since the
TT(salary) property of the CLSS(DogOwner) class is non-private you
should be able to set the value of the TT(salary) property from the
MAIN function of CLSS(DogTest).)
m5_question(BO(Question 11.8:) Write a TT(toString) method for the
class CLSS(DogOwner) and add some code to the MAIN function to call it
for Angus, Brian and Charles.)
m5_question(BO(Question 11.9:) What is the value of:
TT(a.ownersDog.dogsFlea.toString())? Add some code to the MAIN
function to find out if it does what you think it should do.)