16
Object-Oriented Design and Programming II CS262 Finite State Machines

Object-Oriented Design and Programming II CS262 Finite State Machines

Embed Size (px)

Citation preview

Page 1: Object-Oriented Design and Programming II CS262 Finite State Machines

Object-Oriented Design and Programming II

CS262

Finite State Machines

Page 2: 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:

– Q:\Shared\Huen\262\Labs\S10_Labs

• Submit your work to the shared drive: For example: Q:\Shared\Huen\262\Submit\Lab01\

• Read the UWO Student Discipline Code (UWS 14) at – 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

Page 3: Object-Oriented Design and Programming II CS262 Finite State Machines

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?

Page 4: Object-Oriented Design and Programming II CS262 Finite State Machines

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.

Page 5: Object-Oriented Design and Programming II CS262 Finite State Machines

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:

Off Low

Pull/change speed Pull/changed speed

Med High

Pull/change speed

Pull/change speed

Page 6: Object-Oriented Design and Programming II CS262 Finite State Machines

• A state-transition diagram is a visual representation of the logic of the FSM

Observation

Page 7: Object-Oriented Design and Programming II CS262 Finite State Machines

One Java implementation of the ceiling fan example (Furcy)

Q:\Shared\Huen\262\LectureNotes\F09 Slides\week01

Output in pulling the cord 10 timesTurning slowlyTurning at medium speedTurning fastStopped turningTurning slowlyTurning at medium speedTurning fastStopped turningTurning slowlyTurning at medium speed

Page 8: Object-Oriented Design and Programming II CS262 Finite State Machines

Discussion

Question: What will happen if you omit one of the break statements in CeilingFan.java?

Page 9: Object-Oriented Design and Programming II CS262 Finite State Machines

Subway turnstile example

• (From Robert C. Martin, Engineering Notebook Column, June 98)

Locked Unlocked

Coin/unlock

turn/lock

Exercise: Implement this FSM for next time

Page 10: Object-Oriented Design and Programming II CS262 Finite State Machines

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.

Page 11: Object-Oriented Design and Programming II CS262 Finite State Machines

Start

Five

Ten

Fif-teen

Twen-ty

Stop

5

10

5

10

5

10

5

10

5

A State Machine Diagram for the vending machine counter

Page 12: Object-Oriented Design and Programming II CS262 Finite State Machines

Table 1. Transition matrix for the vending machine counter. Inputs are in the cells

ToFrom

Start Five Ten Fifteen Twenty Stop

Start - 5 10 - - -

Five - - 5 10 - -

Ten - - - 5 10 -

Fifteen - - - - 5 10

Twenty - - - - - 5, 10/change

Stop - - - - - -

Page 13: Object-Oriented Design and Programming II CS262 Finite State Machines

Alternative Form of the Transition MatrixTop row contains inputOther rows are states

InputState

5 10

Start Five Ten

Five Ten Fifteen

Ten Fifteen Twenty

Fifteen Twenty Stop

Twenty Stop Stop + change

Stop Stop Stop

Page 14: Object-Oriented Design and Programming II CS262 Finite State Machines

A Gumball Machine• States

– No_quarter– Has_quarter– Sold– Sold-out

• Actions– Insert_quarter– Eject_quarter– Turn_crank– Dispense– Refill

Page 15: Object-Oriented Design and Programming II CS262 Finite State Machines

Finite System Machine for Syllable Recognizer

Not in VG

In VG Vowel

Not vowel

Not vowel

Vowel / count++

Initial state

VG: Vowel Group

Page 16: Object-Oriented Design and Programming II CS262 Finite State Machines

Cruise Control as a Finite State Machine

Controls:ON, OFF buttons.SET, (DEC, ACC) buttons.

Q0 ReadyCruiseON

ON

OFF

SET / set current speed

Brake

OFFOFF, SET,DEC, ACC/ null

How about “decelerate” and “accelerate”?