Java need help writing this class Constructor and Description Animal(int sleepHu
ID: 3590410 • Letter: J
Question
Java need help writing this class
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
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.
Explanation / Answer
// Animal.java
package lab2b;
public class Animal {
private int cleanHappiness;
private int sleepHappiness;
private int sleepHunger;
private int hunger;
private int happiness;
FoodMappings fmaps=null;
public Animal( int sleepHungerIn, int sleepHappinessIn, int cleanHappinessIn) {
super();
this.cleanHappiness = cleanHappinessIn;
this.sleepHappiness = sleepHappinessIn;
this.sleepHunger = sleepHungerIn;
}
public int getHunger() {
return hunger;
}
public void setHunger(int hunger) {
this.hunger = hunger;
}
public int getHappiness() {
return happiness;
}
public void setHappiness(int happiness) {
this.happiness = happiness;
}
public int getCleanHappiness() {
return cleanHappiness;
}
public int getSleepHappiness() {
return sleepHappiness;
}
public int getSleepHunger() {
return sleepHunger;
}
public void setCleanHappiness(int cleanHappiness) {
this.cleanHappiness = cleanHappiness;
}
public void setSleepHappiness(int sleepHappiness) {
this.sleepHappiness = sleepHappiness;
}
public void setSleepHunger(int sleepHunger) {
this.sleepHunger = sleepHunger;
}
public void clean(){
}
public void modifyHappiness(int happinessModifier){
this.happiness=happinessModifier;
}
public void modifyHunger(int hungerModifier){
this.hunger=hungerModifier;
}
public void setFoodBehaviors(FoodMappings foodBehaviorsIn){
fmaps=foodBehaviorsIn;
int nutri= fmaps.getNutrition();
System.out.println("food nutrition and happiness: "+(nutri+this.happiness));
}
public int sleep(){
return 0;
}
public void eat(Food meal){
int food=meal.getMeal()*this.fmaps.getNutrition();
this.modifyHunger(food);
this.modifyHappiness(food);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + cleanHappiness;
result = prime * result + sleepHappiness;
result = prime * result + sleepHunger;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Animal other = (Animal) obj;
if (cleanHappiness != other.cleanHappiness)
return false;
if (sleepHappiness != other.sleepHappiness)
return false;
if (sleepHunger != other.sleepHunger)
return false;
return true;
}
@Override
public String toString() {
return "Animal [cleanHappiness=" + cleanHappiness + ", sleepHappiness=" + sleepHappiness + ", sleepHunger="
+ sleepHunger + "]";
}
}
// Food.java
package lab2b;
public class Food {
private int meal;
public int getMeal() {
return meal;
}
public void setMeal(int meal) {
this.meal = meal;
}
}
//FoodMappings.java
package lab2b;
public class FoodMappings {
private int nutrition;
public FoodMappings(int nutrition) {
super();
this.nutrition = nutrition;
}
public int getNutrition() {
return nutrition;
}
public void setNutrition(int nutrition) {
this.nutrition = nutrition;
}
}
//AnimalTest.java
package lab2b;
public class AnimalTest {
public static void main(String[] args) {
Animal animal=new Animal(100, 100,80);
animal.setHunger(0);
animal.setHappiness(100);
FoodMappings fmap=new FoodMappings(6); // max 10
animal.setFoodBehaviors(fmap);
Food food=new Food();
food.setMeal(5); // max 10
int hunger=animal.getHunger();
if(hunger==0) System.out.println("More Hunger");
if(hunger==100) System.out.println("Less Hunger");
int happy=animal.getHappiness();
if(happy==0) System.out.println("very Unhappy");
if(happy==100) System.out.println(" maximum Happy");
System.out.println(animal.getSleepHunger());
System.out.println(animal.getSleepHappiness());
System.out.println(animal.getCleanHappiness());
System.out.println(animal);
animal.modifyHunger(60);
animal.eat(food);
}
}
/*output:-
food nutrition and happiness: 106
More Hunger
maximum Happy
100
100
80
Animal [cleanHappiness=80, sleepHappiness=100, sleepHunger=100]
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.