public void run() the Toddler becomes more cranky if it is already VERY_CRANKY, the state does not change public void sleep() the Toddler becomes less cranky. If it is already HAPPY, the state does not change public int getState() Gets the integer representing the state public String getMood() Gets a string describing the current mood of the Toddler: "Happy", "Somewhat cranky,"Cranky", or "Very cranky"
use the design pattern for maintaining state. Write a Toddler class. A Toddler has 4 states. (state will be the only instance variable) You will define and use these static constants to represent the states.
- public static final int HAPPY = 1;
- public static final int SOMEWHAT_CRANKY = 2;
- public static final int CRANKY = 4;
- public static final int VERY_CRANKY = 7;
In your code do not assume what the value is for any of the constants.
A Toddler runs around all day, and as it runs, it becomes more tired and cranky. If it is HAPPY, it becomes SOMEWHAT_CRANKY. If it is SOMEWHAT_CRANKY, it becomes CRANKY and so on. When the Toddler sleeps, its state changes. If it is in any of the "cranky states", it will become one level less cranky. If the Toddler is VERY_CRANKY when it sleeps, it will become CRANKY. If it sleeps some more, it will become SOMEWHAT_CRANKY. If it is HAPPY and it sleeps, its state does not change.
The constructor takes no parameters. A Toddler is CRANKY when it is born so the constructor must initialize its state to CRANKY.
Provide methods:
- public void run() the Toddler becomes more cranky if it is already VERY_CRANKY, the state does not change
- public void sleep() the Toddler becomes less cranky. If it is already HAPPY, the state does not change
- public int getState() Gets the integer representing the state
- public String getMood() Gets a string describing the current mood of the Toddler: "Happy", "Somewhat cranky,"Cranky", or "Very cranky"
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images