Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 9 20. October 2008.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 5 6 October 2008.
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 7: SCOOP Type System (based on work with Piotr Nienaltowski)
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Constants, once routines, and helper functions these slides contain advanced material and are optional.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Chair of Software Engineering Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 2.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 3 29 September 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
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 Lecture 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
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 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 6:Computational Model (based on work with Piotr Nienaltowski)
Chapter 15: Operator Overloading
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
Ranga Rodrigo. Class is central to object oriented programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Writing Classes (Chapter 4)
Eiffel Naeem Esfahani University of Tehran "Man cannot discover new oceans unless he has the courage to lose sight of the shore." -- Andre Gide.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
© 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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
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.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008

Chair of Software Engineering Today’s learning goals  Local variables  Clients and suppliers  Qualified and unqualified calls  Assignment to attributes  Entities and variables: summary 2

Chair of Software Engineering  Some variables are only used by a certain routine (examples from your last assignment?)  Declare them as local: feature f (arg1: A; …) require precondition local x, y: B z: C do … end ensure postcondition end Local variables 3

Chair of Software Engineering The scope of names  Attributes:  are declared anywhere inside a feature clause, but outside other features  are visible anywhere inside the class  Formal arguments:  are declared after the feature name  are only visible inside the feature body and its contracts  Local variables:  are declared in a local clause inside the feature declaration  are only visible inside the feature body 4

Chair of Software Engineering Compilation error? (1) class PERSON feature name: STRING set_name (a_name: STRING) do name := a_name end exchange_names (other: PERSON) local s: STRING do s := other.name other.set_name (name) set_name (s) end print_with_semicolon do create s.make_from_string (name) s.append(‘;’) print (s) end 5 Hands-On Error: This variable was not declared

Chair of Software Engineering Compilation error? (2) 6 Hands-On class PERSON feature …-- name and set_name as before exchange_names (other: PERSON) local s: STRING do s := other.name other.set_name (name) set_name (s) end print_with_semicolon local s: STRING do create s.make_from_string (name) s.append(‘;’) print (s) end OK: Two different local variables in two routines

Chair of Software Engineering Compilation error? (3) 7 class PERSON feature …-- name and set_name as before s: STRING exchange_names (other: PERSON) do s := other.name other.set_name (name) set_name (s) end s: STRING print_with_semicolon do create s.make_from_string (name) s.append(‘;’) print (s) end Error: An attribute with the same name was already defined Hands-On

Chair of Software Engineering Compilation error? (4) 8 class PERSON feature …-- name and set_name as before exchange_names (other: PERSON) do s := other.name other.set_name (name) set_name (s) end print_with_semicolon do create s.make_from_string (name) s.append(‘;’) print (s) end s: STRING end Hands-On OK: A single attribute used in both routines

Chair of Software Engineering Local variables vs. attributes  Which one of the two correct versions (2 and 4) do you like more? Why?  Describe the conditions under which it is better to use a local variable instead of an attribute and vice versa 9 Hands-On

Chair of Software Engineering Result  Inside every function you can use the predefined local variable Result (you needn’t and shouldn’t declare it)  The return value of a function is whatever value the Result variable has at the end of the function execution  At the beginning of routine’s body Result (as well as regular local variables) is initialized with the default value of its type  Every regular local variable is declared with some type; and what is the type of Result? 10 It’s the function return type!

Chair of Software Engineering Compilation error? (5) 11 class PERSON feature …-- name and set_name as before exchange_names (other: PERSON) do Result := other.name other.set_name (name) set_name (Result) end name_with_semicolon: STRING do create Result.make_from_string (name) Result.append(‘;’) print (Result) end Hands-On Error: Result can not be used in a procedure

Chair of Software Engineering Clients and suppliers  If class C mentions class A in its text (e.g., declares an entity of type A), then  C is a client of A  A is a supplier of C  A class can be a supplier (and a client) of itself (you’ll see an example tomorrow) 12 AC C

Chair of Software Engineering Clients and suppliers How many suppliers does class COUPLE have? What are they? class COUPLE feature man, woman: HUMAN years_together: INTEGER print_wedding_invitation local s: STRING p: INVITATION_PRINTER do s := “We are happy to invite you to the wedding of ” + man.name + “ and ” + woman.name + “!” create p p.print (s, 50) end 13 Hands-On

Chair of Software Engineering  In object-oriented computation each routine call is performed on a certain object  Inside the routine we can access this object using the predefined entity Current Current 14 ( STATION ) x.connect (y) connect (other: STATION) do … road.connect (Current, other) end  What is the type of Current?

Chair of Software Engineering Qualified vs. unqualified  If the target of a feature call is Current, it is common to omit it: Current.f (a) 15 f (a)  Such a call is unqualified  Otherwise, if the target of a call is specified explicitly, the call is qualified x.f (a)

Chair of Software Engineering Qualified or unqualified? Are the following feature calls qualified or unqualified? What are the targets of these calls? 1)x.y 2)x 3)f (x.a) 4)x.y.z 5)x (y.f (a.b)) 6)f (x.a).y (b) 7)Current.x 16 Hands-On qualified unqualified qualified unqualified qualified

Chair of Software Engineering Assignment to attributes  Direct assignment to an attribute is only allowed if an attribute is called in an unqualified way: y := 5 x.y := 5 Current.y := 5 17 OK Error ?  There are two main reasons for this rule: 1.A client may not be aware of the restrictions put on the attribute value and interdependencies with other attributes => class invariant violation (Example?) 2.Guess! (Hint: uniform access principle)

Chair of Software Engineering Entity: the final definition 18 o variable attribute o constant attribute Variables Read-only entities Only a variable can be used in a creation instruction and in the left part of an assignment An entity in program text is a “name” that directly denotes an object. More precisely: it is one of  attribute name  formal argument name  local variable name  Result  Current

Chair of Software Engineering Find 5 errors class VECTOR feature x, y: REAL copy_from (other: VECTOR) do Current := other end copy_to (other: VECTOR) do create other other.x := x other.y := y end reset do create Current end 19 Hands-On ‘Current’ is not a variable and thus can not be assigned to ‘other’ is a formal argument (not a variable) and thus can not be used in creation ‘other.x’ is a qualified attribute call (not a variable) and thus can not e assigned to the same reason for ‘other.y’ ‘Current’ is not a variable and thus can not be used in creation