Instructions
Program listing |
// REFERENCES TUTORIAL // Copyright (C) 1998-2016 Davin Pearson // Website: http://davin.50webs.com/java-tutorials class Flea { /* * Properties of the class... */ public String name; /* * Constructor of the class... */ public Flea(String aName) { name = aName; } /* * Methods of the class... */ public void describe() { System.out.println("I am a flea called " + name); } } class Dog { /* * Properties of the class... */ public String name; public int age; public Flea dogsFlea; /* * Constructor of the class... */ public Turtle(String aName, int anAge, Flea aFlea) { aName = name; age = anAge; dogsFlea = aFlea; } } class DogTest { /* * * The main method is the point of entry into the program... * */ public static void main(String[] args) { Flea p = new Flea("Pop"); Flea s = new Flea("Squeak"); Flea z = new Flea("Zip"); } } // QUESTIONS: // (1) Fix the two errors in the Dog constructor. // // (2) In the main method of the DogTest class, write code // to call the "describe" method for each of the fleas // referenced by p,s and z. // // (3) In the main method of the DogTest class, write code // to construct three dogs called Rex, Jimbo and Fido. 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 Rex has a flea called Pop, // Jimbo has a flea called Squeak, and Fido 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. // // (4) Write a describe method in the Dog class that works // like the describe method in the Flea class. Then call // this method from the main method to print out the full // statistics of the three dogs that you have just created // in question (3). // // (5) By copying the pattern of the Flea and Dog classes, // write a class DogOwner that has three properties: name, // salary and ownersDog. Also write a three-parameter // constructor for the DogOwner class that sets these // properties. // // (6) Add some code into the main method 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. Use the Dog // references that you created in question (3) to achieve // this. // // (7) Write a describe method for the class DogOwner and // add some code to the main method to call it for Angus, // Brian and Charles. // // (8) If your reference to the Angus object is called // "a", then what is the value of the following: // // a.ownersDog.dogsFlea.describe() // // Add some code to the main method to find out. //