Object-Oriented Design and Programming II CS262 Finite State Machines Getting Oriented • Visit the class web page to access the syllabus, slides, handouts, and grades. – http://www.uwosh.edu/faculty_staff/huen/262/s10262.htm - Pick up code handouts from the shared drive on the Novell network (Soft Q drive): For example: – • Submit your work to the shared drive: For example: Q:\Shared\Huen\262\Submit\Lab01\ Read the UWO Student Discipline Code (UWS 14) at • – • • Q:\Shared\Huen\262\Labs\S10_Labs http://www.uwosh.edu/dean/conduct.htm Textbook companion site: http://cs.armstrong.edu/liang/intro6e/index.html (The contents of chapters 1 through 6 were covered in CS221) BlueJ software documentation: http://www.bluej.org/doc/documentation.html Object-oriented analysis and design For Thursday: Review chapter 7 of our textbook • Objects • Each object has a unique identity that distinguishes it from every other object Examples: ceiling fan, subway turnstile, gumball machine, cruise control unit • Objects have state and behaviors • What is the state of an object? • What do we mean by behavior? Classes • Similar objects are grouped in a single class – A class is a template or blueprint for all the objects that share state and behavior. – More precisely, instances (or objects) of a class share the same behaviors and the same set of states they could be in, but they have a different identity and (possibly) a different current state. – UML diagrams are used to specify state, behaviors, classes, and more. Finite-state machines • A UML class diagram is static. • The UML also has several dynamic models. – One of them is the FSM. FSM = states + transitions (event/action) • Ceiling fan example: Pull/change speed Pull/change speed Off Pull/change speed Pull/changed speed Low Med High Observation • A state-transition diagram is a visual representation of the logic of the FSM One Java implementation of the ceiling fan example (Furcy) Q:\Shared\Huen\262\LectureNotes\F09 Slides\week01 Output in pulling the cord 10 times Turning slowly Turning at medium speed Turning fast Stopped turning Turning slowly Turning at medium speed Turning fast Stopped turning Turning slowly Turning at medium speed Discussion Question: What will happen if you omit one of the break statements in CeilingFan.java? Subway turnstile example • (From Robert C. Martin, Engineering Notebook Column, June 98) Coin/unlock Locked Unlocked turn/lock Exercise: Implement this FSM for next time Vending Machine Example A control system has to control the amount of money dropped into a vending machine. To keep the example simple, let’s restrict the inputs: only 5 and 10 cent coins are accepted. The correct, recognized sum is 25 cents. 10 Five Fifteen 10 5 5 Start 5 5 Stop 10 5 10 Ten Twen -ty A State Machine Diagram for the vending machine counter Table 1. Transition matrix for the vending machine counter. Inputs are in the cells To Start Five Ten Fifteen Twenty Stop From Start - 5 10 - - - Five - - 5 10 - - Ten - - - 5 10 - Fifteen - - - - 5 10 Twenty - - - - - 5, 10/change Stop - - - - - - Alternative Form of the Transition Matrix Top row contains input Other rows are states Input 5 10 State Start Five Ten Five Ten Fifteen Ten Fifteen Twenty Fifteen Twenty Stop Twenty Stop Stop + change Stop Stop Stop A Gumball Machine • States – – – – No_quarter Has_quarter Sold Sold-out • Actions – – – – – Insert_quarter Eject_quarter Turn_crank Dispense Refill Finite System Machine for Syllable Recognizer Not in VG Not vowel Not vowel VG: Vowel Group Initial state Vowel / count++ In VG Vowel Cruise Control as a Finite State Machine Controls: ON, OFF buttons. SET, (DEC, ACC) buttons. SET / set current speed ON Ready Q0 Cruise ON Brake OFF OFF, SET,DEC, ACC/ null How about “decelerate” and “accelerate”? OFF