State ● What is state? ABCDE. State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Types of Recursive Methods
VBA Modules, Functions, Variables, and Constants
CS2200 Software Development Class Design Example: The Time Class A. O’Riordan, 2008 (parts adapted from lecture notes by K. Brown)
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
The Clock Example. 1. The Clock Example 2 3 public class Clock { // instance variables private int hr; private int min; private int sec; // constructors:
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Refactoring Improving the structure of existing code Refactoring1.
Introduction to Software Testing Chapter 2.5 Graph Coverage for Specifications Paul Ammann & Jeff Offutt
6-1 Chapter 6 - Languages and the Machine Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer.
1 2-Hardware Design Basics of Embedded Processors (cont.)
Autumn 2012UCN T&B - IT/Computer Science1 State Pattern Implementation of State Machines State Pattern.
Refactoring1 Improving the structure of existing code.
Chapter 8: User-Defined Classes and ADTs
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
Data Types and Operations On Data Objective To understand what data types are The need to study data types To differentiate between primitive types and.
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Graph Coverage (5). Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Section
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CPS120: Introduction to Computer Science Variables and Constants.
Refactoring1 Improving the structure of existing code.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
Refactoring Constants and Variables Lesson Three: Constants and Variables.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Catalog of Refactoring (5) Simplifying Conditional Expressions.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Catalog of Refactoring
A variable is a name for a value stored in memory.
Paul Ammann & Jeff Offutt
Introduction Introduction to VHDL Entities Signals Data & Scalar Types
CS 153: Concepts of Compiler Design October 17 Class Meeting
Multiple variables can be created in one declaration
User-Defined Classes and ADTs
null, true, and false are also reserved.
CSE 3302 Programming Languages
Stack Memory 2 (also called Call Stack)
CSE401 Introduction to Compiler Construction
Unit 3 - The while Loop - Extending the Vic class - Examples
Improving the structure of existing code
Units with – James tedder
Interpreter Pattern.
CMPE 152: Compiler Design September 27 Class Meeting
Simple Classes in Java CSCI 392 Classes – Part 1.
CSS161: Fundamentals of Computing
Assignment Operators Topics Increment and Decrement Operators
Controlling Program Flow
Test Driven Lasse Koskela Chapter 3: Refactoring in Small Steps
Presentation transcript:

State ● What is state? ABCDE

State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data ● State of encoding Introduced state

Representing State: Data Structures ● We choose data structures based on different criteria: ● Optimal access time ● Optimal manipulation time ● Ease of implementation ● Data Structures are a mechanism of encoding state ● For any state, there are many ways of encoding that state ● The act of encoding state creates new states ● Some encoding mechanisms introduce complex state ● It is necessary to develop skills in evaluating mechanisms of encoding state

State and Behavior – state variables ● Entities may behave differently depending on their state public class Person { boolean married; public boolean willYouGoOutWithMe(Person aPerson) { if (married) return false; else return evaluateDatePotential(aPerson); }

State and Behavior: Examine this code boolean flag; int i = 0; [...] while(i< some big number) { [...] if (flag) x = x + y*30; else x = x - 500; } boolean flag; int i = 0; int temp; [...] if (flag) temp = y*30; else temp = -500; while(i< some big number) { [...] x = x + temp; }

Polymorphism – A mechanism for encoding state ● In the previous slide, the state was defined by boolean. ● This encoding was not compatible with the computation that had to be done ● The if statement provides the mapping from the boolean representation of state to another form of state which is relevant to the use of the state ● In this case, the use of the state is in the form of computation. ● Simple Example: ● Assertions

State variables and state machines ● State machines are a very common way of computation ● Parsing ● Compilers ● Stateful Protocols ● When a state machine is built, there must be some manner of representing the current state of the state machine ● Often encoded as a state variable ● A common procedural method is to encode the state of the machine as a fundamental data type (usually integral) along with a number of symbolic constants which give meaning to the encodings. ● We need to encode: ● Current state ● The States ● Transitions between states ● Events and Actions

Show Time Adjust Hours Adjust Minutes Change Mode Set/increment hours Set/increment minutes State Machine Example ● The timeless digital watch:

// DigitalWatch.java public class DigitalWatch { public static final int SHOW_TIME=0; public static final int ADJUST_HOURS=1; public static final int ADJUST_MINUTES=2; private int currentState = SHOWTIME; private int hours = 0; private int minutes = 0; public void changeMode() { switch(currentState) { case SHOW_TIME:currentState = ADJUST_HOURS; break; case ADJUST_HOURS: currentState = ADJUST_MINUTES; break; case ADJUST_MINUTES:currentState = SHOW_TIME; break; } public void set() { switch(currentState) { case SHOW_TIME: // Do nothing in this state break; case ADJUST_HOURS:incrementHours(); break; case ADJUST_MINUTES: incrementMinutes(); break; }

private void incrementHours() { hours++; if (hours>23) hours = 0; } private void incrementMinutes() { minutes++; if (minutes>59) minutes = 0; }

State Design Pattern ● There is a design pattern which uses polymorphism to encode state ● The State Design pattern ● There is also a refactoring which refers to the state design pattern: ● Replace type-code with State ● General Form: ● State example: Digital Watch. AbstractState + actions ConcreteState2 + actions ConcreteState1 + actions ConcreteState3 + actions Client + actions

More State Examples ● Transaction Processing ● Summer 04 CPSC 457 assignment ● Stateful protocol ● SMTP (Simple mail transport protocol)