// A Parent object represents a parent who is driving children to // an exciting place (such as DisneyLand). The children ask the // parent annoying questions such as, "Are we there yet?" and the // parent becomes progressively more annoyed. // // This is a pretty lame example, but we are showing it as an example // of an object with persistent state that must live across multiple // calls to a method (areWeThereYet) and must behave differently on // each call to the method. public class Parent { // data fields private int timesCalled; private String name; // Constructs a new Parent with the given name. public Parent(String theName) { timesCalled = 0; name = theName; } // Returns a response to the question, "Are we there yet?" // Returns a progressively more annoyed response each time. public String areWeThereYet() { timesCalled++; if (timesCalled == 1 || timesCalled == 2) { return name + ": \"Just a little farther.\""; } else if (timesCalled == 3 || timesCalled == 4) { return name + ": \"NO!\""; } else if (timesCalled == 5) { return name + ": \"STOP ASKING ME THAT!!\""; } else { return name + ": \"That's it, you're grounded.\""; } } }