In: Computer Science
Java Code:
Problem #1:
Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig.
In the base class, provide methods that are common to all rodents based on behaviours you find with a quick Internet search. Be sure to document the behaviours you implement (e.g., eat, sleep, groom, move, etc.). Each behaviour should print its action to standard output (e.g., rodent eating).
Next, refine these behaviours in the child classes to perform different behaviours, depending on the specific type of rodent, but only if the behaviour is actually different (e.g., mouse eating seeds or guinea pig eating grass). If you are having difficulty coming up with various behaviours, create a discussion on the Landing to compare notes with others taking the course.
Test your Rodent classes by writing a main() class and creating instances of every rodent, and demonstrate all the behaviours for each rodent.
I have implemented the inheritance hierarchy of Rodent Using Java. Please find the following Code screenshot, output, and code.
ANY CLARIFICATIONS/MODIFICATIONS/UPDATES REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
class Rodent { void eat() { System.out.println("Rodent is Eating"); } void sleep() { System.out.println("Rodent is Sleeping"); } void move() { System.out.println("Rodent is Running"); } void groom() { System.out.println("Rodent is Grooming"); } } class Mouse extends Rodent { void eat() { System.out.println("Mouse is eating seeds "); } void move() { System.out.println("Mouse is running"); } void sleep() { System.out.println("Mouse is Sleeping"); } void groom() { System.out.println("Mouse is Grooming"); } } class Gerbil extends Rodent { void eat() { System.out.println("Gerbil is eating Carrot"); } void move() { System.out.println("Gerbil is running"); } void sleep() { System.out.println("Gerbil is Sleeping"); } void groom() { System.out.println("Gerbil is Grooming"); } } class Hamster extends Rodent { void eat() { System.out.println("Hamster is eating Strawberry"); } void move() { System.out.println("Hamster is running"); } void sleep() { System.out.println("Hamster is Sleeping"); } void groom() { System.out.println("Hamster is Grooming"); } } class GuineaPig extends Rodent{ void eat() { System.out.println("GuineaPig is eating Timothy"); } void move() { System.out.println("GuineaPig is running"); } void sleep() { System.out.println("GuineaPig is Sleeping"); } void groom() { System.out.println("GuineaPig is Grooming"); } } public class MyRodentsDemo { public static void main(String args[]) { Rodent r[] = new Rodent[4]; r[0] = new Mouse(); r[1] = new Gerbil(); r[2] = new Hamster(); r[3] = new GuineaPig(); r[0].eat(); r[0].move(); r[0].groom(); r[0].sleep(); r[1].eat(); r[1].move(); r[1].groom(); r[1].sleep(); r[2].eat(); r[2].move(); r[2].groom(); r[2].sleep(); r[3].eat(); r[3].move(); r[3].groom(); r[3].sleep(); } } |