Trying to write this class in java and could use some help Class Zoo java.lang.O
ID: 3589074 • Letter: T
Question
Trying to write this class in java and could use some help
Class Zoo
java.lang.Object
zoo.Zoo
public class Zoo extends java.lang.Object
This class models a simple zoo with four cages and four different types of animals (one in each cage). It also has a zoo keeper that tends to the animals as instructed by client code through the various public methods. Lastly, the zoo has a pantry in which different types of foods are stored for the animals.
Field Summary
Fields
Modifier and Type Field and Description
static int DOLPHINLOCATION
The cage number for the Dolphin in the zoo.
static int ELEPHANTLOCATION
The cage number for the Elephant in the zoo.
static int PENGUINLOCATION
The cage number for the Penguin in the zoo.
static int TIGERLOCATION
The cage number for the Tiger in the zoo.
Constructor Summary
Constructor and Description
Zoo()
Constructor for Zoo which creates all necessary elements for the zoo simulation as described in the class-level javadoc.
Method Summary
Modifier and Type Method and Description
boolean cleanCage(int cage)
Cleans the cage of the Animal at the specified location if sufficient energy is available to the keeper.
int endDay()
Ends a day at the Zoo.
boolean feedAnimal(int cage, ZooPantry.AllowableFoods foodId)
Feed an Animal found in the given cage location a Food in the given foodCrate location only if there is enough food in the foodCrate and the Keeper has enough energy to expend on the Food's exhaustion cost.
boolean giveTigerBall()
ZooKeeper gives ball to the Tiger if there is sufficient energy available.
boolean swimWithDolphin()
Keeper Swims with the Dolphin if sufficient energy is available and updates state information of both ZooKeeper and Dolphin accordingly.
java.lang.String toString()
Generates a String representation of the Game.
Field Detail
ELEPHANTLOCATION
public static final int ELEPHANTLOCATION
The cage number for the Elephant in the zoo.
DOLPHINLOCATION
public static final int DOLPHINLOCATION
The cage number for the Dolphin in the zoo
.
PENGUINLOCATION
public static final int PENGUINLOCATION
The cage number for the Penguin in the zoo.
TIGERLOCATION
public static final int TIGERLOCATION
The cage number for the Tiger in the zoo.
See Also:
Constant Field Values
Constructor Detail
Zoo
public Zoo()
Constructor for Zoo which creates all necessary elements for the zoo simulation as described in the class-level javadoc. Upon completion of the constructor, this Zoo's state should include one of each Animal type, a ZooPantry, a ZooKeeper, and an initial score of 0.
Method Detail
toString
public java.lang.String toString()
Generates a String representation of the Game. Creates String with each Animal followed by newline, each Food followed by newline, and Keeper energy and score. Each of these sections is ended with a newline. See example output for format.
Overrides:
toString in class java.lang.Object
Returns:
String of current state of the game.
endDay
public int endDay()
Ends a day at the Zoo. Sends all Animals to sleep. Randomly delivers a food to the ZooPantry, and resets the ZooKeeper's energy level for the next day.
Returns:
Total score for the end of the current day, which is the sum of each Animal's score for the day.
feedAnimal
public boolean feedAnimal(int cage, ZooPantry.AllowableFoods foodId)
Feed an Animal found in the given cage location a Food in the given foodCrate location only if there is enough food in the foodCrate and the Keeper has enough energy to expend on the Food's exhaustion cost. If possible, feeds Food to the Animal, consumes the Food, and reduces Keeper energy by Food's exhaustion cost. Returns true if Food successfully given to Animal, false if not.
Parameters:
cage - Cage location of Animal to feed, must be a valid cage location per the public constants defined in by the Zoo.
foodId - The kind of Food to feed the Animal in the specified cage.
Returns:
Success of feeding Animal. Should be false if there is sufficient food, the keeper lacks enough energy or if the cage value is invalid.
cleanCage
public boolean cleanCage(int cage)
Cleans the cage of the Animal at the specified location if sufficient energy is available to the keeper. If energy is available the keeper cleans the cage and expends energy.
Parameters:
cage - Cage location to be cleaned, must be a valid cage location per the public constants defined in by the Zoo.
Returns:
Success of cleaning the cage. False if the keeper lacks sufficient energy to clean or if the cage location specified is invalid.
swimWithDolphin
public boolean swimWithDolphin()
Keeper Swims with the Dolphin if sufficient energy is available and updates state information of both ZooKeeper and Dolphin accordingly.
Returns:
True if the keeper was able to swim with the dolphin successfully, false otherwise.
giveTigerBall
public boolean giveTigerBall()
ZooKeeper gives ball to the Tiger if there is sufficient energy available. If energy is available, the ZooKeeper will update her energy and give the ball to the Tiger.
Returns:
Returns true if the Keeper was able to give the ball; false if not.
Explanation / Answer
Solution==========================
Seems a lot of classes and it's associated functions are missing, Like Animal, Pantry etc..
I have tried to emulate many these classes, so to make a somewhat complete.. Feel free to modify and enchance to make it more, let me know if any doubts..
import java.util.Random;
//A makeshift Zoo Pantry
class ZooPantry{
public static enum AllowableFoods {Meat, Plants};
public static int foodAmountLeft[] = {100,100};
}
//Animal Class
class Animal{
boolean sleep;
public Animal(){
sleep=false;
}
public void sleep(boolean status){
sleep=status;
}
public void feed(){
}
}
//Zoo keeper
class Zookeeper{
int energy;
public Zookeeper(){
energy=10;
}
public void resetEnergy(){
energy=10;
}
public int getEnergy(){
return energy;
}
public boolean reduceEnergy(int amt){
if((energy-amt)<0) return false;
energy=energy-amt;
return true;
}
}
public class Zoo {
public static int DOLPHINLOCATION;
public static int ELEPHANTLOCATION;
public static int PENGUINLOCATION;
public static int TIGERLOCATION;
Animal tiger;
Animal elephant;
Animal penguin;
Animal dolphin;
Zookeeper zookeeper;
public Zoo(){
zookeeper = new Zookeeper();
tiger= new Animal();
elephant= new Animal();
penguin= new Animal();
dolphin= new Animal();
}
public boolean cleanCage(int cage){
if(zookeeper.getEnergy()>0){
zookeeper.reduceEnergy(1);
System.out.println("Cage "+cage+" cleaned");
return true;
}
return false;
}
public int endDay(){
zookeeper.resetEnergy();
tiger.sleep(true);
elephant.sleep(true);
penguin.sleep(true);
dolphin.sleep(true);
Random rand = new Random();
int cage = rand.nextInt(4) + 1;
feedAnimal(cage, ZooPantry.AllowableFoods.Meat);
return 0;
}
public boolean feedAnimal(int cage, ZooPantry.AllowableFoods foodId){
if(zookeeper.getEnergy()>0){
if(cage==DOLPHINLOCATION && ZooPantry.foodAmountLeft[foodId.ordinal()] > 0){
zookeeper.reduceEnergy(1);
ZooPantry.foodAmountLeft[foodId.ordinal()]--;
dolphin.feed();
}
else if(cage==ELEPHANTLOCATION && ZooPantry.foodAmountLeft[foodId.ordinal()] > 0){
zookeeper.reduceEnergy(1);
ZooPantry.foodAmountLeft[foodId.ordinal()]--;
elephant.feed();
}
else if(cage==PENGUINLOCATION && ZooPantry.foodAmountLeft[foodId.ordinal()] > 0){
zookeeper.reduceEnergy(1);
ZooPantry.foodAmountLeft[foodId.ordinal()]--;
penguin.feed();
}
else if(cage==TIGERLOCATION && ZooPantry.foodAmountLeft[foodId.ordinal()] > 0){
zookeeper.reduceEnergy(1);
ZooPantry.foodAmountLeft[foodId.ordinal()]--;
tiger.feed();
}
return true;
}
return false;
}
public boolean giveTigerBall(){
if(zookeeper.getEnergy()>0){
zookeeper.reduceEnergy(1);
System.out.println("Given ball to tiger");
return true;
}
return false;
}
public boolean swimWithDolphin(){
if(zookeeper.getEnergy()>0){
zookeeper.reduceEnergy(1);
System.out.println("Swimming with dolphin");
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.