Lecture 3: Abstraction by Specification CS201j: Engineering Software?

Slides:



Advertisements
Similar presentations
Offside Examples Compiled by Richard Baker. Richard Baker Should we declare “B” offside? Or should we wait ? declare wait Diagram 1 B A.
Advertisements

Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 3: Abstraction by Specification.
Offside Examples. Should we declare “B” offside? Or should we wait ? declare wait Diagram 1 B A.
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 13: Concurring Concurrently.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Cs2220: Engineering Software Class 4: Specifying Procedures Fall 2010 University of Virginia David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
CS 201 Functions Debzani Deb.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
1 Offside Law At the end of this lesson the student will: Objectives identify offside position state what constitutes involvement in active play.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Cs205: engineering software university of virginia fall 2006 Validation David Evans
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
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/
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Cs205: engineering software university of virginia fall 2006 Introducing Java David Evans Don’t forget to your registration.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 13: Behavioral Subtyping Fall 2010 University of Virginia David Evans.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Modular Decomposition, Abstraction and Specifications
Algorithms and Problem Solving
Law 11 Offside.
All three elements must be present or there cannot be an infraction
Design David Evans cs205: engineering software
Input Space Partition Testing CS 4501 / 6501 Software Testing
Chapter 11 Object-Oriented Design
LAW 11 - OFFSIDE Online Training Script:
Amendments U.S. Soccer Federation Referee Program
CSE 374 Programming Concepts & Tools
At least 30% of 2000 referees missed each one of these questions!
Algorithm and Ambiguity
Design by Contract Fall 2016 Version.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Concurring Concurrently
Lecture 9: Exceptions in Java CS201j: Engineering Software
LAW 11 - OFFSIDE Online Training Script:
Data Abstraction David Evans cs205: engineering software
Recursion (Continued)
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
Algorithm and Ambiguity
CSE 143 Lecture 4 More ArrayIntList:
Offside A Guide for Assistant Referees
IMPORTANT NOTE Some parts of these section slides deal with null ints. This was a mistake, as primitives cannot be null. These issues have been corrected.
CSE 143 Lecture 5 More ArrayIntList:
Compiled by Richard Baker
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Lecture 2: Java Semantics, Validation CS201j: Engineering Software?
Law 11 Offside.
Presentation transcript:

David Evans http://www.cs.virginia.edu/~evans Lecture 3: Abstraction by Specification CS201j: Engineering Software? University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans

Menu Java Semantics Recap Abstraction by Specification PS1 Comments 5 September 2002 CS 201Jj Fall 2002

Java Semantics 5 September 2002 CS 201Jj Fall 2002

s t sb tb s1 t1 java.lang.String “hello goodbye!” java.lang.String public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } java.lang.String “hello” s java.lang.String t “hello” sb java.lang.StringBuffer tb “he” “hello goodbye!” s1 java.lang.String java.lang.String t1 “hello” “hello goodbye!” 5 September 2002 CS 201Jj Fall 2002

Which of these are true: a) s == t b) s1 == t1 c) s == s1 java.lang.String Which of these are true: a) s == t b) s1 == t1 c) s == s1 d) s.equals (t) e) sb == tb f) t.equals (tb) “hello goodbye!” java.lang.String “hello” s  java.lang.String t “hello” sb java.lang.StringBuffer  tb “he” “hello goodbye!” s1 java.lang.String java.lang.String t1 “hello” “hello goodbye!” 5 September 2002 CS 201Jj Fall 2002

Abstraction by Specification 5 September 2002 CS 201Jj Fall 2002

Managing Complexity Divide problem into subproblems that Can be solved independently Can be combined to solve the original problem How do we know they can be solved independently? How do we know they can be combined to solved the original problem? 5 September 2002 CS 201Jj Fall 2002

Abstraction A I3 I4 I1 I2 I5 An abstraction is a many-to-one map. 5 September 2002 CS 201Jj Fall 2002

Using Abstractions A Client When a client uses an abstraction, it should work as the client expects it to no matter with implementation is provided. How should client know what to expect? 5 September 2002 CS 201Jj Fall 2002

Specification Tells the client of an abstraction what she can expect it to do Tells the implementer of an abstraction what the implementation must do to satisfy the client Contract between client and implementer: Client will only rely on behavior described by specification Implementer will provide an implementation that satisfies the specification 5 September 2002 CS 201Jj Fall 2002

Good Specifications Clear, precise and unambiguous Complete Clients and implementers will agree on what they mean Complete Describe the behavior of the abstraction in all situations Declarative Describe what the abstraction should do, not how it should do it 5 September 2002 CS 201Jj Fall 2002

Formality of Specifications Informal: written in a natural language (e.g., English) People can disagree on what it means Degrees of informality Formal: written in a specification language Meaning is defined by specification language (whose meaning is defined precisely, but eventually informally) May be analyzed by machines 5 September 2002 CS 201Jj Fall 2002

What do you call people who decide what informal specifications mean? 5 September 2002 CS 201Jj Fall 2002

Example Informal Specification Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted. 8th Amendment 5 September 2002 CS 201Jj Fall 2002

Correct Implementation? public static boolean violatesEigthAmendment (Punishment p) { // EFFECTS: Returns true if p violates the 8th // amendment: cruel and unusual // punishments. return (p.isCruel () && p.isUnusual ()); } Or did they mean p.isCruel () || p.isUnusual () ? 5 September 2002 CS 201Jj Fall 2002

Example Informal Specification A player is in an offside position if: he is nearer to his opponents’ goal line than both the ball and the second last opponent A player is not in an offside position if: he is in his own half of the field of play or he is level with the second last opponent or he is level with the last two opponents Offence A player in an offside position is only penalised if, at the moment the ball touches or is played by one of his team, he is, in the opinion of the referee, involved in active play by: interfering with play or interfering with an opponent or gaining an advantage by being in that position No Offence There is no offside offence if a player receives the ball directly from: a goal kick or a throw-in or a corner kick. http://www.fifa.com/fifa/handbook/laws/2002/LOTG2002_E.pdf, Law 11 5 September 2002 CS 201Jj Fall 2002

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 5 September 2002 CS 201Jj Fall 2002

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 5 September 2002 CS 201Jj Fall 2002

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 September 2002 CS 201Jj Fall 2002

Specification Contract REQUIRES: precondition EFFECTS: postcondition precondition { f (); } postcondition If the precondition is true, after we call f (), the postcondition is true. 5 September 2002 CS 201Jj Fall 2002

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! 5 September 2002 CS 201Jj Fall 2002

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? 5 September 2002 CS 201Jj Fall 2002

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 The more difficult it is to test 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 5 September 2002 CS 201Jj Fall 2002

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. 5 September 2002 CS 201Jj Fall 2002

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… 5 September 2002 CS 201Jj Fall 2002

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… 5 September 2002 CS 201Jj Fall 2002

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 http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q265230& 5 September 2002 CS 201Jj Fall 2002

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. 5 September 2002 CS 201Jj Fall 2002

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). 5 September 2002 CS 201Jj Fall 2002

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. 5 September 2002 CS 201Jj Fall 2002

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. (Lose points for not writing a specification) 5 September 2002 CS 201Jj Fall 2002

Specifications in CS201J PS2 PS3 and later Informal, but precise Use Requires/Modifies/Effects clauses Need a very good reason to have a precondition stronger than true (especially after we cover Exceptions). PS3 and later Informal and some formal Formal specifications as ESC/Java annotations Don’t describe all behavior (so still need the informal specs) 5 September 2002 CS 201Jj Fall 2002

Problem Set 1 5 September 2002 CS 201Jj Fall 2002

Example Informal Specification If a cell is currently dead cell and has three live neighbors, then it becomes a live cell. If a cell is currently alive and has two or three live cells it remains alive. Otherwise, the cell dies. Any ambiguities in this? 5 September 2002 CS 201Jj Fall 2002

PS1 Comments Java has real booleans isAlive () and isAlive () == true mean the same thing Read the coding guidelines on course website Be careful when you cut and paste Lost one point for having misleading/incorrect comments in your code 5 September 2002 CS 201Jj Fall 2002

PS1 Comments A few of you turned in code which clearly wouldn’t even compile Why? Don’t put your SSN on assignments You should keep it as secret as possible University shouldn’t use it as an identifier Ask for a student ID number that is not your SSN Don’t call the Assistant Coaches Unless its their scheduled lab hours and they aren’t there! 5 September 2002 CS 201Jj Fall 2002

Charge PS2: due Tuesday Lab hours posted on web site Today: 5-9pm Sunday: 4-8pm 5 September 2002 CS 201Jj Fall 2002