Question
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
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)
Pet.java
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() { age++; 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"; } }
PetDriver.java
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); System.out.println(oakey); System.out.println(roxie); System.out.println(scooter); System.out.println(bigBunny); System.out.println(thing); oakey.eat(); roxie.dig(); scooter.celebrateBirthday(); bigBunny.chew(); thing.play(); oakey.scratch(); roxie.eat(); scooter.runOnWheel(); bigBunny.thump(); thing.sleep(); } }
Complete the Pet hierarchy as follows:
-
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
Pet Dog Cat SmallAnimal Hamster RabbitStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started