Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Stacks, Queues, and Linked Lists
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Inheritance 2 Ranga Rodrigo Based on Mark Priestley's Lectures.
Inheritance. class RECTANGLE inherit POLYGON RECTANGLE includes features of POLYGON r: RECTANGLE; p: POLYGON Polymorphism p := r -- legal assignment Dynamic.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Design by Contract. Specifications Correctness formula (Hoare triple) {P} A {Q} – A is some operation (for example, a routine body) – P and Q are predicates.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Principles of Object-Oriented Software Development The language Eiffel.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Chair of Software Engineering ATOT - Lecture 9, 30 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering 1 Introduction to Programming Tuples and Agents Exercise Session 1 and 2 December 2008.
Chair of Software Engineering ATOT - Lecture 14, 19 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
1 Introduction to: Design by Contract Fall 2005 OOPD John Anthony.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 10 M. Piccioni 24/25 November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Software Engineering Design by Contract 1 Design by Contract ™
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
Ranga Rodrigo. Class is central to object oriented programming.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Chapter 7 Stacks II CS Data Structures I COSC 2006
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Data Structures & Algorithms
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Design by Contract. The Goal Ensure the correctness of our software (correctness) Recover when it is not correct anyway (robustness) Correctness: Assertions.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Stacks: Implemented using Linked Lists
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks CS-240 Dick Steflik.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008

Chair of Software Engineering 2 Der Plan  Contracts how-to  Inheritance  Polymorphism/Dynamic Binding  Inheritance and contracts

Chair of Software Engineering 3 The mock exam showed that...  It is reasonably easy to write “basic” contracts  Non void, non empty, correct assignment  Writing meaningful contracts may be challenging reset -- Remove all path segments except for the first... ensure only_one_seg: count=1 --easy exactly_the_same_seg: first = old first --not so easy

Chair of Software Engineering 4 Contracts are for semantics  Questions that you may want to ask yourself  What does a feature need to execute correctly?  Does the set of the arguments’ possible values need to be restricted?  What is a feature supposed to guarantee?  Who is a feature supposed to guarantee it to?  If command, what are the effects on the class?  Is there a property that holds for all objects of a class after creation and pre/after each routine execution?

Chair of Software Engineering 5 Stacks A possible definition: A stack is a data structure based on the principle of Last In First Out (LIFO) ‏ [Wikipedia]  Very often used in CS  Often implemented as array top push pop put remove item

Chair of Software Engineering 6 Class MY_STACK (overview) ‏ class MY_STACK [G] create make make (n: INTEGER)‏ -- Allocate stack for a maximum of n elements... capacity: INTEGER -- Maximum number of stack elements count: INTEGER -- Number of stack elements item: G is -- Top element... is_empty: BOOLEAN -- Is stack empty?... is_full: BOOLEAN -- Is stack full?... put (x: G)‏ -- Add x on top... remove -- Remove top element....

Chair of Software Engineering 7 Class MY_STACK (detail) ‏ class MY_STACK [G] create make invariant … count_non_negative: 0 <= count Hands-On count_bounded: count <= capacity fullness: (count = capacity) = is_full emptiness: (count = 0) = is_empty capacity_non_negative: capacity >= 0

Chair of Software Engineering 8 Class MY_STACK (detail) ‏ make (n: INTEGER) ‏ -- Allocate stack for a maximum of n elements require do... ensure end non_negative_capacity: n >= 0 capacity_set: capacity = n Hands-On empty_stack: is_empty

Chair of Software Engineering 9 Class MY_STACK (detail) ‏ item: G -- Top element require do... ensure end not_empty: not is_empty Hands-On result_exists: Result /= Void

Chair of Software Engineering 10 Class MY_STACK (detail) ‏ is_empty: BOOLEAN -- Is stack empty? require do... ensure end True Hands-On True‏ They are part of the invariant. Default is True, thus they are not needed

Chair of Software Engineering 11 Class MY_STACK (detail) ‏ is_full: BOOLEAN -- Is stack full? require do... ensure end True Hands-On True They are part of the invariant. Default is True, thus they are not needed

Chair of Software Engineering 12 Class MY_STACK (detail) ‏ put (x: G) ‏ -- Add `x' on top require do... ensure end x_exists: x /= Void not_full: not is_full one_more_item: count = old count + 1 added_to_top: item = x Hands-On

Chair of Software Engineering 13 Class MY_STACK (detail) ‏ remove ‏ -- Remove top element require do... ensure end not_empty: not is_empty one_less_item: count = old count - 1 Hands-On

Chair of Software Engineering Inheritance There are two aspects:  Reuse  Existing code processing the same task doesn't have to be rewritten.  Subtyping  Allows to write code that can process different types of objects (polymorphism)

Chair of Software Engineering Example: Pets Dogs and cats are animals (IS-A relation) Types/Subtypes are used for classification ANIMAL DOG CAT

Chair of Software Engineering Dynamic Binding class ZOO feature -- action generate_animal (flag: BOOLEAN) ‏ do create c; create d if flag then x := c else x := d end print (x.genus)-- ? end feature -- Attributes x: ANIMAL c: CAT d: DOG end class CAT inherit ANIMAL redefine genus end feature -- Status genus: STRING do Result := “Felis” end class DOG inherit ANIMAL redefine genus end feature -- Status genus: STRING do Result := “Canis” end class ANIMAL feature -- status genus: STRING do Result := “Animal” end

Chair of Software Engineering 17 Class ANIMAL class ANIMAL create make feature -- Initialization make (a_name: STRING) ‏ do name := a_name end feature -- Status name: STRING feature –– Basic operations set_name (a_name: STRING) ‏ do name := a_name end eat do print (“Animal eating”) ‏ end

Chair of Software Engineering 18 Creation status is not inherited! Feature make is inherited, but in PANTHER it’s not known as a creation procedure, so you need to list it in the create clause Feature redefinition class PANTHER inherit ANIMAL redefine eat end create make feature eat is do print (“Panther devouring”) ‏ end roar is do print (“ROOOAAAAAARRR R”) ‏ end Class PANTHER

Chair of Software Engineering 19 Quiz 1: Animals and Panthers r local a: ANIMAL p: PANTHER do create a.make (“Small Animal”) ‏ create p.make (“Black Panther”) ‏ a.set_name (“Creature”) ‏ p.set_name (“Bagheera”) ‏ a.eat p.eat end => “Animal eating” => “Panther devouring” Hands-On

Chair of Software Engineering 20 Quiz 2: Animals and Panthers r local a: ANIMAL p: PANTHER do create p.make (“Black Panther”) ‏ a := p print (a.name) ‏ a.eat a.roar end => “Black Panther” => “Panther devouring” Hands-On

Chair of Software Engineering 21 Quiz 3: Animals and Panthers r local a: ANIMAL p: PANTHER do create a.make (“Creature”) ‏ p := a end Hands-On

Chair of Software Engineering 22 Quiz 4: Animals and Panthers r local a: ANIMAL p: PANTHER do create {PANTHER} a.make (“Bagheera”) ‏ a.roar end Hands-On

Chair of Software Engineering 23 Forcing a type - the problem animal_list: LINKED_LIST [ANIMAL]... animal_list.put_front(a_panther) ‏ animal_list.store ("filename") ‏ -- Two years later: animal_list := retrieved ("filename") x := animal_list.i_th (1) -- [1] x.roar -- [2] But: If x: PANTHER, [1] is invalid If x: ANIMAL, [2] is invalid.

Chair of Software Engineering 24 The old solution: Assignment attempt x ?= y with x: A If y is attached to an object whose type conforms to A, perform normal reference assignment. Otherwise, make x void.

Chair of Software Engineering 25 Quiz 5: Animals and Panthers r local a: ANIMAL p: PANTHER do create {PANTHER} a.make (“Bagheera”) ‏ p := a p ?= a if p /= Void then print (p.name) ‏ p.roar end => “Bagheera” Hands-On

Chair of Software Engineering 26 The new solution: the Object Test if {x: MY_TYPE} object_to_be_tested then -- use x, guaranteed to be non void -- and of dynamic type MY_TYPE else -- Here code that applies if x is not -- of dynamic type MY_TYPE end

Chair of Software Engineering 27 Quiz 5: Alternative solution r local a: ANIMAL do create {PANTHER} a.make (“Bagheera”) ‏ if {p: PANTHER} a then print (p.name) ‏ p.roar end => “Bagheera”

Chair of Software Engineering 28 Inheritance and assertions Correct call: if a1. then a1.r (...) ‏ else... end r is require  ensure  r is require  ensure  CA B a1: A a1. r (…)‏ …

Chair of Software Engineering Assertion redeclaration rule Redefined version may not have require or ensure. May have nothing (assertions kept by default), or require else new_pre ensure then new_post Resulting assertions are: ● original_precondition or new_pre ● original_postcondition and new_post ● original_invariant and new_invariant 29

Chair of Software Engineering Weaker or stronger? A formula F is stronger than another formula G if: F implies G (or: F ⇒ G) In other words, formula G is weaker than F. Precondition (weakened): original_pre implies original_pre or new_pre Postcondition (strengthened): original_post and new_post implies original_post Invariant (strengthened): original_inv and new_inv implies original_inv 30

Chair of Software Engineering Exercise Create 3 examples (one for preconditions, one for postconditions, the other for invariants) which demonstrate that these rules have to be obeyed, otherwise programs may crash. Hints:  you need two classes, one descendant of the other  descendant need to redefine a feature  descendant’s contract violates inheritance rules  you need a feature with a polymorphic argument  you need a call to the polymorphic feature 31 Hands-On

Chair of Software Engineering Rules for Subtyping: Preconditions class PARENT feature make_of_size ( n: INTEGER ) require n >=3 do -- create array of size n print (i_th (3)) end crash( s: PARENT) do s.make_of_size( 4 ) end temp: CHILD... create temp crash ( temp ) class CHILD inherit PARENT redefine make_of_size end feature make_of_size ( n: INTEGER ) require n >= 5 do -- create array of size n print (i_th (5)) end Examples based on KOOP - With kind permission of Peter Müller 32

Chair of Software Engineering Rules for Subtyping: Postconditions class PARENT feature foo: INTEGER do Result := 1 ensure Result > 0 end crash( s: PARENT) do att := 5 // s.foo end temp: CHILD... create temp crash ( temp ) class CHILD inherit PARENT redefine foo end feature foo: INTEGER do Result := 0 ensure Result >= 0 end 33

Chair of Software Engineering Rules for Subtyping: Invariants class PARENT create make feature n: INTEGER make do n := 5 end crash: INTEGER do Result := 5 // n end invariant n > 0 end temp: CHILD; i: INTEGER... create temp i := temp.crash class CHILD inherit PARENT redefine make end create make feature make do n := 0 end invariant n >= 0 end 34

Chair of Software Engineering 35 Questions?