Question: In this problem you will use the design pattern for maintaining state. Write a Toddler class. A Toddler has 4 states (state will be the
In this problem you will 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 eat and become CRANKY. If it sleeps some more, it will become SOMEWHAT_CRANKY. If it is HAPPY, 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". Provide Javadoc. the tester: /** * Tester for the Toddler class */ public class ToddlerTester { public static void main(String[] args) { Toddler toddler = new Toddler(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 4 Cranky"); toddler.run(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 7 Very cranky"); toddler.run(); toddler.sleep(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 4 Cranky"); toddler.sleep(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 2 Somewhat cranky"); toddler.sleep(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 1 Happy"); toddler.sleep(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 1 Happy"); toddler.run(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 2 Somewhat cranky"); toddler.run(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 4 Cranky"); toddler.run(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 7 Very cranky"); toddler.run(); System.out.println("state: " + toddler.getState() + " " + toddler.getMood()); System.out.println("Expected: 7 Very cranky"); } } codecheck: http://www.codecheck.it/files/18032406126gd2na0crp54zfkqnswd5t0zn
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
