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

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

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.
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation.
Cs2220: Engineering Software Class 4: Specifying Procedures Fall 2010 University of Virginia David Evans.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Constants and Data Types Constants Data Types Reading for this class: L&L,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Introduction SWE 619. Why Is Building Good Software Hard? Large software systems enormously complex  Millions of “moving parts” People expect software.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Course Lectures Available on line:
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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,
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Java Translation Program Structure
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
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:
Written by: Dr. JJ Shepherd
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Modular Decomposition, Abstraction and Specifications
Programming Language Concepts (CIS 635)
Chapter 2.
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.
MSIS 655 Advanced Business Applications Programming
Introduction to Java Programming
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Introduction to Primitive Data types
Data Abstraction David Evans cs205: engineering software
Chapter 2: Java Fundamentals
Lecture 4: Data Abstraction CS201j: Engineering Software
Programs and Classes A program is made up from classes
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
Software Specifications
Lecture 2: Java Semantics, Validation CS201j: Engineering Software?
Introduction to Primitive Data types
Presentation transcript:

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

2 cs205: engineering software Java Semantics

3 cs205: engineering software The Stack and Heap String s = new String (“hello”); s “hello” java.lang.String Objects live on the heap new creates an object on the heap Local variables live on the stack May point to objects on the heap String is a type in the Java API for representing sequences of characters

4 cs205: engineering software String s = new String (“hello”); s “hello” java.lang.String String t = s; t

5 cs205: engineering software String s = new String (“hello”); s “hello” java.lang.String String t = s; t s = new String (“goodbye”); “goodbye” java.lang.String

6 cs205: engineering software Primitive Types Not everything in Java is an Object Some types are primitive types –boolean, byte, char, double, float, int, long, short Values of primitive types are stored directly on the stack

7 cs205: engineering software String s = new String (“hello”); s “hello” java.lang.String String t = s; t int i = 205; i 205 int j = i; j 205 How can we see the difference between primitive types and objects?

8 cs205: engineering software Equality x == y Object Types: same objects Primitive Types: same value x.equals (y) Object Types: method that compares values of objects Primitive Types: doesn’t exist

9 cs205: engineering software “hi” “high” Mutability If an object is mutated, all references to the object see the new value sb java.lang.StringBuffer tb StringBuffer sb = new (“hi”); StringBuffer tb = sb; tb.append (“gh”);

10 cs205: engineering software Immutable/Mutable Types Types can be mutable or immutable –Objects of an immutable type never change value after they are created String is immutable, StringBuffer is mutable –String.concat creates a new String object –StringBuffer.append mutates the old object

11 cs205: engineering software public class Strings { public static void test (String [] args) { 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!"); // What are the values of s, t, sb and tb now? // 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) }

12 cs205: engineering software Java Semantics Question 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!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String String spec is not enough to determine if s, t, s1 and t1 are the same objects.

13 cs205: engineering software Java Language Specification (Section : String Literals) Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.(§4.3)§4.3.1§12.5§4.3.3(§15.28)

14 cs205: engineering software Java Semantics Question 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!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello”

15 cs205: engineering software Java Semantics Question 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!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!”

16 cs205: engineering software Java Semantics Question 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!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!” java.lang.String

17 cs205: engineering software 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!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!” java.lang.String “hello goodbye!” java.lang.String

18 cs205: engineering software 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!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!” java.lang.String “hello goodbye!” java.lang.String After test returns?

19 cs205: engineering software Abstraction by Specification

20 cs205: engineering software 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?

21 cs205: engineering software Abstraction A I1I1 I2I2 I3I3 I4I4 An abstraction is a many-to-one map. I5I5

22 cs205: engineering software Using Abstractions A I1I1 I2I2 I3I3 I4I4 I5I5 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?

23 cs205: engineering software Specification Tells the client of an abstraction what the client 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

24 cs205: engineering software Good Specifications Clear, precise and unambiguous –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

25 cs205: engineering software 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

26 cs205: engineering software What do you call people who decide what informal specifications mean?

27 cs205: engineering software Example Informal Specification Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted. 8 th Amendment

28 cs205: engineering software Correct Implementation? public static boolean violatesEigthAmendment (Punishment p) { // EFFECTS: Returns true if p violates the 8 th // amendment: cruel and unusual // punishments. return (p.isCruel () && p.isUnusual ()); } Or did they mean p.isCruel () || p.isUnusual () ?

29 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

30 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

31 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.

32 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.

33 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!

34 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?

35 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

36 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.

37 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…

38 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…

39 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)

40 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.

41 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).

42 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.

43 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.

44 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 Reading before next class: Chapters 3 and 9