• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on designing model

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic