Next: Tutorial 12 Overloading methods, Previous: Tutorial 10 Object arrays, Up: J.T.W. Tutorials [Contents][Index]
The following code presents example involving three classes Flea, Dog and DogOwner to represent the idea that a dog has a flea and a dog-owner has a dog. The class 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.
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".
class Flea begin property String name; constructor Flea(String aName) begin aName = name; end public method String toString() begin return "I am a flea called " + name; end end class Dog begin property String name; property int age; // Age in years property Flea dogsFlea; constructor Turtle(String aName, int anAge, Flea aFlea) begin name = aName; age = anAge; dogsFlea = aFlea; end end class DogTest begin beginMain Flea p = new Flea("Pop"); Flea s = new Flea("Squeak"); Flea z = new Flea("Zip"); System.out.println("p=" + p); endMain end
Question 4.11.2: In the main function of the DogTest class, write code to call the toString method for the fleas referenced by s and z.
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 f, j and 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.
HINT: If the flea called Pop is referenced by the variable name p, then this reference should appear as the third argument in one of the calls to the Dog constructor.
Question 4.11.4: Write a toString method in the Dog class that works like the toString method in the 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.
Question 4.11.5: By copying the pattern of the Flea and Dog classes, write a class DogOwner that has three non-private properties: name, salary and ownersDog. Also write a three-parameter constructor for the DogOwner class that sets these properties.
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) a, b and c. Use the 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.
Question 4.11.7: Without changing the call to the DogOwner constructor, change the value of the salary property of object referenced by a to 1,000,000. Note that since the salary property of the DogOwner class is non-private you should be able to set the value of the salary property from the main function of DogTest.
Question 4.11.8: Write a toString method for the class DogOwner and add some code to the main function to call it for Angus, Brian and Charles.
Question 4.11.9: What is the value of: a.ownersDog.dogsFlea.toString()? Add some code to the main function to find out if it does what you think it should do.
Next: Tutorial 12 Overloading methods, Previous: Tutorial 10 Object arrays, Up: J.T.W. Tutorials [Contents][Index]