CS 1401 Homework #8

Date Assigned: Tuesday, March 15, 2005

Due Date: Thursday, March 17, 2005, before the beginning of your lecture section.

Goal: the goal of this assignment is to practice with inheritance and static variables.


1.      Inheritance. Assume that all animals have an integer weight expressed in pounds, and that all animals make noise.  The default noise will be assigned initially as “chirps”.  An object-creating superclass Animal has the following definition:

            public class Animal  {


            // Weight in pounds

private int weight;


            //  Constructor

           public Animal (int inputWeight) {

               weight = inputWeight;              



            // Accessor method for weight

           public int getWeight ( ) {

                return weight;



// Facilitator method to return sound animal makes

public String makesNoise ( ) {

    return “chirps”;



// toString method

public String toString ( ) {

    return (“The animal weighs :  ” + weight  + “ pounds and ” + makesNoise ( ) ) ;



      On a separate sheet of paper, write the code to define a class Dog, which will be a derived class (subclass) of Animal.  Within the application class, the line below will be used to instantiate a new Dog object (“Chihuahua” is a breed of dog).  Override any of the methods of the superclass that should be modified to fit the Dog class.

      Dog doggy = new Dog ( “Killer” ,  Chihuahua” , 8); 

      Hint:  The Dog subclass constructor is passed 3 arguments; the Animal superclass requires only 1.




2.      Static Variables.  In the application class, we want to add a static variable numDogs, which will keep count of how many dog objects are created.  It will be initialized to 0 when the application starts.  Add a line of code to your Dog subclass to increment numDogs each time a new instance of Dog is created.