Question: I need help implementing this Animal class public abstract class Animal extends java.lang.Object Abstract class representing the base requirements for an Animal. Animals have happiness
I need help implementing this Animal class
public abstract class Animal extends java.lang.Object
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.
Constructor Summary
Constructors Constructor and Description Animal(int sleepHungerIn, int sleepHappinessIn, int cleanHappinessIn)
Animal Constructor, creates a new animal that starts of with maximum hunger and happiness values (100 is the max for both).
Method Summary
All MethodsInstance MethodsAbstract MethodsConcrete Methods Modifier and Type Method and Description abstract void clean()
The Animal's cage is cleaned.
void eat(Food meal)
Method called to feed an Animal.
int getCleanHappiness()
Getter to retrieve the amount of change in happiness this Animal experiences when getting a cleaned cage.
int getHappiness()
Retrieves the current happiness value of the Animal.
int getHunger()
Retrieves the current hunger value of the Animal.
int getSleepHappiness()
Getter to retrieve the amount of change in happiness this Animal experiences during sleep.
int getSleepHunger()
Getter to retrieve the amount the change in hunger this Animal experiences during sleep.
void modifyHappiness(int happinessModifier)
Modifies this Animals' happiness by the amount specified.
void modifyHunger(int hungerModifier)
Modifies this Animals' hunger by the amount specified.
void setFoodBehaviors(FoodMappings foodBehaviorsIn)
Updates the food behaviors for this animal object based on the provided mappings.
abstract int sleep()
The Animal sleeps for the night.
java.lang.String toString()
String representation of the Animal.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
Animal
public Animal(int sleepHungerIn, int sleepHappinessIn, int cleanHappinessIn)
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.
Parameters:
sleepHungerIn - Amount of hunger increases while sleeping.
sleepHappinessIn - Amount of happiness lost when sleeping.
cleanHappinessIn - Amount of happiness gained when cage is cleaned.
Method Detail
setFoodBehaviors
public void setFoodBehaviors(FoodMappings foodBehaviorsIn)
Updates the food behaviors for this animal object based on the provided mappings.
Parameters:
foodBehaviorsIn - Describes the nutrition and happiness behaviors of this animal.
getHunger
public int getHunger()
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.
Returns:
Animal's hunger value.
getHappiness
public int getHappiness()
Retrieves the current happiness value of the Animal. Happiness is measured on a scale of 0 (very unhappy) to 100 (maximum happy), inclusive.
Returns:
Animal's happiness value.
getSleepHunger
public int getSleepHunger()
Getter to retrieve the amount the change in hunger this Animal experiences during sleep.
Returns:
sleep hunger value of the Animal.
getSleepHappiness
public int getSleepHappiness()
Getter to retrieve the amount of change in happiness this Animal experiences during sleep.
Returns:
sleep happiness value of the Animal.
getCleanHappiness
public int getCleanHappiness()
Getter to retrieve the amount of change in happiness this Animal experiences when getting a cleaned cage.
Returns:
happiness from cleaning value of the Animal.
toString
public java.lang.String toString()
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
Returns:
String of the Animal.
modifyHunger
public void modifyHunger(int hungerModifier)
Modifies this Animals' hunger by the amount specified. Resulting state will never be outside the range [0-100].
Parameters:
hungerModifier - Value to add to hunger.
modifyHappiness
public void modifyHappiness(int happinessModifier)
Modifies this Animals' happiness by the amount specified. Resulting state will never be outside the range [0-100].
Parameters:
happinessModifier - Value to add to happiness.
eat
public void eat(Food meal)
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.
Parameters:
meal - The Food being fed to the Animal.
sleep
public abstract int sleep()
The Animal sleeps for the night. To perform different actions for each animal.
Returns:
The score gained for the Animal for the day which is the sum of its hunger and happiness.
clean
public abstract void clean()
The Animal's cage is cleaned. Different effects depending on the specific animal
provided Food class
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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
