1. Engineering
  2. Computer Science
  3. please help me code this project in java i would...

Question: please help me code this project in java i would...

Question details

Please help me code this project in JAVA. I would really appreciate slashes explaining the code as you go along, as I am a little confused.


(Simple Hierarchy Design)

Create a new folder for this lab (if working in Eclipse, create a new project)

Download Pet.java and PetDriver.java (I will paste code)


public class Pet
   private String name;
   private int age;
   private double weight;

   public Pet(String n, int a, double w)
      name = n;
      age = a;
      weight = w;

   public String getName()
      return name;
   public void celebrateBirthday()
      System.out.println("Happy birthday to " + name + "!");
   public void eat()
      System.out.println(name + " is eating");
   public void play()
      System.out.println(name + " is playing");
   public void sleep()
      System.out.println(name + " is sleeping");

   //NOTE: getClass().getName() will return the class name that the object was instantiated as.

   public String toString()
      return name + ", " + getClass().getName() + ", " + age + " years old, " + weight + " pounds";



public class PetDriver
   public static void main(String[] args)
      Cat oakey = new Cat("Oakey", 6, 18.5);
      Dog roxie = new Dog("Roxie", 12, 33.6);
      Hamster scooter = new Hamster("Scooter", 1, 0.3);
      Rabbit bigBunny = new Rabbit("Big Bunny", 9, 7.0);
      Pet thing = new Pet("Mystery Creature", 5, 10.0);

Complete the Pet hierarchy as follows:

Pet Dog Cat SmallAnimal Hamster Rabbit

  • All Pets can eat, sleep and play.

  • Dogs can bark and dig.

  • Cats can purr and scratch.

  • SmallAnimals can chew.

  • Hamsters can run on wheels.

  • Rabbits can thump.

All your class files should be in the same folder. After you complete all classes, run PetDriver.java and ensure that your output matches this example.

Desired output:

Oakey, Cat, 6 years old, 18.5 pounds
Roxie, Dog, 12 years old, 33.6 pounds
Scooter, Hamster, 1 years old, 0.3 pounds
Big Bunny, Rabbit, 9 years old, 7.0 pounds
Mystery Creature, Pet, 5 years old, 10.0 pounds
Oakey is eating
Roxie is digging
Happy birthday to Scooter!
Big Bunny is chewing
Mystery Creature is playing
Oakey is scratching
Roxie is eating
Scooter is running on its wheel!
Big Bunny is thumping
Mystery Creature is sleeping

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