DogTest.java Answers

Click here to go back to the questions
// ANSWERS:

// (1) Fix the two errors in the Dog constructor.
// 

// ERROR #1: replace "Turtle" with "Dog".
// ERROR #2: replace line "aName = name" with "name = aName".

// 
// (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.
// 

System.out.println(p.describe());
System.out.println(s.describe());
System.out.println(z.describe());

// 
// (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.
// 
//
Dog r = new Dog("Rex",  3,p);
Dog j = new Dog("Jimbo",4,s);
Dog f = new Dog("Fido", 5,z);

// 
// (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).
// 

public void describe()
{
   System.out.println("I am a dog called " + name +
                      " and my age is " + age +
                      " and I have a flea called " + dogsFlea.name);
}

// Add the following lines to the main method:
System.out.println(r.describe());
System.out.println(j.describe());
System.out.println(f.describe());

// 
// (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.
// 

class DogOwner
{
   // Properties of the class...
   private String name;
   private int    salary;
   private Dog    ownersDog;

   // Constructor of the class...
   public DogOwner(String aName, int aSalary, Dog aDog)
   {
      name   = aName;
      salary = aSalary;
      dog    = aDog;
   }

}

// 
// (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.
// 

DogOwner a = new DogOwner("Angus",  10000,r);
DogOwner b = new DogOwner("Brian",  20000,j);
DogOwner c = new DogOwner("Charles",30000,f);

// 
// (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.
// 

public void describe()
{
   System.out.println("I am a dog owner called " + name +
                      " and my salary is " + salary +
                      " and my dog is " + ownersDog.name);
}

// Add the following lines to the main method:
System.out.println(a.describe());
System.out.println(b.describe());
System.out.println(c.describe());

// 
// (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.
// 
//
// ANSWER: I am a flea called Pop
//
//