SuperClass/Subclass

/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class ZooKeeperRunner

{

  public static void main(String[] args)

  {

   // Step 5: students create the Elephant, Feline, Lion, and Tiger

   // classes each in their own file

   

   // Step 6:     Elephant elephant = new Elephant();

   // step 14

   Elephant elephant = new Elephant("leaves, grasses, roots", false, 60.0);

   elephant.trumpet();

   // step 15

   elephant.forage();

   elephant.eat();

   elephant.isNocturnal();

   elephant.getLifeSpan();

   

   System.out.println();

 

   // Step 8:     Tiger tiger = new Tiger();

   // step 22

   Tiger tiger = new Tiger("meat", true, 17.0);

   tiger.swim();

   tiger.huntAlone();

   // step 10

   tiger.growl();

   tiger.eat();

   tiger.isNocturnal();

   

   System.out.println();

   

   // step 24

   Tiger tiger2 = new Tiger();

   Elephant elephant2 = new Elephant();


   // step 25

   Gorilla gorilla = new Gorilla();

   

   System.out.println();

   

   // step 27

   Giraffe giraffe = new Giraffe("leaves", false, 25.0);

   giraffe.eat();

   

   System.out.println();

   

   // step 29

   Hippo hippo = new Hippo();

   hippo.eat();

   

   System.out.println();

 

   // Its Your Turn

   Penguin penguin = new Penguin();

   penguin.fish();

   

   System.out.println();

   

   Owl owl = new Owl();

   owl.hunt();

   Owl owl2 = new Owl("mice,insects", true, 4.0);

   owl2.eat();

   

   System.out.println();

   

   Animal a1 = new Animal();

   a1.sleep();

   Object a2 = new Animal();

  // a2.sleep();    error: cannot find symbol

   

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Animal

{

  private String food;

  private boolean nocturnal;

  private double aveLifeSpan;

 

  // step 11

  public Animal(String food, boolean nocturnal, double aveLifeSpan)

  {

    this.food = food;

    this.nocturnal = nocturnal;

    this.aveLifeSpan = aveLifeSpan;

  }

 

  // step 12

  public Animal()

  {

  }

 

  // It's your turn

  public void sleep()

  {

    System.out.println("zzzzz");

  }

 

  public void eat()

  {

    System.out.println("This animal's diet consists of " + food + ".");

  }

 

  public void isNocturnal()

  {

    if (nocturnal)

      System.out.println("This animal is active at night.");

    else

      System.out.println("This animal is active during the day.");

  }

 

  public void getLifeSpan()

  {

     System.out.println("This animal's life span is " + aveLifeSpan + " years.");

  }

 

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Primate extends Animal

{

  public void forage()

  {

    System.out.println("The primates forages for food.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Monkey extends Primate

{

  public void climb()

  {

    System.out.println("The monkey climbs.");

  }

 

  public void hoot()

  {

    System.out.println("The monkey hoots.");

  }

 

  public void howl()

  {

    System.out.println("The monkey howls.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Gorilla extends Primate

{

  // step 25

  public Gorilla()

  {

    System.out.println("A gorilla arrives.");

  }

 

  public void grunt()

  {

    System.out.println("The gorilla grunts.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Giraffe extends Hooved

{

  // step 27

  public Giraffe(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("A giraffe arrives.");

  }

 

  public void hum()

  {

    System.out.println("The giraffe hums.");

  }

 

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Monkey extends Primate

{

  public void climb()

  {

    System.out.println("The monkey climbs.");

  }

 

  public void hoot()

  {

    System.out.println("The monkey hoots.");

  }

 

  public void howl()

  {

    System.out.println("The monkey howls.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * created in step 5

 *

 *********************************************/

public class Feline extends Animal

{

  // step 23

  public Feline(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("A feline arrives.");

  }

 

  // step 24

  public Feline()

  {

    System.out.println("A feline arrives.");

  }

 

  public void roar()

  {

    System.out.println("The feline roars.");

  }

 

  public void growl()

  {

    System.out.println("The feline growls.");

  }


}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * created in step 5

 *

 *********************************************/

public class Tiger extends Feline

{

  // step 21

  public Tiger(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("A tiger arrives.");

  }

 

  // step 24

  public Tiger()

  {

    System.out.println("A tiger arrives.");

  }

 

  public void huntAlone()

  {

    System.out.println("The tiger hunts alone.");

  }

 

  public void swim()

  {

    System.out.println("The tiger swims!");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * created in step 5

 *

 *********************************************/

public class Lion extends Feline

{

  public void huntInPacks()

  {

    System.out.println("The lion hunts in packs.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Hooved extends Animal

{

  // step 27

  public Hooved(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("A hooved animal arrives.");

  }

 

  // step 29

  public Hooved()

  {

    System.out.println("A hooved animal arrives.");

  }

 

  public void forage()

  {

    System.out.println("The hooved animal forages for food.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Giraffe extends Hooved

{

  // step 27

  public Giraffe(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("A giraffe arrives.");

  }

 

  public void hum()

  {

    System.out.println("The giraffe hums.");

  }

 

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Hippo extends Hooved

{

  public void groan()

  {

    System.out.println("The hippo groans.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * created in step 5

 *

 *********************************************/

public class Elephant extends Animal

{

  // step 13

  public Elephant(String food, boolean nocturnal, double aveLifeSpan)

  {

    // step 16: System.out.println("Elephant arrives.");

    super(food, nocturnal, aveLifeSpan);

    // step 18:

    System.out.println("An elephant arrives.");

  }

 

  // step 24

  public Elephant()

  {

    System.out.println("An elephant arrives.");

  }

 

  public void trumpet()

  {

    System.out.println("The elephant trumpets loudly!");

  }

 

  public void forage()

  {

    System.out.println("The elephant forages for food.");

  }

}



/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Deer extends Hooved

{

  public void grunt()

  {

    System.out.println("The deer grunts.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * Its Your Turn

 *

 *********************************************/

public class Bird extends Animal

{

  public Bird(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("A bird arrives.");

  }

 

  public Bird()

  {

    System.out.println("A bird arrives.");

  }


}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * Its Your Turn

 *

 *********************************************/

public class Penguin extends Bird

{

  public void fish()

  {

    System.out.println("The penguin fishes for food.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *

 * Its Your Turn

 *

 *********************************************/

public class Owl extends Bird

{

  public Owl(String food, boolean nocturnal, double aveLifeSpan)

  {

    super(food, nocturnal, aveLifeSpan);

    System.out.println("An owl arrives.");

  }

 

  public Owl()

  {

    System.out.println("An owl arrives.");

  }

 

  public void hunt()

  {

    System.out.println("The owl hunts for food.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Animal

{

  private String food;

  private boolean nocturnal;

  private double aveLifeSpan;

 

  // step 11

  public Animal(String food, boolean nocturnal, double aveLifeSpan)

  {

    this.food = food;

    this.nocturnal = nocturnal;

    this.aveLifeSpan = aveLifeSpan;

  }

 

  // step 12

  public Animal()

  {

  }

 

  // It's your turn

  public void sleep()

  {

    System.out.println("zzzzz");

  }

 

  public void eat()

  {

    System.out.println("This animal's diet consists of " + food + ".");

  }

 

  public void isNocturnal()

  {

    if (nocturnal)

      System.out.println("This animal is active at night.");

    else

      System.out.println("This animal is active during the day.");

  }

 

  public void getLifeSpan()

  {

     System.out.println("This animal's life span is " + aveLifeSpan + " years.");

  }

 

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Deer extends Hooved

{

  public void grunt()

  {

    System.out.println("The deer grunts.");

  }

}


/*********************************************

 * Sample solution Activity 4.9.2

 *********************************************/

public class Animal

{

  private String food;

  private boolean nocturnal;

  private double aveLifeSpan;

 

  // step 11

  public Animal(String food, boolean nocturnal, double aveLifeSpan)

  {

    this.food = food;

    this.nocturnal = nocturnal;

    this.aveLifeSpan = aveLifeSpan;

  }

 

  // step 12

  public Animal()

  {

  }

 

  // It's your turn

  public void sleep()

  {

    System.out.println("zzzzz");

  }

 

  public void eat()

  {

    System.out.println("This animal's diet consists of " + food + ".");

  }

 

  public void isNocturnal()

  {

    if (nocturnal)

      System.out.println("This animal is active at night.");

    else

      System.out.println("This animal is active during the day.");

  }

 

  public void getLifeSpan()

  {

     System.out.println("This animal's life span is " + aveLifeSpan + " years.");

  }

 

}