State MachinesWhat is a state machine?Example I: Even or oddSimplifying drawings IExample II: Nested parenthesisNested parentheses IINested parentheses IIIExample: Making numbers boldState machines in JavaOutline of the bold programThe two statesConclusionsThe EndJan 13, 2019State MachinesAbbreviated lecture2What is a state machine?A state machine is a different way of thinking about computationA state machine has some number of states, and transitions between those statesTransitions occur because of inputsState machines are useful when you need to respond to a sequence of inputsA “pure” state machine only knows which state it is in—it has no other memory or knowledgeThis is the kind of state machine you learn about in your math classesWhen you program a state machine, you don’t have that restrictionA state machine may produce output as it goes alongAlternatively, the “output” may be what state it ends up in3Example I: Even or oddThe following machine determines whether the number of As in a string is even or oddCircles represent states; arrows represent transitionsInputs are the characters of a stringThe “output” is the resultant stateAeven oddstartAanything but Aanything but A4Simplifying drawings ISome state machines may have a error state with the following characteristics:An illegal input will cause a transition to the error stateAll subsequent inputs cause the state machine to remain in the error stateWe can simplify the drawing by leaving out the error stateThe error state is still part of the machineAny input without a transition on our drawing is assumed to go to the error stateAnother simplification: Use * to indicate “all other inputs”This is a convention when drawing the machine—it does not mean we look for an asterisk in the input5Example II: Nested parenthesisThe following example tests whether parentheses are properly nested (up to 3 deep)How can we extend this machine to handle arbitrarily deep nesting?start)()()(OKError)* * * *(*6Nested parentheses IIQuestion: How can we use a state machine to check parenthesis nesting to any depth?Answer: We can’t (with a finite number of states)We need to count how deep we are into a parenthesis nest: 1, 2, 3, ..., 821, ...The only memory a state machine has is which state it is inHowever, if we aren’t required to use a pure state machine, we can add memory (such as a counter) and other features7Nested parentheses IIIThis machine is based on a state machine, but it obviously is not just a state machineOK( do count=1) and count==1do count=0( do count++) and count>1 do count--start8Example: Making numbers boldIn HTML, you indicate boldface by surrounding the characters with <b> ... </b>Suppose we want to make all the integers bold in an HTML page—we can write a state machine to do thisNORMAL NUMBERdigitoutput <b>digitnondigitoutput </b>nondigit*: output *end of inputoutput </b>startdigitoutput digitend9State machines in JavaIn a state machine, you can have transitions from any state to any other stateThis is difficult to implement with Java’s loops and if statementsThe trick is to make the “state” a variable, and to embed a switch (state) statement inside a loopEach case is responsible for resetting the “state” variable as needed to represent transitions10Outline of the bold program void run() { int state = NORMAL; for (int i = 0; i < testString.length(); i++) { char ch = testString.charAt(i); switch (state) { case NORMAL: { not inside a number } case NUMBER: { inside a number } } } if (state == NUMBER) result.append("</b>");11The two statescase NORMAL: if (Character.isDigit(ch)) { result.append("<b>" + ch); state = NUMBER; break; } else { result.append(ch); } break;case NUMBER: if (!Character.isDigit(ch)) { result.append("</b>" + ch); state = NORMAL; break; } else { result.append(ch); }break;12ConclusionsA state machine is a good model for a number of problemsYou can think of the problem in terms of a state machine but not actually do it that way (e.g. German vocabulary)You can implement the problem as a state machine (e.g. making integers bold) Best done as a switch inside some kind of loopPure state machines have some severe limitationsJava lets you do all kinds of additional tests and actions; you can ignore these limitations13The EndEclipse Hints ● To see older versions of a class or method, right-click its name and choose Local History Compare with... ● To go back to an older version of a class or method, right-click its name and choose Local History Replace
View Full Document