1. Engineering
  2. Computer Science
  3. using your dog class from earlier this week complete the...

Question: using your dog class from earlier this week complete the...

Question details

Using your Dog class from earlier this week, complete the following:

Create a new class called DogKennel with the following:

  • Instance field(s) - array of Dogs + any others you may want

  • Contructor - default (no parameters) - will make a DogKennel with no dogs in it

  • Methods:

  • public void addDog(Dog d) - which adds a dog to the array

  • public int currentNumDogs() - returns number of dogs currently in kennel

  • public double averageAge() - which returns the average age of all dogs in the kennel

  • public void adoptDog(String n) - remove the dog with given name

  • Create 1 more method that uses your mutator method from the dog class somehow

Create a main test - either in the DogKennel class file or in it’s own file

  • Test should create a DogKennel and test each of the methods you have written

Here is the Dog Class:

public class Dog {
   // Instance FIelds
   String name, breed;
   int age;

   // Constructors
   // Default Constructor
   public Dog() {
       name = "";
       breed = "";
       age = 0;
   }

   // Parameterized Constructor
   public Dog(String n, String b, int a) {
       name = n;
       breed = b;
       age = a;
   }

   // Methods
   // Accessor Methods
   // Method to return name of Dog
   public String getName() {
       return name;
   }

   // Method to return breed of Dog
   public String getBreed() {
       return breed;
   }

   // Method to return age of Dog
   public int getAge() {
       return age;
   }

   // Mutator Method
   // Method to change breed of Dog
   public void changeBreed(String b) {
       breed = b;
   }

   // Driver method main
   public static void main(String args[]) {
       // Instantiate the class Dog
       Dog dog1 = new Dog(); // Using Default Constructor
       Dog dog2 = new Dog("Browney", "German Shepered", 5); // Using Parameterized Constructor

       // Print the current status of dogs
       System.out.println("Dog 1: ");
       System.out.println("Name: " + dog1.getName() + " Breed: " + dog1.getBreed() + " Age: " + dog1.getAge());
       System.out.println("Dog 2: ");
       System.out.println("Name: " + dog2.getName() + " Breed: " + dog2.getBreed() + " Age: " + dog2.getAge());

       // Mutate the breed of dog1
       dog1.changeBreed("Pomerian");

       // Print the changed dog
       System.out.println("Dog 1: ");
       System.out.println("Name: " + dog1.getName() + " Breed: " + dog1.getBreed() + " Age: " + dog1.getAge());
   }
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution