Question
Need help with Java Polymorphism and Inheritance Only need the help with the Fish, Furit, Grass, Meat, Seeds, Animal, Dolphin, Elephant, and Zoo classes Theyve
Need help with Java Polymorphism and Inheritance
Only need the help with the Fish, Furit, Grass, Meat, Seeds, Animal, Dolphin, Elephant, and Zoo classes
Theyve all been annotated to help with understanding where im trying to go
Zoo
package zoo;
/**
* 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.
*/
public class Zoo extends java.lang.Object
{
/**
* The cage number for the Elephant in the zoo.
*/
public static final int ELEPHANTLOCATION = 0;
/**
* The cage number for the Dolphin in the zoo.
*/
public static final int DOLPHINLOCATION = 0;
/**
* The cage number for the Penguin in the zoo.
*/
public static final int PENGUINLOCATION = 0;
/**
* The cage number for the Tiger in the zoo.
*/
public static final int TIGERLOCATION = 0;
/**
* 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.
*/
public Zoo()
{
}
/**
* 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
* @return String of current state of the game.
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
/**
* 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.
* @return Total score for the end of the current day, which is the sum of each Animal's score for the day.
*/
public int endDay()
{
int result = 0;
return result;
}
/**
* 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.
*
* @param cage Cage location of Animal to feed, must be a valid cage location
* per the public constants defined in by the Zoo.
* @param foodId The kind of Food to feed the Animal in the specified cage.
* @return Success of feeding Animal. Should be false if there is sufficient food,
* the keeper lacks enough energy or if the cage value is invalid.
*/
public boolean feedAnimal(int cage,
ZooPantry.AllowableFoods foodId)
{
boolean result = false;
return result;
}
/**
* 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.
*
* @param cage Cage location to be cleaned,
* must be a valid cage location per the public constants defined in by the Zoo.
* @return Success of cleaning the cage.
* False if the keeper lacks sufficient energy to clean or
* if the cage location specified is invalid.
*/
public boolean cleanCage(int cage)
{
boolean result = false;
return result;
}
/**
* Keeper Swims with the Dolphin if sufficient energy is available and
* updates state information of both ZooKeeper and Dolphin accordingly.
* @return True if the keeper was able to swim with the dolphin successfully, false otherwise.
*/
public boolean swimWithDolphin()
{
boolean result = false;
return result;
}
/**
* 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.
* @return Returns true if the Keeper was able to give the ball; false if not.
*/
public boolean giveTigerBall()
{
boolean result = false;
return result;
}
}
ZooKeeper
package zoo;
import zoo.foods.Food;
/**
* Provides all energy-tracking functionality for the zoo keeper in our
* simulation.
*
*
NOTE: YOU DO NOT NEED TO EDIT THIS FILE.
*
*/
public class ZooKeeper
{
/**
* Maximum energy the ZooKeeper can have.
*/
private static final int MAXKEEPERENERGY = 100;
/**
* Energy cost of cleaning an Animal's cage.
*/
private static final int CLEANCAGEENERGYCOST = 30;
/**
* Energy cost of swimming.
*/
private static final int SWIMENERGYCOST = 40;
/**
* Energy cost of giving a ball.
*/
private static final int GIVEBALLENERGYCOST = 20;
/**
* Current available energy of the Zoo Keeper.
*/
private int keeperEnergy;
/**
* Creates a new ZooKeeper with the maximum
* daily energy available.
*/
public ZooKeeper()
{
reset(); // Let the reset method handle energy.
}
/**
* Retrieves the current energy level for this ZooKeeper.
*
* @return the current available energy units
*/
public int getEnergy()
{
return keeperEnergy;
}
/**
* Resets the ZooKeeper's available energy to the maximum.
*/
public void reset()
{
keeperEnergy = MAXKEEPERENERGY;
}
/**
* Updates the ZooKeeper to reflect the energy expended when feeding
* a given Food type.
*
* @param f The Food to feed.
*/
public void feed(Food f)
{
if (canFeed(f))
{
keeperEnergy -= f.getExhaustionCost();
}
}
/**
* Updates the ZooKeeper to reflect the energy expended when cleaning
* an Animal enclosure.
*/
public void clean()
{
if (canClean())
{
keeperEnergy -= CLEANCAGEENERGYCOST;
}
}
/**
* Updates the ZooKeeper to reflect the energy expended when swimming with an Animal.
*/
public void swim()
{
if (canSwim())
{
keeperEnergy -= SWIMENERGYCOST;
}
}
/**
* Updates the ZooKeeper to reflect the energy expended when playing ball with an Animal.
*/
public void playBall()
{
if (canPlayBall())
{
keeperEnergy -= GIVEBALLENERGYCOST;
}
}
/**
* Predicate method to determine if this ZooKeeper has enough energy to feed a given Food.
*
* @param f The Food to feed.
* @return true if and only if this Zookeeper has sufficient energy. False otherwise.
*/
public boolean canFeed(Food f)
{
return hasEnoughEnergy(f.getExhaustionCost());
}
/**
* Predicate method to determine if this ZooKeeper has enough energy to clean an enclosure.
*
* @return true if and only if this Zookeeper has sufficient energy. False otherwise.
*/
public boolean canClean()
{
return hasEnoughEnergy(CLEANCAGEENERGYCOST);
}
/**
* Predicate method to determine if this ZooKeeper has enough energy to swim with an Animal.
*
* @return true if and only if this Zookeeper has sufficient energy. False otherwise.
*/
public boolean canSwim()
{
return hasEnoughEnergy(SWIMENERGYCOST);
}
/**
* Predicate method to determine if this ZooKeeper has enough energy to play ball with an Animal.
*
* @return true if and only if this ZooKeeper has sufficient energy. False otherwise.
*/
public boolean canPlayBall()
{
return hasEnoughEnergy(GIVEBALLENERGYCOST);
}
/**
* Helper predicate method used to verify whether this ZooKeeper has at least a given
* amount of energy to expend.
*
* @param cost The amount of energy needed.
* @return True if and only if cost <= this ZooKeepers available energy. False otherwise.
*/
private boolean hasEnoughEnergy(int cost)
{
return keeperEnergy >= cost;
}
}
ZooPantry
package zoo;
import zoo.foods.Food;
import zoo.foods.Seeds;
import zoo.foods.Meat;
import zoo.foods.Grass;
import zoo.foods.Fish;
import zoo.foods.Fruit;
/**
* Provides an abstraction for Food storage in the Zoo of our simulation.
*
*
NOTE: YOU DO NOT NEED TO EDIT THIS FILE.
*
*/
public class ZooPantry
{
/**
* This enumerated type provides a list of the foods which are allowed
* in our ZooPantry. This will correspond to the set of concrete foods
* defined in zoo.foods.
*/
public enum AllowableFoods
{
/**
* Corresponds to the Fish food type.
*/
FISH,
/**
* Corresponds to the Fruit food type.
*/
FRUIT,
/**
* Corresponds to the Grass food type.
*/
GRASS,
/**
* Corresponds to the Meat food type.
*/
MEAT,
/**
* Corresponds to the Seeds food type.
*/
SEEDS
}
/**
* Used to represent the places where Foods are stored in the pantry. It
* will contain appropriate Food sub-type object references at index values
* provided by the AllowableFoods datatype.
*/
private Food[] foodCrates;
/**
* Creates a newly stocked FoodPantry containing all available Foods.
*/
public ZooPantry()
{
foodCrates = new Food[getNumFoods()];
foodCrates[getIndex(AllowableFoods.SEEDS)] = new Seeds();
foodCrates[getIndex(AllowableFoods.MEAT)] = new Meat();
foodCrates[getIndex(AllowableFoods.GRASS)] = new Grass();
foodCrates[getIndex(AllowableFoods.FISH)] = new Fish();
foodCrates[getIndex(AllowableFoods.FRUIT)] = new Fruit();
}
/**
* Randomly selects a Food in this FoodPantry to receive a delivery
* of additional stock.
*/
public void randomDelivery()
{
int whichCrate = (int) (Math.random() * foodCrates.length);
foodCrates[whichCrate].receiveDelivery();
}
/**
* Retrieves a reference to the Food object that corresponds
* to the AllowableFood indicated.
*
* @param f One of the foods allowed in this pantry.
* @return The corresponding Food subclass object from this pantry.
*/
public Food getFood(AllowableFoods f)
{
return foodCrates[getIndex(f)];
}
/**
* Produces a String representation of this FoodPantry with
* the contents of all its stored Food objects, followed by
* a newline.
*
* @return the formatted String representation
*/
public String toString()
{
String ret = "***FOOD CRATES*** ";
for (int x = 0; x < foodCrates.length; x++)
{
ret += foodCrates[x] + " ";
}
return ret;
}
/**
* Helper method that converts an AllowableFoods value into
* a corresponding index value ranging from 0 to getMaxFoods() - 1.
*
* @param f The AllowableFood to be indexed.
* @return the corresponding index value.
*/
private int getIndex(AllowableFoods f)
{
return f.ordinal(); //nothing fancy, just rely on Enum's ordinal method.
}
/**
* Helper method to retrieve the total number of allowed food types
* in this pantry.
*
* @return A non-negative number of foods stored in this pantry.
*/
private int getNumFoods()
{
return AllowableFoods.values().length;
}
}
Animal
package zoo.animals;
/**
* Abstract class representing the base requirements for an Animal.
* Animals have happiness and hunger properties.
* Happiness is measured on a scale of 0 (very unhappy) to 100 (maximum happy), inclusive.
* Hunger is measured on an inverted scale of 100 (not hungry at all) to 0 (very hungry), inclusive.
*/
public class Animal extends java.lang.Object
{
/**
* Animal Constructor, creates a new animal that starts of with maximum hunger and happiness values
* (100 is the max for both).
* It also will use the provided parameters as the values for adjusting state
* during sleep and cage cleaning in the future.
* @param sleepHungerIn Amount of hunger increases while sleeping.
* @param sleepHappinessIn Amount of happiness lost when sleeping.
* @param cleanHappinessIn Amount of happiness gained when cage is cleaned.
*/
public Animal(int sleepHungerIn,
int sleepHappinessIn,
int cleanHappinessIn)
{
}
/**
* Updates the food behaviors for this animal object based on the provided mappings.
* @param foodBehaviorsIn Describes the nutrition and happiness behaviors of this animal.
*/
public void setFoodBehaviors(FoodMappings foodBehaviorsIn)
{
}
/**
* Retrieves the current hunger value of the Animal.
* Hunger is measured on an inverted scale of 100 (not hungry at all) to 0 (most hungry possible), inclusive.
* @return Animal's hunger value.
*/
public int getHunger()
{
}
/**
* Retrieves the current happiness value of the Animal.
* Happiness is measured on a scale of 0 (very unhappy) to 100 (maximum happy), inclusive.
* @return Animal's happiness value.
*/
public int getHappiness()
{
}
/**
* Getter to retrieve the amount the change in hunger this Animal experiences during sleep.
* @return sleep hunger value of the Animal.
*/
public int getSleepHunger()
{
}
/**
* Getter to retrieve the amount of change in happiness this Animal experiences during sleep.
* @return sleep happiness value of the Animal.
*/
public int getSleepHappiness()
{
}
/**
* Getter to retrieve the amount of change in happiness this Animal experiences when getting a cleaned cage.
* @return happiness from cleaning value of the Animal.
*/
public int getCleanHappiness()
{
}
/**
* String representation of the Animal. See example for format.
* Happiness and hunger values are formatted to width of 3.
* Overrides toString in class java.lang.Object
* @return String of the Animal.
*/
public java.lang.String toString()
{
}
/**
* Modifies this Animals' hunger by the amount specified.
* Resulting state will never be outside the range [0-100].
* @param hungerModifier Value to add to hunger.
*/
public void modifyHunger(int hungerModifier)
{
}
/**
* Modifies this Animals' happiness by the amount specified.
* Resulting state will never be outside the range [0-100].
* @param happinessModifier Value to add to happiness.
*/
public void modifyHappiness(int happinessModifier)
{
}
/**
* Method called to feed an Animal.
* Determines the specific type of Food being fed and modifies the Animal's hunger by multiplying the
* Food's nutrition value by the Animal's nutrition multiplier for that Food using the modifyHunger method.
* Modifies the Animal's happiness value by its happiness value for that Food.
* @param meal The Food being fed to the Animal.
*/
public void eat(Food meal)
{
}
/**
* The Animal sleeps for the night.
* To perform different actions for each animal.
* @return The score gained for the Animal for the day which is the sum of its hunger and happiness.
*/
public abstract int sleep()
{
}
/**
* The Animal's cage is cleaned.
* Different effects depending on the specific animal.
*/
public abstract void clean()
{
}
}
Dolphin
package zoo.animals;
/**
* The Dolphin class inherits from Animal.
* Adds specifics of a Dolphin.
*/
public class Dolphin extends Animal
{
/**
* Constructor of the Dolphin.
* Sets its Food nutrition and happiness values, sleep nutrition and happiness values, and cage cleaning values.
* These values are as follows:
* Fish Nutrition Multiplier: 1.0
Fruit Nutrition Multiplier: 1.0
Grass Nutrition Multiplier: .4
Meat Nutrition Multiplier: .8
Seeds Nutrition Multiplier: .2
Fish Happiness: 10
Fruit Happiness: 20
Grass Happiness: -10
Meat Happiness: 0
Seeds Happiness: -30
Sleep Hunger: -10
Sleep Happiness: -30
Clean Cage Happiness: 10
* These Magic values are allowed, but only in the constructor.
*/
public Dolphin()
{
}
/**
* The Dolphin sleeps for the day.
* Modifies hunger and happiness by its associated values.
* Returns the Dolphin's score for the day which is summation of happiness and hunger.
* @return Score for the day.
*/
public int sleep()
{
int result = 0;
return result;
}
/**
* Cleans the Dolphin's cage and modifies this Dolphin's happiness value by the amount specified in
* Animal's superclass definition.
*/
public void clean()
{
}
/**
* Zoo Keeper swims with this Dolphin, and its happiness is increased by 30 units.
*/
public void swim()
{
}
/**
* String representation of Dolphin.
* Returns a String with "Dolphin" formatted to 8 spaces left justified followed by a colon and a space,
* followed by current hunger and happiness values from Animal toString.
* See example output for formatting.
* Overrides toString in class Animal
* @return All Dolphin information as a String.
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
Elephant
package zoo.animals;
/**
* The Elephant class inherits from Animal.
* Adds specifics of a Elephant.
*
* In addition to food-related aspects, this class also models the fact that Elephants can get lonely.
* Loneliness ranges from 0 (not lonely at all) to 100 (the maximum loneliness), inclusive.
* Once an Elephant's loneliness exceeds the midpoint of 50 on this scale,
* there will be a negative daily effect on the Elephant's overall happiness.
*/
public class Elephant extends Animal
{
/**
* Constructor of the Elephant.
* Sets its Food nutrition and happiness values, sleep nutrition and happiness values, and cage cleaning values.
* These values are as follows:
* Fish Nutrition Multiplier: .4
Fruit Nutrition Multiplier: 1.0
Grass Nutrition Multiplier: 1.0
Meat Nutrition Multiplier: .7
Seeds Nutrition Multiplier: .5
Fish Happiness: -10
Fruit Happiness: 20
Grass Happiness: 0
Meat Happiness: -20
Seeds Happiness: 0
Sleep Hunger: -15
Sleep Happiness: -10
Clean Cage Happiness: 30
* Initially, newly created Elephant objects are not lonely at all.
*
* These Magic values are allowed, but only in the constructor.
*/
public Elephant()
{
}
/**
* The Elephant sleeps for the day.
* Modifies hunger and happiness by its Animal sleepHunger and sleepHappiness values.
* This Elephant's loneliness grows by 20 units while sleeping because there
* are no other Elephants or people around.
* Sleeping is also when the daily accumulated impact of loneliness will take its
* toll on the Elephant happiness for -20 units.
* @return the Animal's score for the day which is summation of happiness and hunger.
*/
public int sleep()
{
int result = 0;
return result;
}
/**
* Cleans the Elephants's cage.
* Happiness will grow by the Elephant's corresponding cleanHappiness value.
* The elephant also gets less lonely by 20 units because the ZooKeeper visited the cage.
*/
public void clean()
{
}
/**
* Retrieves this Elephant's current loneliness value,
* which ranges from 0 (not lonely at all) to 100 (maximum loneliness).
* @return The current loneliness value
*/
public int getLoneliness()
{
int result = 0;
return result;
}
/**
* String representation of Elephant.
* Returns a String with "Elephant" formatted to 8 spaces left justified followed by a colon and a space,
* then Loneliness value (formatted to a width of 3),
* followed by current hunger and happiness values from Animal toString.
* See example output for formatting.
* Overrides toString in class Animal
* @return All Elephant information as a String.
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
FoodMappings
package zoo.animals;
import zoo.ZooPantry.AllowableFoods;
/**
* This class is used to represent mappings between Food's allowed in
* a ZooPantry and corresponding nutrition multipliers and happiness
* modifiers.
*
* NOTE: YOU DO NOT NEED TO EDIT THIS FILE
*
*/
public class FoodMappings
{
/**
* An array of nutrition multipliers to be used when feeding
* an Animal.
*/
private double[] nutritionMultipliers;
/**
* An array of happiness modifiers to be used when feeding an
* Animal.
*/
private int[] happinessValues;
/**
* Constructs a FoodMappings object with all nutrition and happiness
* values initialized to 0.
*/
public FoodMappings()
{
//Get the number of options available
int numberFoods = AllowableFoods.values().length;
//Create a new array to hold the mappings
nutritionMultipliers = new double[numberFoods];
happinessValues = new int[numberFoods];
}
/**
* Sets the nutrition multiplier for a corresponding AllowableFoods
* position.
*
* @param f the type of food whose nutrition value is being set
* @param value the new value
*/
public void updateNutritionMultiplier(AllowableFoods f, double value)
{
nutritionMultipliers[f.ordinal()] = value;
}
/**
* Sets the happiness modifier for a corresponding AllowableFoods
* position.
*
* @param f the type of food whose happiness value is being set
* @param value the new value
*/
public void updateHappinessModifier(AllowableFoods f, int value)
{
happinessValues[f.ordinal()] = value;
}
/**
* Retrieves the nutrition multiplier for a given AllowableFoods position.
*
* @param f the type of food whose nutrition multiplier is requested
* @return the requested multiplier
*/
public double getNutritionMultiplier(AllowableFoods f)
{
return nutritionMultipliers[f.ordinal()];
}
/**
* Retrieves the happiness modifier for a given AllowableFoods position.
*
* @param f the type of food whose happiness modifier is requested.
* @return the requested modifier
*/
public int getHappinessModifier(AllowableFoods f)
{
return happinessValues[f.ordinal()];
}
}
Penguin
package zoo.animals;
import zoo.ZooPantry.AllowableFoods;
/**
* The Penguin class inherits from Animal. Adds specifics of a Penguin.
*
* NOTE: YOU DO NOT NEED TO EDIT THIS FILE
*/
public class Penguin extends Animal
{
/**
* Chance Penguin will lay an egg when sleeping.
*/
private static final double EGGCHANCE = .5;
/**
* Value of eggs in which Penguin will start losing happiness.
*/
private static final int EGGLIMIT = 3;
/**
* Amount of happiness lost when sleeping if EGGLIMIT reached.
*/
private static final int EGGHAPPINESSLOST = -30;
/**
* Amount of eggs the Penguin has laid.
*/
private int eggs;
/**
* Constructor of the Penguin. Sets its Food nutrition and happiness values, sleep nutrition
* and happiness values, and cage cleaning values. These values are as follows:
* Fish Nutrition Multiplier: .8
* Fruit Nutrition Multiplier: 1.0
* Grass Nutrition Multiplier: .65
* Meat Nutrition Multiplier: .3
* Seeds Nutrition Multiplier: 1.0
* Fish Happiness: 20
* Fruit Happiness: 20
* Grass Happiness: 0
* Meat Happiness: -15
* Seeds Happiness: 10
* Sleep Hunger: -10
* Sleep Happiness: -20
* Clean Cage Happiness: 20
* eggs initialized to 0.
* These Magic values are allowed in the constructor only.
*
*/
public Penguin()
{
//CHECKSTYLE:OFF: checkstyle:magicnumber
super(-10, -20, 20);
FoodMappings map = new FoodMappings();
map.updateNutritionMultiplier(AllowableFoods.FISH, .8);
map.updateHappinessModifier(AllowableFoods.FISH, 20);
map.updateNutritionMultiplier(AllowableFoods.FRUIT, 1.0);
map.updateHappinessModifier(AllowableFoods.FRUIT, 20);
map.updateNutritionMultiplier(AllowableFoods.GRASS, .65);
map.updateHappinessModifier(AllowableFoods.GRASS, 0);
map.updateNutritionMultiplier(AllowableFoods.MEAT, .3);
map.updateHappinessModifier(AllowableFoods.MEAT, -15);
map.updateNutritionMultiplier(AllowableFoods.SEEDS, 1.0);
map.updateHappinessModifier(AllowableFoods.SEEDS, 10);
super.setFoodBehaviors(map);
eggs = 0;
//CHECKSTYLE:ON: checkstyle:magicnumber
}
/**
* The Penguin sleeps for the day. Modifies hunger and happiness by its Animal sleepHunger and
* sleepHappiness values. Randomly determines if Penguin lays an egg while sleeping (50% chance).
* If this Penguin has too many eggs (3 or more), the Penguin will lose 30 happiness units.
*
* @return the Penguin's score for the day which is summation of happiness and hunger.
*/
@Override
public int sleep()
{
modifyHunger(getSleepHunger());
modifyHappiness(getSleepHappiness());
if (Math.random() >= EGGCHANCE)
{
eggs++;
}
if (eggs >= EGGLIMIT)
{
modifyHappiness(EGGHAPPINESSLOST);
}
return getHappiness() + getHunger();
}
/**
* Cleans the Penguins's cage. Happiness is modified by its cleanHappiness and all eggs are
* removed from the cage.
*/
public void clean()
{
modifyHappiness(getCleanHappiness());
eggs = 0;
}
/**
* Retrieve the number of eggs this Penguin currently has in the cage.
*
* @return the number of eggs
*/
public int getNumEggs()
{
return eggs;
}
/**
* String representation of Penguin. Returns a String with "Penguin" formatted to 8 spaces left
* justified followed by a colon and a space, followed by current hunger and happiness values
* from Animal toString. See example output for formatting.
* @return All Penguin information as a String.
*/
public String toString()
{
return String.format("%-8s: %s Eggs: %d", "Penguin", super.toString(), eggs);
}
}
Tiger
package zoo.animals;
import zoo.ZooPantry.AllowableFoods;
/**
* The Tiger class inherits from Animal. Adds specifics of a Tiger.
*
* NOTE: YOU DO NOT NEED TO EDIT THIS FILE
*/
public class Tiger extends Animal
{
/**
* Chance Tiger will break its ball when sleeping.
*/
private static final double BREAKBALLCHANCE = .7;
/**
* Amount of happiness lost when sleeping if ball is broke.
*/
private static final int BROKEBALLHAPPINESSLOST = -30;
/**
* The Tiger has a ball to play with, states whether or not it currently has its ball.
*/
private boolean hasBall;
/**
* Constructor of the Tiger. Sets its Food nutrition and happiness values, sleep nutrition
* and happiness values, and cage cleaning values. These values are as follows:
* Fish Nutrition Multiplier: .7
* Fruit Nutrition Multiplier: 1.0
* Grass Nutrition Multiplier: .5
* Meat Nutrition Multiplier: 1.0
* Seeds Nutrition Multiplier: .15
* Fish Happiness: 10
* Fruit Happiness: 20
* Grass Happiness: -10
* Meat Happiness: 20
* Seeds Happiness: -10
* Sleep Hunger: -20
* Sleep Happiness: -10
* Clean Cage Happiness: 20
* hasBall initialized to true.
* These Magic values are allowed in the constructor only.
*
*/
public Tiger()
{
//CHECKSTYLE:OFF: checkstyle:magicnumber
super(-20, -10, 20);
FoodMappings map = new FoodMappings();
map.updateNutritionMultiplier(AllowableFoods.FISH, .7);
map.updateHappinessModifier(AllowableFoods.FISH, 10);
map.updateNutritionMultiplier(AllowableFoods.FRUIT, 1.0);
map.updateHappinessModifier(AllowableFoods.FRUIT, 20);
map.updateNutritionMultiplier(AllowableFoods.GRASS, .5);
map.updateHappinessModifier(AllowableFoods.GRASS, -10);
map.updateNutritionMultiplier(AllowableFoods.MEAT, 1.0);
map.updateHappinessModifier(AllowableFoods.MEAT, 20);
map.updateNutritionMultiplier(AllowableFoods.SEEDS, .15);
map.updateHappinessModifier(AllowableFoods.SEEDS, -10);
super.setFoodBehaviors(map);
hasBall = true;
//CHECKSTYLE:ON: checkstyle:magicnumber
}
/**
* The Tiger sleeps for the day. Modifies hunger and happiness by its Animal sleepHunger and
* sleepHappiness values. Randomly determines if Tiger breaks its ball while sleeping (70%
* chance this is true). If ball is currently broken, this Tiger loses 30 happiness units.
*
* @return the Tiger's score for the day which is summation of happiness and hunger.
*/
@Override
public int sleep()
{
modifyHunger(getSleepHunger());
modifyHappiness(getSleepHappiness());
if (Math.random() >= BREAKBALLCHANCE)
{
hasBall = false;
}
if (!hasBall)
{
modifyHappiness(BROKEBALLHAPPINESSLOST);
}
return getHappiness() + getHunger();
}
/**
* New ball given to this Tiger.
*/
public void giveBall()
{
hasBall = true;
}
/**
* Cleans the Tiger's cage and updates happiness accordingly.
*/
public void clean()
{
modifyHappiness(getCleanHappiness());
}
/**
* String representation of Tiger. Returns a String with "Tiger" formatted to 8 spaces left
* justified followed by a colon and a space, followed by current hunger and happiness values
* from Animal toString. Finally, the "Has Ball" or "Broke Ball" is appended based upon hasBall.
* See example output for formatting.
* @return All Penguin information as a String.
*/
public String toString()
{
return String.format("%-8s: %s %s", "Tiger", super.toString(), hasBall ? "Has Ball" : "Broke Ball");
}
}
Fish
package zoo.foods;
/**
* The Fish class is a sub-class of Food.
*/
public class Fish extends Food
{
/**
* Fish constructor, calls upon super-class constructor with exhaustion of 40 and nutrition of 25.
* Valid use of magic numbers for the super-class constructor call.
*/
public Fish()
{
}
/**
* Returns String representation of Fish.
* Formats the String "Fish" with width of 5 spaces left justified followed by a colon,
* then space, followed by exhaustion cost, nutrition, and amount from Food toString().
* @return String
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
Food
package zoo.foods;
/**
* Abstract class which contains the requirements for all Food.
*
*
NOTE: YOU DO NOT NEED TO EDIT THIS FILE
*
*/
public abstract class Food
{
/**
* How much exhaustion is spent feeding an Animal the Food.
*/
private int exhaustionCost;
/**
* Base nutrition value of the Food.
*/
private int nutrition;
/**
* The amount of the Food that is in stock.
*/
private int amount;
/**
* Food constructor, sets exhaustionCost and nutrition to passed values. Sets amount to 1.
* @param exhaustionCostIn Exhaustion cost of the Food.
* @param nutritionIn Base nutrition of the Food.
*/
public Food(int exhaustionCostIn, int nutritionIn)
{
exhaustionCost = exhaustionCostIn;
nutrition = nutritionIn;
amount = 1;
}
/**
* Retrieves the exhaustion cost of the Food.
* @return Exhaustion cost of the Food.
*/
public int getExhaustionCost()
{
return exhaustionCost;
}
/**
* Retrieves the base nutrition of the Food.
* @return Nutrition value of the Food.
*/
public int getNutrition()
{
return nutrition;
}
/**
* Retrieves the current amount of the Food.
* @return How many units of this Food that are in stock.
*/
public int getAmount()
{
return amount;
}
/**
* A delivery of this Food is received, increases amount by 1.
*/
public void receiveDelivery()
{
amount++;
}
/**
* A unit of this food is being consumed, decreases amount by 1.
*/
public void consume()
{
amount--;
}
/**
* String representation of Food. See example output for format.
* @return String representation of Food giving exhaustion cost, nutrition, and current amount.
*/
public String toString()
{
return String.format("Exhaustion: %d Nutrition: %d Amount: %d", exhaustionCost, nutrition, amount);
}
}
Fruit
package zoo.foods;
/**
* The Fruit class is a sub-class of Food.
*/
public class Fruit extends Food
{
/**
* Fruit constructor, calls upon super-class constructor with exhaustion of 25 and nutrition of 10.
* Valid use of magic numbers for the super-class constructor call.
*/
public Fruit()
{
}
/**
* Returns String representation of Fruit.
* Formats the String "Fruit" with width of 5 spaces left justified followed by a colon,
* then space, followed by exhaustion cost, nutrition, and amount from Food toString().
* @return String
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
Grass
package zoo.foods;
/**
* The Grass class is a sub-class of Food.
*/
public class Grass extends Food
{
/**
* Grass constructor, calls upon super-class constructor with exhaustion of 30 and nutrition of 30.
* Valid use of magic numbers for the super-class constructor call.
*/
public Grass()
{
}
/**
* Returns String representation of Grass.
* Formats the String "Grass" with width of 5 spaces left justified followed by a colon,
* then space, followed by exhaustion cost, nutrition, and amount from Food toString().
* @return String
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
Meat
package zoo.foods;
/**
* The Meat class is a sub-class of Food.
*/
public class Meat extends Food
{
/**
* Meat constructor, calls upon super-class constructor with exhaustion of 50 and nutrition of 40.
* Valid use of magic numbers for the super-class constructor call.
*/
public Meat()
{
}
/**
* Returns String representation of Meat.
* Formats the String "Meat" with width of 5 spaces left justified followed by a colon,
* then space, followed by exhaustion cost, nutrition, and amount from Food toString().
* @return String
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
Seeds
package zoo.foods;
/**
* The Seeds class is a sub-class of Food.
*/
public class Seeds extends Food
{
/**
* Seeds constructor, calls upon super-class constructor with exhaustion of 25 and nutrition of 20.
* Valid use of magic numbers for the super-class constructor call.
*/
public Seeds()
{
}
/**
* Returns String representation of Seeds.
* Formats the String "Seeds" with width of 5 spaces left justified followed by a colon,
* then space, followed by exhaustion cost, nutrition, and amount from Food toString().
* @return String
*/
public java.lang.String toString()
{
String str = new String("");
return str;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started