This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Question on designing model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Question on designing model" Watch "Question on designing model" New topic
Author

Question on designing model

g masson
Greenhorn

Joined: Oct 07, 2013
Posts: 1
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?
 
Don't get me started about those stupid light bulbs.
 
subject: Question on designing model
 
Similar Threads
violence or non-violence
Survival of Strongest
making two methods into one
Should there ever be an abstract class for which ALL the methods are abstract?
what is the exact definition for Class and Object. with example