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.
1. Add this assignment to your project called CS1150
2. Create a Java class within that project called LastNameFirstNameAssignment12
3. Follow "CS1150 Programming Assignments Policy"
4. Create 1 superclass Animal (see Classes section below for details)
5. Create 4 subclasses: Bear, Elephant, Monkey, Sloth (see Classes section below for details)
6. Write a test program (i.e., main) that:
a. Open the test file Animals.txt for reading.
b. Creates a polymorphic array to store the animals.
i. The 1st 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 1st 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 7 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 2nd loop to iterate through the array and:
1. 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[0] is a Sloth
2. Display name, weight, sleep, and location of animal
a. Call toString to display these values.
3. Call the following methods:
eat
sleep
swim
ii. See output section at end of document for example output.
7. Test file (Animals.txt) 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 7 animals in the order specified in the file.
c. The file contains the following animals:
Animal
Name
Food
Weight
Sleep
Location
Sloth
Ace
Leaves
12
15
Urban Jungle
Bear
Po
Bamboo
550
20
Asian Passage
Monkey
Rafiki
Fruit
27
17
Lost Forest
Sloth
Sid
Leaves
15
12
Urban Jungle
Elephant
Titan
Grass
12500
4
Elephant Odyssey
Monkey
Louie
Fruit
14
16
Lost Forest
Bear
Baloo
Honey
1050
18
Asian Passage
d. The 1st 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 1215 Urban Jungle
Name Food Weight Sleep Location (store these 5 values in instance variables)
f. The Animal.txt file that is provided looks like this:
7Number of animals
Sloth Ace Leaves 1215 Urban Jungle
Bear Po Bamboo 55020 Asian Passage
Monkey Rafiki Fruit 2717 Lost ForestEach line provides the
Sloth Sid Leaves 1512 Urban Jungledetails for one animal
Elephant Titan Grass 125004 Elephant Odyssey
Monkey Louie Fruit 1416 Lost Forest
Bear Baloo Honey 105018 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 Animal(String 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
Description This assignment is an opportunity to

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 Programming Questions!