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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CS5371 Theory of Computation
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.
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
26-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CS1101: Programming Methodology Aaron Tan.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Functions Definition & purpose Notation Functions on binary / returning binary values Finite automaton model We haven’t completely left the world of counting.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Loops and Iteration for Statements, while Statements and do-while Statements.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
©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.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Tokenizers 29-Nov-15. Tokens A tokenizer is a program that extracts tokens from an input stream A token is a “word” or a significant punctuation mark.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
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.
19-Dec-15 Tokenizers. Tokens A tokenizer is a program that extracts tokens from an input stream A token has two parts: Its value—this is just the characters.
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.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
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,
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Introduction to Exceptions in Java CS201, SW Development Methods.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Building Java Programs
Repetition (While-Loop) version]
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
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.
Finite State Machines Part I
Counted Loops.
Lecture 4B More Repetition Richard Gesick
Lesson 2: Building Blocks of Programming
Electronics II Physics 3620 / 6620
Chapter 2 Edited by JJ Shepherd
Chapter 3 - Structured Program Development
Looping III (do … while statement)
Tokenizers 25-Feb-19.
State Machines 6-Apr-196-Apr-19.
Tokenizers 26-Apr-19.
Tokenizers 3-May-19.
State Machines 8-May-19.
State Machines 16-May-19.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Lecture 18 Compilers and Language Translation (S&G, ch. 9)
Arrays Introduction to Arrays Reading for this Lecture:
Problem 1 Given n, calculate 2n
Presentation transcript:

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 has some number of states, and transitions between those states Transitions occur because of inputs State machines are useful when you need to respond to a sequence 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 A state machine may produce output as it goes along Alternatively, the “output” may be what state it ends up in

3 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

4 Simplifying drawings I Some state machines may have a error state with the following characteristics: An illegal input will cause a transition to the error state All subsequent inputs cause the state machine to remain in the error state 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 inputs” This is a convention when drawing the machine—it does not mean we look for an asterisk in the input

5 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 ) **** ( *

6 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

7 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

8 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

9 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

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

11 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;

12 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

13 The End Eclipse 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 with...