Question

In: Computer Science

Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In...

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.

Solutions

Expert Solution

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();
   }
}

Related Solutions

1)A black guinea pig crossed with an albino guinea pig produced twelve black offspring. When the...
1)A black guinea pig crossed with an albino guinea pig produced twelve black offspring. When the albino was crossed with a second black guinea pig, six blacks and six albinos were obtained. What is the best explanation for this genetic situation? A)Albino is recessive; and the second black guinea pig was homozygous dominant B)Albino is co-dominant; and the second black guinea pig was heterozygous C)Albino is recessive; and the second black guinea pig was heterozygous D)Albino and black are codominant....
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship...
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object. A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation: CommissionCompensationModel - For Employees who are paid...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types described above Create and Add...
Java programming question:Consider the inheritance hierarchy in the figure on the next page.The top...
Java programming question:Consider the inheritance hierarchy in the figure on the next page. The top of the hierarchy begins with class Shape, which is extended by subclasses TwoDShape and ThreeDShape, corresponding to 2D and 3D shapes, respectively. The third level of this hierarchy contains specific types of 2D and 3D shapes such as circles and spheres, respectively. The arrows in the graph represent is a(inheritance) relationships and the boxes represent classes. For example, a Triangle is a TwoDShape, which in...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home,...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home, vehicle , life 2 member functions should be included: - calcpremium    - Home- 0.5% of the value of the home    - Life- 1% of the value of the policy    - Vehicle - 0.5% of the value of the policy calcCommission Home- 0.2% of the value of the home Life- 1% of the value of the policy Vehicle - 0.5% of the...
java code: Problem 1: Create a class called Elevator that can be moved between floors in...
java code: Problem 1: Create a class called Elevator that can be moved between floors in an N-storey building. Elevator uses a constructor to initialize the number of floors (N) in the building when the object is instantiated. Elevator also has a default constructor that creates a five storey building. The Elevator class has a termination condition that requires the elevator to be moved to the main (i.e., first) floor when the object is cleaned up. Write a finalize() method...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT