14-Jun-15 State Machines. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
25-Jun-15 State Machines. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number.
1 Intro to Finite Automata Chapter 2 introduces the concept of a Finite Automata (or FA). An FA has several properties: It is theoretical. It allows computer.
CS 201 Functions Debzani Deb.
14-Jul-15 State Machines Abbreviated lecture. 2 What is a state machine? A state machine is a different way of thinking about computation A state machine.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Input, Output, and Processing
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
1 Problem Solving We now have enough tools to start solving some problems. For any problem, BEFORE you start writing a program, determine: –What are the.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
13-Nov-1513-Nov-1513-Nov-15 State Machines. What is a state machine? A state machine is a different way of thinking about computation A state machine.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
1 CS161 Introduction to Computer Science Topic #8.
Exceptions and Assertions Chapter 15 – CSCI 1302.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process 
INTRO TO STATE MACHINES CIS 4350 Rolf Lakaemper. State Machines A state machine represents a system as a set of states, the transitions between them,
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Introduction to Exceptions in Java CS201, SW Development Methods.
Topics for today: 1.Comments 2.Data types 3.Variable declaration.
Govt. Engineering College- Gandhinagar. It is all about……  STATE MACHINE.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Chapter 4 C Program Control Part I
Repetition (While-Loop) version]
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Even/odd parity (1) Computers can sometimes make errors when they transmit data. Even/odd parity: is basic method for detecting if an odd number of bits.
Counted Loops.
Lecture 4B More Repetition Richard Gesick
Finite Automata Motivation An Example.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 Control structures and Loops
Lesson 2: Building Blocks of Programming
Electronics II Physics 3620 / 6620
Chapter 2 Edited by JJ Shepherd
Chapter 5 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
State Machines 6-Apr-196-Apr-19.
Program Flow.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
State Machines 8-May-19.
State Machines 16-May-19.
Variables in C Topics Naming Variables Declaring Variables
Exception Handling.
Problem 1 Given n, calculate 2n
Chapter 13 Control Structures
Presentation transcript:

14-Jun-15 State Machines

2 What is a state machine? A state machine is a different way of thinking about computation A state machine has some number of states, and transitions between those states Transitions occur because of inputs A “pure” state machine only knows which state it is in—it has no other memory or knowledge This is the kind of state machine you learn about in your math classes When you program a state machine, you don’t have that restriction

3 State machine I/O State machines are designed to respond to a sequence of inputs, such as The individual characters in a string A series of external events State machines may produce output (often as a result of transitions) Alternatively, the “result” of a state machine may be the state it ends up in

4 Example I: Even or odd The following machine determines whether the number of A s in a string is even or odd Circles represent states; arrows represent transitions Inputs are the characters of a string The “output” is the resultant state A evenodd start A anything but A

5 Error states Some state machines may have a error state with the following characteristics: An unexpected input will cause a transition to the error state All subsequent inputs cause the state machine to remain in the error state

6 Simplifying drawings I State machines can get pretty complicated We can simplify the drawing by leaving out the error state The error state is still part of the machine Any input without a transition on our drawing is assumed to go to the error state Another simplification: Use * to indicate “all other characters” This is a convention when drawing the machine—it does not mean we look for an asterisk in the input

7 Example II: Nested parenthesis The following example tests whether parentheses are properly nested (up to 3 deep) How can we extend this machine to handle arbitrarily deep nesting? start ) ( ) ( ) ( OK Error ) **** ( *

8 Nested parentheses II Question: 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 in However, if we aren’t required to use a pure state machine, we can add memory (such as a counter) and other features

9 Nested parentheses III This machine is based on a state machine, but it obviously is not just a state machine OK ( do count=1 ) and count==1 do count=0 ( do count++ ) and count>1 do count-- start

10 The states of a Thread A Thread is an object that represents a single flow of execution through a program A Thread’s lifetime can be described by a state machine ready waiting runningdead start

11 A Memorization program A program to help people memorize paired-associates might be designed like this: Some stimulus is presented The user has to type in a response A paired associate is considered “learned” when the user answers correctly three times in a row Each item (stimulus + response) can be thought of as a state machine Here, however, we are looking at states of data rather than states of a program

12 Item states What’s the value in thinking of this as a state machine? Answer: When I wrote one of these for real use, I was able to adjust the difficulty of the study session by measuring the percentage correct from each state. 0th1st2nd3rd right start wrong out

13 Example: Making numbers bold In HTML, you indicate boldface by surrounding the characters with... Suppose we want to make all the integers bold in an HTML page—we can write a state machine to do this NORMALNUMBER digit output digit nondigit output nondigit *: output * end of input output start digit output digit end

14 State machines in Java In a state machine, you can have transitions from any state to any other state This is difficult to implement with Java’s loops and if statements The trick is to make the “state” a variable, and to embed a switch ( state ) statement inside a loop Each case is responsible for resetting the “state” variable as needed to represent transitions

15 Outline 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(" ");

16 The two states case NORMAL: if (Character.isDigit(ch)) { result.append(" " + ch); state = NUMBER; break; } else { result.append(ch); } break; case NUMBER: if (!Character.isDigit(ch)) { result.append(" " + ch); state = NORMAL; break; } else { result.append(ch); } break;

17 Conclusions A state machine is a good model for a number of problems You 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 loop Pure state machines have some severe limitations Java lets you do all kinds of additional tests and actions; you can ignore these limitations

18 The End