
The following code presents example involving three classes TT(Flea), TT(Dog)
and TT(DogOwner) to represent the idea that a dog has a flea and a
dog-owner has a dog. The CLASS TT(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.

@cindex Setting up relationships between different classes.
@cindex Relationships between different classes.

BO(Question 4.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 "p=I am a flea called Pop".

@example
CLASS Flea
BEGIN
PD PROPERTY String name;

PD CONSTRUCTOR Flea(String aName)
PD BEGIN
PD PD aName = name;
PD END

PD POBLIC METHOD String toString()
PD BEGIN
PD PD return "I am a flea called " + name;
PD END
END

CLASS Dog
BEGIN
PD PROPERTY String name;
PD PROPERTY int age; COMM(// Age in years)
PD PROPERTY Flea dogsFlea;

PD CONSTRUCTOR Turtle(String aName, int anAge, Flea aFlea)
PD BEGIN
PD PD name     = aName;
PD PD age      = anAge;
PD PD dogsFlea = aFlea;
PD END
END

CLASS DogTest
BEGIN
PD BEGINMAIN
PD PD Flea p = new Flea("Pop");
PD PD Flea s = new Flea("Squeak");
PD PD Flea z = new Flea("Zip");
PD PD System.out.println("p=" + p);
PD ENDMAIN
END
@end example

BO(Question 4.11.2:) In the MAIN FUNCTION of the DogTest class, write code
to call the TT(toString) METHOD for the fleas referenced by s and z.

BO(Question 4.11.3:) In the MAIN FUNCTION of the DogTest class, write code
to construct three dogs called "Fido", "Jimbo" and "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 Dog class is of type Flea. Therefore
you will need to supply a 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.

BO(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 Dog constructor.

BO(Question 4.11.4:) Write a TT(toString) method in the TT(Dog) CLASS that
works like the TT(toString) method in the TT(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 11.3.

BO(Question 4.11.5:) By copying the pattern of the TT(Flea) and TT(Dog)
classes, write a class TT(DogOwner) that has three non-private
properties: name, salary and ownersDog. Also write a three-parameter
constructor for the TT(DogOwner) CLASS that sets these properties.

BO(Question 4.11.6:) Add some code into the MAIN FUNCTION to construct three
dog owners called Angus, Brian and 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 TT(Dog) references that you created in
Question 11.3 to achieve this. Make it so that Angus, Brian and
Charles have initial salaries of 10,000, 20,000 and 30,000.

BO(Question 4.11.7:) Without changing the call to the TT(DogOwner)
constructor, change the value of the salary property of object
referenced by TT(a) to 1,000,000. Note that since the salary property
of the TT(DogOwner) class is non-private you should be able to set the
value of the salary property from the MAIN FUNCTION of DogTest.

BO(Question 4.11.8:) Write a TT(toString) METHOD for the CLASS
TT(DogOwner) and add some code to the MAIN FUNCTION to call it for
Angus, Brian and Charles.

BO(Question 4.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.
