GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

Main Menu Research Projects Photo Album Curriculum Vitae The Greatest Artists
Email Address Computer Games Web Design Java Training Wheels The Fly (A Story)
Political Activism Scruff the Cat My Life Story Smoking Cessation Other Links
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5
Tutorial 6 Tutorial 7 Tutorial 8 Tutorial 9 Tutorial 10
Tutorial 11 Tutorial 12 Tutorial 13 Tutorial 14 Tutorial 15
Tutorial 16 Tutorial 17 Tutorial 18 Using Emacs Download Links


J.T.W. tutorial 11: using references to link between different classes


§ 11 Tutorial 11

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 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 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 11.3: In the main method 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 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 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 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 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 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 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.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Fri 30 Jul 2021 19:01:06 NZST
Best viewed at 1024x768 or above resolution.
© Copyright 1998-2021 Davin Pearson.
Please report any broken links to