Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task: You must create a very simple farm simulation. The simulation will run in a loop and call a method that indicates the passage of
Task:
You must create a very simple farm simulation. The simulation will run in a loop and call
a method that indicates the passage of time.
YOU MUST USE A GENERIC CONTAINER such as ArrayList.
The Generic it has must be some form of the base class ANIMAL such as:
ArrayList farm new ArrayList Animal ;
Create an Animal Class that will be the base class for all your animals: All animals
have some level of hunger from
When an animal is instantiated, it should randomly pick some number of hunger units
between and in the constructor
Since it is a small farm all animals have their own name.
All animals must be able to be fed decrease their hunger units by when they are fed
Animals must increase their hunger as time goes on: create a method that
increases their hunger by unit of hunger.
In the loop example below tick is used since the method relates to the virtual passing of
time. While in this case all that happens is the hunger level increases other simulators
might have much more complex effects.
When printed an animal should indicate. The animals name, the type of the animal
and the animals hunger level: Ely the cow is very hungry
If we are never going to instantiate an Animal directly what type of class could we make it
Explain. Put your answer in the comments at the bottom of your report.
Note: It is possible that when you run the simulation it will loop infinitely Can you
explain why? Put your answer in the comments at the bottom of your report
For larger sets of animals this would almost always happen.
You could put this in the constructor:
hungerUnits hungerUnitshungerUnits;
What does this do Why does this stop infinite loops?
Hunger Level Chart
Hunger Units Hunger Level Name Hunger Level
Full
Peckish
Hungry
Very Hungry
Starving
Create subclasses of farmyard animals:
a Each animal should also speak based on what type of animal it is and how
much they speak should change with their hunger level. A cow might moo
b The calculation for the times speaking will be different for each subclass
c The calculation for the first five animal subclasses should be:
i First Animal Class: Hunger Level
ii Second Animal Class: Hunger Level
iii. Third Animal Class: Hunger Level
iv Fourth Animal Class: Hunger Level
v Fifth Animal Class: Hunger Level
Create a class with a main method
a that instantiates of each or your animals and put them in an ArrayList.
Sort the animals by their hunger level most hungry to least hungry
b Allow each animal to speak, print it out and then feed it if it is not full.
Continue this process until all of them are full.
Implement a sleeping feature.
All animals have a percent chance of falling asleep when they are fed.
Side Effects of sleeping:
Animals that are asleep do not decrease their hunger units when fed.
Animals that are asleep Snore when then speak instead of their normal vocalization.
Animals wake up if their Hunger Level changes
The Output from the simulator might look something like this:
Feeding
cluck cluck cluck cluck
Yellow the Chicken is Very Hungry
woof woof woof
Teddy the Dog is Hungry
moo moo
Jackie the Cow is Peckish
cluck cluck
Dolly the Chicken is Peckish
moo
Jenny the Cow is Full
woof
Fido the Dog is Full
Feeding
cluck cluck cluck
Yellow the Chicken is Hungry
woof woof
Teddy the Dog is Peckish
moo
Jenny the Cow is Full
moo
Jackie the Cow is Full
cluck
Dolly the Chicken is Full
woof
Fido the Dog is Full
Feeding
cluck cluck
Yellow the Chicken is Peckish
woof woof
Teddy the Dog is Peckish
moo moo
Jenny the Cow is Peckish
moo
Jackie the Cow is Full
cluck
Dolly the Chicken is Full
woof
Fido the Dog is Full
Feeding
cluck cluck
Yellow the Chicken is Peckish
moo moo
Jackie the Cow is Peckish
cluck
Dolly the Chicken is Full
woof
Fido the Dog is Full
woof
Teddy the Dog is Full
moo
Jenny the Cow is Full
Feeding
cluck cluck
Dolly the Chicken is Peckish
woof woof
Fido the Dog is Peckish
woof
Teddy the Dog is Full
moo
Jenny the Cow is Full
cluck
Yellow the Chicken is Full
moo
Jackie the Cow is Full
Feeding
woof
Teddy the Dog is Full
moo
Jenny the Cow is Full
cluck
Yellow the Chicken is Full
moo
Jackie the Cow is Full
cluck
Dolly the Chicken is Full
woof
Fido the Dog is Full
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started