Question: In this project you will learn about abstract classes. inheritance, and interfaces in Java. You also may learn something about farm animals that you may

In this project you will learn about abstract classes. inheritance, and interfaces in Java. You also may learn something about farm animals that you may not have known before. You will, however, NOT be tested about farm animals. But you DO need to learn about inheritance and interfaces.

In this project, we will model three grazing farm animals, namely Cows, Goats, and Horses. All of these animals are mammals. They are also all grazing mammals. But of course, all Mammals do not graze.

A few facts about these farm animals: Ruminants are a particular type of grazing mammal, in that Ruminants chew cud. They also have multiple stomachs for digesting their forage.

Cows and goats have multiple stomachs and chew cud. Horses graze but are not ruminants and do not chew cud and do not have multiple stomachs.

The concepts of Mammal, Grazing Mammal, and Ruminant are abstractions, which is a conceptual construct. You cannot go to a farm animal auction or Amazon and buy a Ruminant. If you ask for one, the seller will ask you what kind? You must buy an actual, concrete, non-abstract animal such as a cow. goat, or horse. For this reason, if we model Mammal, GrazingMammal, and Ruminant classes in Java, these classes are declared as abstract and cannot be instantiated (just like a ruminant cannot be born or bought).

However, the characteristics and behavior of mammals, grazing mammals, and ruminants can be inherited by their descendants, as shown below.

Your job is to implement the following class design:

All components of this exercise are provided in the instructions below. You will just need to assemble the components as instructed. To make this easier, you are provided below with a stubbed-out source code file listing only the declarations fo the classes that you need to populate with code.

You may implement your project using either Codiva or Intelli by creating a new project and adding this java file to your project, or adding a new class and pasting in the file below, to your new class.

You can download the empty java file GrazingMammals.java that you need to implement here.

How to implement the GrazingMammals project:

  1. Create an abstract class Mammal. As shown in the class diagram. Mammal has one public method nursesYoung() that prints out I am a + className + . I am nursing. You can get the class name from the instance of any class with the following:

String className = this.getClass().getName();

To get you started here is the full implementation of the class Mammal:

abstract class Mammal { public void nursesYoung() { String className = this.getClass().getName(); System.out.println("I am a " + className + ". I am nursing."); } }

2. Create an interface RuminantTester.. The interface should contain two public methods. You can copy and paste these into the interface.

 void testIfRuminant(); void testHasMultipleStomachs(); 

3. Create an abstract class GrazingMammal similar to Mammal but which implements the method grazes(). This method should print out "I am a " + className + ". I am grazing.", similar to what we did with the Mammal class.

GrazingMammal extends from Mammal and implements the interface RuminantTester.

In order to implement the interface you need to add the two methods in the interface to GrazingMammal. These are provided here:

 @Override public void testHasMultipleStomachs() { String className = this.getClass().getName(); if (this instanceof Ruminant) System.out.println("I am a " + className + ". I have multiple stomachs."); else System.out.println("I am a " + className + ". I do not have multiple stomachs."); } @Override public void testIfRuminant() { String className = this.getClass().getName(); if (this instanceof Ruminant ) System.out.println("I am a " + className + ". I am a Ruminant."); else System.out.println("I am a " + className + ". I am not a Ruminant."); }

The @Override annotation indicates that we are replacing the interface stub method with a method body. We can also override methods from an inherited class and this case also uses this annotation.

4. Create an abstract class Ruminant that extends GrazingMammal. Ruminant should print out "I am a " + className + ". I am chewing cud."), similar to the Mammal method implementation.

5. Add the classes Cow and Goat, which extend Ruminant and add the class Horse which extends GrazingMammal. These three classes are just class declarations with empty bodies, yet they provide all the behavior we need.

How is it possible that these classes can provide behavior when they have no code in the body of the class? Please include an answer to this question as part of your assignment submission.

6. A test driver class called GrazingMammals is proviided below, Add this to your project file.

public class TestMammals { public static void main(String[] args) { Cow cow = new Cow(); cow.nursesYoung(); cow.grazes(); cow.chewsCud(); cow.testIfRuminant(); cow.testHasMultipleStomachs(); System.out.println(" "); Goat goat = new Goat(); goat.nursesYoung(); goat.grazes(); goat.chewsCud(); goat.testIfRuminant(); goat.testHasMultipleStomachs(); System.out.println(" "); Horse horse = new Horse(); horse.nursesYoung(); horse.grazes(); horse.testIfRuminant(); horse.testHasMultipleStomachs(); } }

Notes:

  1. All these classes can be included in a single code file named the same as the main class, in this case, GrazingMammals.java. All other classes should be declared with just the class name (without the public access modifier).
  2. All components of this exercise are provided in the instructions above. You will just need to assemble the components as instructed. You may implement your project using either the Codiva or IntelliJ IDE. IntelliJ is preferred though.

Submission Instructions:

  • Submit your completed java file and a screenshot of your test output. Please also answer the question in step 3 above.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!