I was asked to finish this test in one of my interview and they wanted me to create classes intereface following all the right practices. I couldn't clear the interview but I sure am interested how you all experts out there would handle this.
Create an OODesign for the following scenario
There is a zoo, its has some animals. Following are the animals which live in the zoo Lion Tiger Bear
Each animal has enerygy level
Every animal speaks, eats, play by defalut when animal eat they gain +8 energy level by default when animal play they loose -5 energy level by defalut when animal speak they loose -1
By default each animal speak "Grrr..." By default every animal when they play they say "I am loving it"
Only bear can dance but when bear dance he says "Look MA' I am dancing"
Following are the foods available for animals Grain, Steak and salmon
开发者_高级运维When soundOff() method gets called on Zoo every animal speak
When Tiger speaks it says "ROARR..." When Lion speaks he says "Don't you dare ask me"
I would structure Zoo to contain a collection of the Animal base class to start with:
class Zoo {
private Collection<Animal> animals;
...
public void soundOff() {
Iterator<Animal> i = animals.iterator();
while( i.hasNext() )
i.next().speak();
}
then the Animal base class has the common methods/properties: speak, eat, play, energy level; also allow for the animal to reject food they don't like in the eat() method:
abstract class Animal {
private int energyLevel;
private String playMessage;
private String speakMessage;
...
public Animal(String speakMessage, String playMessage) {
this.speakMessage = speakMessage;
this.playMessage = playMessage;
}
public Animal() {
this( "Grrr...", "I am loving it" );
}
...
public abstract void eat( Food f ) throws IllegalArgumentException;
public void play() {
System.out.println( playMessage );
energyLevel += 8;
}
public void speak() {
System.out.println( speakMessage );
energyLevel -= 1;
}
..
}
then you can have specialized classes -- like DancerAnimal:
abstract class DancerAnimal extends Animal {
private String danceMessage;
public DancerAnimal(String speakMessage, String playMessage, String danceMessage) {
super( speakMessage, playMessage );
this.danceMessage = danceMessage;
}
public DancerAnimal( String danceMessage ) {
super();
this.danceMessage = danceMessage;
}
public void dance() {
System.out.println( danceMessage );
}
...
}
based on this you can implement Bear:
class Bear extends DanceAnimal {
public Bear() {
super( "Look MA' I am dancing" );
}
...
}
Lion and Tiger would just extend Animal base class and Food would probably be an enum.
My 2 cents on it. Bear in mind that the above doesn't handle concurrency -- so one thread calling dance while the other calls play will corrupt the energyLevel -- but you can simply add a Lock around this.
class AnimalZoo {
public int energyLevel=0;
public String defaultVoice = "Grrr";
public String sing = "I am lovinig t";
public static void main(String [] args) {
Tiger t = new Tiger();
Lion l = new Lion();
Bear b = new Bear();
AnimalZoo az = new AnimalZoo();
az.soundOff();
}
public void soundOff() {
t.speak();
l.speak();
b.speak();
}
public void eat {
energyLevel+=8;
}
public void play{
energyLevel-=5;
}
public void speak {
energyLevel-=1;
}
}
}
class Lion extends AnimalZoo {
defaultVoice = "Don't you dare ask me";
System.out.printnln(defaultVoice);
}
class Tiger extends AnimalZoo {
public void speak {
defaultVoice = "ROARR";
System.out.println(defaultVoice);
}
}
class Bear extends AnimalZoo {
public void dance() {
System.out.println("Look MA' I am dancing");
}
Not all what you're looking for but something like this would help you to figure it out, d
eclare an animal class with all the properties and behavior common to all of them, then declare all the classes that define properties and behavior for each animal that extends the base class:
abstract class Animal
{
private int energyLevel;
public Animal(int eLevel)
{
setEnergyLevel(eLevel);
}
public void setEnergyLevel(int eLevel)
{
this.energyLevel = eLevel;
if(energyLevel <= 0)
{
energyLevel = 0;
}
}
public void speak()
{
System.out.println("grr...");
setEnergyLevel(energyLevel - 1);
}
public void eat()
{
setEnergyLevel(energyLevel + 8);
}
public void play()
{
System.out.println("I'm loving it...");
setEnergyLevel(energyLevel - 5);
}
}
class Lion extends Animal
{
public Lion(int energy)
{
super(energy);
}
}
class Bear extends Animal
{
public Bear(int energy)
{
super(energy);
}
public void dance()
{
System.out.println("I am dancing...");
}
}
class Tiger extends Animal
{
public Tiger(int energy)
{
super(energy);
}
}
Try the following code:
public abstract class Animal {
int energyLevel = ZooAnimalConstants.DEFAULT_ENERGY;
String speakMessage;
String playMessage;
public Animal(int energy) {
this.setEnergyLevel(energy);
speakMessage="Grr...";
playMessage="I am loving it";
}
public void setEnergyLevel(int energy) {
if (energy < 0) {
this.energyLevel = ZooAnimalConstants.DEFAULT_ENERGY;
} else {
this.energyLevel = energy;
}
}
public void speak() {
System.out.println("speak message : "+speakMessage);
this.setEnergyLevel(this.energyLevel - ZooAnimalConstants.MINUS_SPEAK_ENERGY);
}
public void eat() {
this.setEnergyLevel(this.energyLevel + ZooAnimalConstants.ADD_EAT_ENERGY);
}
public void play() {
System.out.println("play message : "+playMessage);
this.setEnergyLevel(this.energyLevel - ZooAnimalConstants.MINUS_PLAY_ENERGY);
}
public int getEnergyLevel()
{
return this.energyLevel;
}
}
Dance Behaviour:
public interface DanceBehaviour {
public abstract void dance();
}
public class Bear extends Animal implements DanceBehaviour{
public Bear() {
super(ZooAnimalConstants.BEAR_DEFAULT_ENERGY);
}
public void dance()
{
System.out.println("Look MA' I am dancing");
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Bear";
}
}
public class Tiger extends Animal {
public Tiger() {
super(ZooAnimalConstants.TIGER_DEFAULT_ENERGY);
super.speakMessage = "ROARR ..";
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Tiger";
}
}
public class Lion extends Animal {
public Lion() {
super(ZooAnimalConstants.LION_DEFAULT_ENERGY);
this.speakMessage = "Don't you dare ask me";
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Lion";
}
}
import java.util.ArrayList;
public class Zoo {
ArrayList<Animal> listAnimal;
public Zoo()
{
listAnimal= new ArrayList<Animal>();
}
public void addAnimal(Animal animal)
{
if(null != listAnimal && null != animal)
{
listAnimal.add(animal);
}
}
public void soundOff()
{
for(Animal animal : listAnimal)
{
animal.speak();
}
}
public void printAnimalEnergyReport()
{
for(Animal animal : listAnimal)
{
System.out.println("Energy Level for : "+animal.toString() + " : "+animal.energyLevel);
}
}
}
public interface ZooAnimalConstants {
int DEFAULT_ENERGY=0;
int ADD_EAT_ENERGY = 8;
int MINUS_SPEAK_ENERGY = 1;
int MINUS_PLAY_ENERGY = 5;
int TIGER_DEFAULT_ENERGY=5;
int LION_DEFAULT_ENERGY=6;
int BEAR_DEFAULT_ENERGY=4;
}
public class ZooDemoMain {
public static void main (String args[])
{
Zoo myZoo = new Zoo();
Animal tiger = new Tiger();
Animal bear = new Bear();
Animal lion = new Lion();
myZoo.addAnimal(tiger);
myZoo.addAnimal(lion);
myZoo.addAnimal(bear);
myZoo.soundOff();
myZoo.printAnimalEnergyReport();
bear.setEnergyLevel(0);
if(bear.getEnergyLevel() == 0)
{
bear.eat();
myZoo.printAnimalEnergyReport();
}
}
}
精彩评论