Question: Description This assignment is an opportunity to work with objects using inheritance and polymorphism. Write a program that creates one superclass ( parent ) ,
Description
This assignment is an opportunity to work with objects using inheritance and polymorphism. Write a program that creates one superclass parent several subclasses child and use polymorphism. Write code to create the following class hierarchy in your program.
Animal
Bear Elephant Monkey Sloth
Superclass
Animal
Subclasses of Animal
Bear
Elephant
Monkey
Sloth
The code will perform actions on these objects and display information proving you have properly used inheritance and polymorphism. Use the provided file Animals.txt to test your code.
Specifications
Read these specifications to get an overview of assignment. When writing code, first create the classes.
Add this assignment to your project called CS
Create a Java class within that project called LastNameFirstNameAssignment
Follow CS Programming Assignments Policy"
Create superclass Animal see Classes section below for details
Create subclasses: Bear, Elephant, Monkey, Sloth see Classes section below for details
Write a test program ie main that:
a Open the test file Animals.txt for reading.
b Creates a polymorphic array to store the animals.
i The st value in the file indicates how big the array needs to be
ii Be sure to use a regular array, NOT an array list.
c Uses a loop to fill the array with each animals information read from a file.
i Instead of asking the user for information, you will read information from a file.
ii After reading the st value array size read the remaining values to create the animal objects and place them into the array.
For each line in the file:
a Read the type bear elephant, etc. name, food, weight, sleep, and location of the animal.
b Create a specific animal object.
i The type indicates what object to create.
ii Note that type is not sent to the constructor, instead, it is only used in main.
c Place the animal object into the polymorphic Animal array.
iii. For help, see step below for details about the file and the Must Do and Tips.
d AFTER all lines in the file have been read and all animal objects are in the array:
i Create a nd loop to iterate through the array and:
Display the animals type Bear Elephant, etc.
a Note that type is not an instance variable in Animal.
b Instead, we can use the instanceof operator to determine which object is in the current location in the array and display its type.
Animal is a Sloth
Display name, weight, sleep, and location of animal
a Call toString to display these values.
Call the following methods:
eat
sleep
swim
ii See output section at end of document for example output.
Test file Animalstxt information:
a Run your code on the provided file Animals.txt to test your code.
b This is a test file, so DO NOT assume that your code should work for only animals in the order specified in the file.
c The file contains the following animals:
Animal
Name
Food
Weight
Sleep
Location
Sloth
Ace
Leaves
Urban Jungle
Bear
Po
Bamboo
Asian Passage
Monkey
Rafiki
Fruit
Lost Forest
Sloth
Sid
Leaves
Urban Jungle
Elephant
Titan
Grass
Elephant Odyssey
Monkey
Louie
Fruit
Lost Forest
Bear
Baloo
Honey
Asian Passage
d The st line in the file is an integer value representing the number of animals in the file.
e The remaining lines contain details for each animal:
i There will be one animal for each line in the file.
ii The format of each animal line is as follows:
Type tells you what object to create it is NOT stored as an instance variable in object
Sloth Ace Leaves Urban Jungle
Name Food Weight Sleep Location store these values in instance variables
f The Animal.txt file that is provided looks like this:
Number of animals
Sloth Ace Leaves Urban Jungle
Bear Po Bamboo Asian Passage
Monkey Rafiki Fruit Lost ForestEach line provides the
Sloth Sid Leaves Urban Jungledetails for one animal
Elephant Titan Grass Elephant Odyssey
Monkey Louie Fruit Lost Forest
Bear Baloo Honey Asian Passage
Classes
Animal Class
Description
o Class that represents an animal.
o Superclass in hierarchy.
Private Data Fields all fields must be defined as private!
o name String
o food String
o weight int
o sleep int
o location String
Public Methods
o Constructor: public AnimalString name, String food, int weight,
int sleep, String location
Creates an Animal by initializing all private data fields to incoming values.
o Getter for each instance variable name, food, weight, sleep, and location
o Setters none this is being done on purpose so do NOT include setters
o The following additional public methods. The only code in themethods is a statement that t
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
