Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans www.cs.virginia.edu/cs205.

Slides:



Advertisements
Similar presentations
2017/3/25 Test Case Upgrade from “Test Case-Training Material v1.4.ppt” of Testing basics Authors: NganVK Version: 1.4 Last Update: Dec-2005.
Advertisements

Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Chapter 6 Writing a Program
Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
ZMQS ZMQS
Lecture 15 Linked Lists part 2
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 17: ProvingUndecidability.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Data Structures ADT List
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
ADTs unsorted List and Sorted List
Modern Programming Languages, 2nd ed.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
ABC Technology Project
Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1.
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
Review Pseudo Code Basic elements of Pseudo code
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.
Squares and Square Root WALK. Solve each problem REVIEW:
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 The C Language An International Standard CIS2450 Professional Aspect of Software Engineering.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
How Cells Obtain Energy from Food
Introduction to Recursion and Recursive Algorithms
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Cs2220: Engineering Software Class 4: Specifying Procedures Fall 2010 University of Virginia David Evans.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Testing It is much better to have a plan when testing your programs than it is to just randomly try values in a haphazard fashion. Testing Strategies:
Design by Contract Fall 2016 Version.
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Data Abstraction David Evans cs205: engineering software
Lecture 4: Data Abstraction CS201j: Engineering Software
Lecture 13: Subtyping Rules Killer Bear Climber
Presentation transcript:

cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans

2 cs205: engineering software Procedural Specifications Specification for a procedure describes: –What its inputs are –What the mapping between inputs and outputs are –What it can do the state of the world

3 cs205: engineering software Requires and Effects Header: name of procedure, types of parameters and return value –Java declaration Clauses (comments in Java) –REQUIRES- precondition the client must satisfy before calling –EFFECTS – postcondition the implementation satisfy at return

4 cs205: engineering software Contract Client promises to satisfy the precondition in the requires clause Implementer promises if client satisfies the precondition, the return value and state when the function returns will satisfy the postcondition.

5 cs205: engineering software Specification Contract f () REQUIRES: precondition EFFECTS: postcondition precondition { f () ; } postcondition If the precondition is true, after we call f (), the postcondition is true.

6 cs205: engineering software Specification Example public String bestStock () // REQUIRES: false // EFFECTS: Returns the name of the // best stock to buy on the NASDAQ // tomorrow. Can we implement a procedure that satisfies this specification? Yes, any implementation will satisfy this specification! If the precondition in the requires clause is not satisfied, the procedure can do anything and still satisfy its specification!

7 cs205: engineering software Specification Example public String bestStock () // REQUIRES: true // EFFECTS: Returns the name of the // best stock to buy on the NASDAQ // tomorrow. Can we implement a procedure that satisfies this specification?

8 cs205: engineering software Requires Clauses The weaker (more easy to make true) the requires clause: –The more useful a procedure is for clients –The more difficult it is to implement correctly Avoid requires clauses unless there is a good reason to have one –Default requires clause is: REQUIRES true –Client doesn’t need to satisfy anything before calling

9 cs205: engineering software Specification Example public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: Returns the value of the // biggest element of a. Is this a reasonable specification? No, what should client expect to happen if a is empty.

10 cs205: engineering software Specification Example public static int biggest (int [ ] a) // REQUIRES: a has at least one element. // EFFECTS: Returns the value of the // biggest element of a. Is this a good specification? Maybe, depends on the client. Its risky…

11 cs205: engineering software Specification Example public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: If a has at least one // element, returns the value biggest // element of a. Otherwise, returns // Integer.MIN_VALUE (smallest int // value). Better, but client has to deal with special case now. Best would probably be to use an exception…

12 cs205: engineering software Bad Use of Requires Clause Bug discovered in Microsoft Outlook that treats messages that start with “begin ” as empty attachments (can be exploited by viruses) To workaround this problem: Do not start messages with the word "begin" followed by two spaces. Use only one space between the word "begin" and the following data. Capitalize the word "begin" so that it is reads "Begin." Use a different word such as "start" or "commence ". from (this is no longer available, was “revoked” by Microsoft)

13 cs205: engineering software Modifies How does a client know a is the same after biggest returns? public static int biggest (int [ ] a) // REQUIRES: true // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer.MIN_VALUE // (smallest int value). Reading the effects clause is enough – if biggest modifies anything, it should describe it. But, that’s a lot of work.

14 cs205: engineering software Modifies Modifies clause: any state not listed in the modifies clause may not be changed by the procedure. public static int biggest (int [ ] a) // REQUIRES: true // MODIFIES: nothing // EFFECTS: If a has at least one element, // returns the value biggest element of a. // Otherwise, returns Integer.MIN_VALUE // (smallest int value).

15 cs205: engineering software Modifies Example public static int replaceBiggest (int [ ] a, int [] b) // REQUIRES: a and b both have at least one // element // MODIFIES: a // EFFECTS: Replaces the value of the biggest // element in a with the value of the biggest // element in b.

16 cs205: engineering software Defaults What should it mean when there is no requires clause? What should it mean when there is no modifies clause? What should it mean when there is no effects clause? REQUIRES: true MODIFIES: nothing Meaningless.

17 cs205: engineering software PS1 Question 2 was very ambiguous: Modify the program so that instead of dying instantly, cells go through a dying state where they are displayed in a different color for one step before they die. How should the life rules deal with dying cells?

18 cs205: engineering software Dealing with Bad Specs When a specification is ambiguous: –Ask the provider to figure out what it means –Or, state very clearly what additional assumptions you make Good (easy to implement) assumption here: dying cells are not alive (as far as neighbors are concerned), and don’t become alive (whatever their neighbor states are)

19 cs205: engineering software Avoid Changing Interfaces If we change this, need to examine all code that uses isAlive If we just change its implementation, don’t need to examine anything else boolean isAlive() // EFFECTS: Returns true if the cell is alive, false otherwise.

20 cs205: engineering software enum (added to Java 1.5) public enum StateValue { dead, dying, alive } ;... public Color getColor() { switch (alive) { case alive: return Color.green; case dying: return Color.red; case dead: return Color.white; }

21 cs205: engineering software PS1 Grading Full credit even for inelegant code Later assignments: need clear, elegant, economical code for full credit

22 cs205: engineering software PS2 You will need to write a fair bit of code yourself Challenge is to make your specification and implementation cover all possible inputs Next class: some hints on PS2

23 cs205: engineering software Charge Specifications in CS205 –Will be informal: written in English (aided by common mathematical notations) –...but must be precise and clear –REQUIRES/MODIFIES/EFFECTS style