F27SB2 Software Development 2 Lecture 7: State Diagrams.

Slides:



Advertisements
Similar presentations
GUI Testing. High level System Testing Test only those scenarios and outputs that are observable by the user Event-driven Interactive Two parts to test.
Advertisements

Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Io package as Java’s basic I/O system continue’d.
F27SB2 Software Development 2 Lecture 8: State Diagram & GUI Example.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Welcome to CIS 083 ! Events CIS 068.
Programming & Scratch. Programming Learning to program is ultimately about learning to think logically and to approach problems methodically. The building.
F27SB2 Software Development 2 Lecture 6: Java GUIs 5.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
GUI programming Graphical user interface-based programming.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Lecture 2 Exception handling Advanced Java Programming 1 dr inż. Wojciech Bieniecki
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
1 אירוע אמאזון. 2 שלבי הפיתוח עם דיאגרמות UML 3 אמאזון תרשים תוכן.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
CHAPTER:07 JAVA IDE PROGRAMMING-II Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
CSE 205 Exam 2 Review By Cheri Thompson and Katherine Kincade.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
Lecture 15 Basic GUI programming
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 230 S Programming Techniques
Introduction to programming in java
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Ellen Walker Hiram College
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Graphical user interface-based programming
Introduction to Event Handling
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Graphical User Interface
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

F27SB2 Software Development 2 Lecture 7: State Diagrams

State and Interaction view a system as a sequence of events and associated actions event ==> something that indicates the need for change – e.g. switch setting, button selection action ==> something that causes change – e.g. switch is set ==> light goes on – e.g. button is selected ==> method is invoked to respond to event

State and Interaction identify distinct system states characterise state as: – current configuration i.e. status of things in system that may change – valid events/actions for current configuration i.e. how change is indicated/what may be changed and how think of entire system as a set of states

State and Interaction system execution consists of series of state transitions in current state: 1.event occurs 2.action is triggered 3.configuration is changed 4.new state is entered

State and Interaction e.g. light circuit ON STATE – configuration - light is on – event - switch set to off action - light goes off now in OFF STATE OFF STATE – configuration - light is off – event - switch set to on action - light goes on now in ON STATE

State and Interaction e.g. text editor INITIAL STATE – configuration - nothing available to edit – event - select open file action - contents from file available for editing now in EDIT STATE – event - select open new action - empty contents available for editing now in EDIT STATE – event - select exit

State and Interaction EDIT STATE – configuration - file open for edit – event - select edit operation action - file contents changed now in EDIT STATE – event - select save file action - contents copied back to file now in EDIT STATE – event - select close file action - contents copied back to file now in INITIAL STATE

State Transition Diagrams also known as state charts closely related to finite state machines state: – box with its name in it transition: – arc from a state to a state – labelled with event and action

State Transition Diagrams e.g. light switch ON OFF set switch off/ light goes off set switch on/ light goes on

State Transition Diagrams e.g. editor INITIAL EDIT select open/ file contents editable select new/ empty contents editable select edit/ contents changes select save/ contents written to file select close/ contents written to file select exit/ return to host system

State Transition Diagrams e.g. traffic lights STOP READY GO SLOW time signal 1/ amber on time signal 2/ red & amber off; green on time signal 3/ green off; amber on time signal 4/ amber off; green on

State Transition Diagrams system may only have 1 state e.g. timer TIMER select increment/ add 1 to count display select clear/ set count display to 0 select go/ decrement count display to 0

State Transition Diagrams can attach a guard to a state: [guard] guard must be true for transition to occur CHECK INPUT check input [bad input]/ request re-enter input check input [input OK]/ process input

State and Interactive System Design very useful to think about interactive systems in terms of sequences of states user’s view of system state is not same as developer’s view of system state – user interprets system behaviour in terms of their conceptual model of what system is for – developer knows about underlying programming constructs – e.g. to use editor, user doesn’t need to know how interface or file system are actually

State and Interactive System Design user characterises a state by properties of entities in problem domain as shown by what’s on the display – current configuration ==> entities in problem domain represented by constructs in display – events ==> interact with entities in problem domain by selecting appropriate display constructs – actions ==> changes to entities in problem domain reflected by changes in display organisation

State and Interactive System Design e.g. editor – user thinks about documents in files represented by scrollable text on screen developer characterises a state by implementation-level entities: – current configuration ==> variables values; open files; JFrame Component s – events ==> Java Event s – actions ==> methods

State and Interactive System Design e.g. editor – developer thinks about character positions in arrays of String s etc developer should construct system to: – reflect user’s conceptual model – enable effective system use e.g. in editor, if file is open can’t exit

State and Interactive System Design in any system, in any given state, only some of all possible events and actions are appropriate – e.g. if light is off then can turn it on but can’t turn it off because it’s off already – e.g. in editor, if file is open can’t exit often, when the state changes the appropriate events and actions change – e.g. after turning light on can turn it off but can’t turn it on because it’s on already – e.g. in editor, after closing file, can’t close it again

State and Interactive System Design important to constrain available events and actions to prevent action sequences which might damage system (and user) – e.g. can’t open washing machine door during wash cycle – e.g. can’t exit editor without closing file important to constrain available actions to prevent user confusion – e.g. can’t close file which is already closed

State and Interactive System Design for effective system use, user always needs to know what state the system is in hidden mode – in some state but no way to tell which one avoid hidden modes ensure user always knows current system state unambiguous display content explicit statement of mode – e.g. MS Word always indicates current style, font etc

From State Transition Diagram to Java idealised stages: draw state transition diagram will realise: – events as Event s with Component sources – guards as if / while – actions as methods design and implement interface implement actions as methods

From State Transition Diagram to Java implement state transitions in actionPerformed if only one state then: – no transitions – only need to identify events if more than one state then: – need to keep track of current state when reacting to event – to ensure that event is valid for state

From State Transition Diagram to Java maintain state variable – value reflects current state for simplicity, represent states as integers – could use enumerated types? actionPerformed must: – have separate cases for each state – within each state, identify and react to appropriate events

From State Transition Diagram to Java int state variable = initial state;... public void actionPerformed(ActionEvent e) { switch(state variable) { case state1: if(e.getSource()==Component 11 ) { state 1 action for this event state variable = next state; } else if(e.getSource()==Component 12 ) {... } else...

From State Transition Diagram to Java case state2: if(e.getSource()==Component 21 ) { state 2 action for this event state variable = next state; } else... }

Example: password control system access controlled by password file of user names and passwords user must enter name and password verify name and password from file don’t worry about encryption, hiding password etc here promptentry JFrame JLabel JTextField

Example: password control CHECK LOGIN CHECK PASSWORD get login [invalid]/ request login get password [invalid]/ request password.../ request login get login [valid]/ request password get password[valid]/...

Example: password control import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; class ID { String login, password; ID(String l,String p) { login = l; password = p; }

Example: password control class Login extends JFrame implements ActionListener { JLabel prompt; JTextField entry; ID [] details; int n = 0; static int idno; define state variable & constants int state; final int LCHECK = 0; final int PCHECK = 1; final int SUCCESS = 2;

Example: password control final int MAXID = 100; Login() { setLayout(new FlowLayout()); prompt = new JLabel("Please enter login: "); prompt.setFont(new Font("SanSerif",Font.BOLD,18)); add(prompt); entry = new JTextField(12); entry.setFont(new Font("SanSerif",Font.BOLD,18)); add(entry); entry.addActionListener(this); }

Example: password control read login/password as strings from file void getDetails(String f) throws IOException { String l,p; BufferedReader file = new BufferedReader(new FileReader(f)); details = new ID[MAXID]; l = file.readLine(); while(l!=null) { p = file.readLine(); details[n] = new ID(l,p); n++; l = file.readLine(); }

Example: password control find login in file int checkLogin(String l) { for(int i=0;i<n;i++) if(details[i].login.equals(l)) return i; return -1; } check password for legal login boolean checkPassword(String p) { return details[idno].password.equals(p); }

Example: password control public void actionPerformed(ActionEvent e) { switch(state) check login state { case LCHECK: if(e.getSource()==entry) { idno = checkLogin(entry.getText()); if(idno==-1) prompt.setForeground(Color.red);

Example: password control invalid login so don’t change state else { prompt.setForeground(Color.black); prompt. setText("Please enter password:"); valid login so change state state = PCHECK; } } entry.setText(""); return;

Example: password control check password state case PCHECK: if(e.getSource()==entry) { if(!checkPassword(entry.getText())) prompt.setForeground(Color.red);

Example: password control invalid password so don’t change state else { prompt.setForeground(Color.black); prompt.setText("Login successful"); valid password so change state state = SUCCESS; } } entry.setText(""); return; }

Example: password control class TestLogin { public static void main(String [] args) throws IOException { Login l = new Login(); l.setTitle("Login"); l.setSize(450,80); l.setVisible(true); l.addWindowListener(...) l.getDetails("users.txt"); set intitial state l.state = LCHECK; }

Example: password control

Swing provides specialised JPasswordField like JTextField but: – can set to not show password as typed – returns array of char not String