Question: In this problem you will use the design pattern for maintaining state. Write a Zebra class. A Zebra has 4 states. You will define and
In this problem you will use the design pattern for maintaining state. Write a Zebra class. A Zebra has 4 states. You will define and use these static constants to represent the states.
public static final int NOT_HUNGRY = 1;
public static final int SOMEWHAT_HUNGRY = 2;
public static final int HUNGRY = 3;
public static final int VERY_HUNGRY = 4;
While in your code you can not assume what the value is for any of the constants, you can assume that they the values are consecutive integers. That is, VERY_HUNGRY will be 1 greater than HUNGRY, etc.
Zebras roam the Serengeti Plain in Tanzania.
(image from Wikipedea)
A Zebra runs across the plain and as it runs, it becomes more hungry. If it is NOT_HUNGRY, it becomes SOMEWHAT_HUNGRY. If it is SOMEWHAT_HUNGRY, it becomes HUNGRY and so on. When the Zebra sees food, if it is in any of the "hungry states", it will eat and become one level less hungry. If the Zebra is VERY_HUNGRY when it sees food, it will eat and become HUNGRY. The next time it sees food, it will eat again and become SOMEWHAT_HUNGRY. If it is NOT_HUNGRY, it does not eat and its state does not change.
The Zebra can not be less than NOT_HUNGRY or more than VERY_HUNGRY.
The constructor takes no parameters. A Zebra is very hungry when it is creates so the constructor must initialize the state to VERY_HUNGRY.
Provide methods:
public void run() the Zebra becomes more hungry if not already VERY_HUNGRY
public void seeFood() the Zebra will eat if it is hungry and become less hungry
public int getState() Gets the integer representing the state, an integer 1 through 4.
public String getHungerLevel() Gets a string describing the current hunger state of the Zebra: "Not hungry", "Somewhat hungry ,"Hungry", or "Very hungry"
Provide Javadoc
Please use this link to check for answer: http://www.codecheck.it/files/17080103161yveadoizt3xofaxyfo9cu8bp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
