Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal.

Slides:



Advertisements
Similar presentations
Chapter 8 Improving the User Interface
Advertisements

CS107: Introduction to Computer Science Lecture 2 Jan 29th.
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 1: Overview of Computers & Programming
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Programming Types of Testing.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
ECS15 for and sequence. Comments  Another way to repeat.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Digital Logic Design Lecture 26. Announcements Exams will be returned on Thursday Final small quiz on Monday, 12/8. Final homework will be assigned Thursday,
Sequential circuit design
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 12: Exception Handling
High-Level Programming Languages: C++
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
General Programming Introduction to Computing Science and Programming I.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Lecture 2: Classes and Objects, using Scanner and String.
Lexical Analysis Hira Waseem Lecture
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.
Black Box Testing Techniques Chapter 7. Black Box Testing Techniques Prepared by: Kris C. Calpotura, CoE, MSME, MIT  Introduction Introduction  Equivalence.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
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.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Escape sequence Certain characters, preceded by a backslash ( \ ), are known as escape sequences They are used to display certain characters, or as display.
Lab 4 Instructions If you have a text book, you can go to drill at pp. 126 for steps 1-6 at the end of chapter 4, Or you can read following slides of the.
Introduction to Computing Systems and Programming Programming.
Introduction to Computer Programming using Fortran 77.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Computing Science and Programming I
CNG 140 C Programming (Lecture set 3)
Repetition Structures
Formatting Output.
Programming Fundamentals
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 2 Introduction to Programming
Formatting Output.
More Selections BIS1523 – Lecture 9.
Sequential circuit design
Exception Handling.
Sequential circuit design
Sequential circuit design
CS 3304 Comparative Languages
Fundamentals of Data Representation
Repetition Structures
ECE 352 Digital System Fundamentals
Chapter 10: Compilers and Language Translation
ECE 352 Digital System Fundamentals
EGR 2131 Unit 12 Synchronous Sequential Circuits
Software Development Techniques
Presentation transcript:

Lecture 20 The last function of reason is to recognize that there are an infinity of things which surpass it. Blaise Pascal

State machines Lets take a look at the programs that we’ve written so far. We should realize that we haven’t been to strict about erroneous input. Our programs lack fundamental error handling. What are some standard ways for dealing with this problems?

The phone conversion program Given a phone number in the letter form, print it out using digits. (This is one of the problems from last time quiz). We need to design a robust program which can perform this task.

Defining the input format First thing to do is to enforce user input. There are many different ways to format phone number, so just pick one: (XXX)XXX-XXXX We have area code in parenthesis then three digits, dash, then four last digits.

Observe the details to extract states. (XXX)XXX-XXXX Area code - enclosed in () First three digits are followed by ‘-’ Finally four last digits. What are the states and what is a state machine?

State machine State machine is a part of the program, usually used to keep track of where we are in the certain process. For example, in the phone number problem, we can be reading the area code, which is one state, or we can be reading first three digits of a phone number which is a different state.

State transition diagram S0 S1 S2 S3 a b c If you are in state 0 and you see a transition to state 1 If you are in state 0 and you see b transition to state 2 If you are in state 0 and you see c transition to state 3

Parsers (essentially a ‘format- enforcer’) - Read next input character - Look in what state you are and what is the character that you read. Decide if this character is a valid input at the moment, figure out what is the next state. If the input was not valid, issue an error message.

Phone number parser (XXX)XXX-XXXX States: 1. Init (didn’t see any input) 2. Area code (reading area code) 3. First three digits (reading first three digits) 4. Last four digits (reading last four digits)

Phone number parser (XXX)XXX-XXXX What are the transitions? When we see ‘(‘ transition from State 1 to State 2 (Init ----> read area code) When we see ‘)’ transition from State 2 to State 3 ( Read area code --> read first 3 digits)

Phone number parser (XXX)XXX-XXXX What are the transitions? When we see ‘-‘ transition from State 3 to State 4 ( Read first 3 digits --> read last four digits) The characters ‘(‘, ‘)’ and ‘-’ are signal characters that help us identify transitions.

How do we code the state machine? State machines are usually encoded using case statement, in pseudo code: case iState of State1 : do something State2 : do something State3 : do something etc.

Why use state machines? They provide logical, methodical approaches to sequential input processing. It is easier to design state machine, then have a a lot of if-then-else statements. Lets try to design the state machine for the phone number problem.

Phone number example state machine Init Area code Last four Letters Input is ‘(‘ Input is digit and count <= 3 First three letters Input is ’)’ and count = 4 Input is digit and count <= 3 Input is ’-’ and count = 4 (XXX)XXX-XXXX Input is digit In every state, if the input is not correct issue the error and halt.

Optional (hard) problem for next time Write a program that converts letter phone numbers into digit phone numbers using the state machine that we discussed.

While loops We have already encountered construct in Pascal which are not necessary but convenient. For example case statement. Pascal provides two more for-loop-like constructs of which we will learn one.

Format While ( ) DO BEGIN … END; { of while loop }

While loop This simply allows us to perform set of operations while certain condition is true. Why is this useful? Lets see an example.

While loop is actually more powerful than for-loop Can we simulate while-loop with for-loop? Can we simulate for-loop with while loop?

While loop is actually more powerful than for-loop We can’t simulate while loop with for-loop because we will not know when we should terminate. On the other hand, we can simulate for-loop with while loop like this:

Simulating the for-loop with while loop For I := 1 to 10 do … I := 1; while ( I != 10 ) do begin … I := I + 1; end;

Home work Program 6 is due Monday, November 30 Read chapter 8, sections 1,2 ignore whatever you don’t understand. Attempt the optional problem (spend at least 1/2 hour thinking about it)

Program from this lecture Count words