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....
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...
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...
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT