Java Newbie here. Had the below question during an interview sometime last week.
Model a forest given using OO design in Java 1. The forest contains several species of animals; lions, gorillas and rats.
All animals can do three things, make a sound, eat food, and sleep.
Each species of animal knows how many others of its kind exist.
By default when an animal’s energy level changes, it changes in the following ways:
-3 for making a sound
+5 for eating food
+10 for sleeping
The forest can perform a sound off. This involves all of the animals in the jungle each making their sound, along with reporting their energy level.
Lions get +5 energy for sleeping.
Gorillas get +2 energy for eating and -4 energy for making a sound.
Some animals have the ability to play.
Only gorillas can play. When they do they say "Oooo Oooo Oooo" and get -8 energy. If a gorilla doesn't have enough energy to play they say "Gorilla is too tired".
The jungle contains several types of food; meat, fish, bugs and grain.
Lions can't eat grain because they have sensitive digestive systems.
So I went about it like this:
Created an Interface IForrest which had getSpecie(), eventSoundOff(), getFood()
Class Forrest implements IForrest
Enum Class for Event : EAT, SLEEP, SOUND
abstract class Specie where i implemented a setEnergyLevel passing the event as the parameter ... used switch case on event to increment/decrease energylevel. was getting a compilation error here: Type mismatch: cannot convert from com.test.Specie.Event to com.test.Specie.Event. Not sure why.
created class Gorilla, Lion, Rats that extended Specie abstract class.
How would you go about it?