IN JAVA Utilizing polymorphism construct a class hierarchy t
IN JAVA
Utilizing polymorphism, construct a class hierarchy that totals up feeding history of 4 animal types (elephant, dolphin, lion, and beaver).
You must at a minimum include the following methods:
getAnimalType (Returns the animal’s type. Should return “Elephant”, “Lion”, etc.)
getAnimalName (Return animal’s name, not what animal type it is. For instance, “Simba”, “Dumbo”, etc.)
getAnimalLocation (returns the animal’s location in the zoo. For instance, “Lion Cage 1”)
getFoodTypes (Returns a list of the types of food the animal eats and the unit the food is measured in (HINT: Enumerations may help))
addFeeding (this will take in the type and amount of food fed)
getFeedingTotals (this returns a list of the animal\'s total food intake. For Dumbo in the table above, this would be 80 lbs of hay and 5 lbs of peanuts)
Construct a tester program that tests your class hierarchy (no user input necessary). Your program must have at least one of each animal instantiated. At a minimum, your tester program must feed each of the animals and then print out records of their feedings. Each public method must be called at least once in the test.
Solution
Polymorphism refers to the smae funciton name with different arguments, that can be passed.
It is part of the OOPs concept in Java.
Program listed below -
In the below program the function getAnimalType() has been overloaded, to demonstarte polymorphism.
Return type mainly \'string\' is used here, to return the corresponding values.
Please review.
public class Animals
{
public static void main(String agrs[]) // main method
{
string anmltype[3]={\"elephant\",\"dolphin\",\"lion\",\"beaver\"}; // declaring array for the animals
public string getAnimalType(string \"type\") // function to return the category or type of the animal in \'getanimaltype\' method.
{
System.out.println(\"Enter the animal ou wish\");
scanner.in(type);
for(int i =0; i<size(anmltype);i++)
{
if(anmltype[i] = type);
systme.out.println (\"The animal chosen is\" + type);
return type;
}
}
==========================================
public String getAnimalType(String \"name\") // Function to return the name of the animal demonstarted using switch case construct.
{
int num;
System.out.println(\"enter the number\")
scanner.in(num);
case
switch(num)
case 1:System.out.println(\"Elephant name is Dumbo\");
return name;
break;
case 2:System.out.println(\"Lion name is Simba\");
return name;
break;
case 3:System.out.println(\"Dolphin name is Dolly\");
return name;
break;
case 4: System.out.println(\"Bevear name is teddy\");
return name;
end case
break;
System.out.println(\"please enter correctly\");
break;
}
==========================================================================
public String getAnimalType(String \"getanimallocation\") // function to print the food eaten as the per the names of the animals.
{
System.out.println(\"Dumbo, the elehphant stays next to th eBanyan tree\");
System.out.println(\"Simba, the lion stays in the cave\");
Sytem.out.println(\"Dolly, the dolphin stays in the pool\");
System.out.println(\"Teddy the bevear stays in the bush\");
}
========================================================================
public String getFoodTypes(String \"food\") //returns the food type based on the animal type demonstarted using hashtable using enumeration.
{
Hashtable <object> ht = new Hashtable <object>
Hashtable <object,object> ht = new Hashtable<object,object>();
ht.put(\"Dumbo\",\"1kg of rice\");
ht.put(\"Simba\",\"1 buffallo\");
ht.put(\"Dolly\",\"3 medium size fish\");
ht.put(\"teddy\",\"3 bundles of grass\");
Eumeration <object> en = ht.keys();
Enumeration <object> en1 = ht.elements();
while(en.hasmoreelements())
{
system.out.println(en.nextElement() + \"____\" + en1.nextElement());
return;
}}
}
}

