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

Slides:



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

Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Semantics Static semantics Dynamic semantics attribute grammars
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
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
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
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 3.
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.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
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 3.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
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 6.
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 11.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
UNIT II Decision Making And Branching Decision Making And Looping
Fundamentals of Python: From First Programs Through Data Structures
Ranga Rodrigo. Class is central to object oriented programming.
Fundamentals of Python: First Programs
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
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 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
1 Assertions. 2 A boolean expression or predicate that evaluates to true or false in every state In a program they express constraints on the state that.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
© 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
The following slides contain advanced material and are optional.
Java - Data Types, Variables, and Arrays
CS1100 Computational Engineering
The structure of programming
Einführung in die Programmierung Introduction to Programming Prof. Dr
Problem 1 Given n, calculate 2n
Presentation transcript:

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

2 Today  Attributes, formal arguments, and local variables  Control structures

3 Declared anywhere inside a feature clause, but outside other features class C feature attr1 : CA1 f (arg1 : A …) do … end … end Visible anywhere inside the class visible outside the class (depending on their visibility) Attributes

4 Declared after the feature name, in parenthesis: feature f (arg1 : C1 ; …; argn : CN ) require... local … do … ensure... end only visible inside the feature body and its contracts Formal arguments

5 Some variables are only used by a certain routine. Declare them as local: feature f (arg1 : A …) require... local x, y : B z : C do … ensure... end only visible inside the feature body Local variables

6 Summary: the scope of names Attributes:  declared anywhere inside a feature clause, but outside other features  visible anywhere inside the class  visible outside the class (depending on their visibility) Formal arguments:  declared after the feature name, in parenthesis  only visible inside the feature body and its contracts Local variables:  declared in a local clause inside the feature declaration  only visible inside the feature body

7 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 Hands-On Error: this variable was not declared

8 Compilation error? (2) 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

9 An example of side effects Hands-On class PERSON feature … name : STRING print_with_semicolon local s : STRING do create s.make_from_string (name) s.append (“;”) print (s) end print_with_sticky_semicolon do name.append (“;”) print (name) end Now the semicolon sticks to the attribute. This is called side effect

10 Compilation error? (3) 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 Hands-On Error: an attribute with the same name was already defined

11 Compilation error? (4) 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

12 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 Hands-On

13 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? It’s the function return type!

14 Compilation error? (5) 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

15  In object-oriented computation each routine call is performed on a certain object  From inside a routine we can access this object using the predefined entity Current Current (STATION ) x.change_name (y) change_name (n: STRING) do … city.internal_stations.extend (Current, n) end  What is the type of Current?

16  If the target of a feature call is Current, it is omitted: Current.f (a) f (a) Revisiting qualified vs. unqualified feature calls  Such a call is unqualified  Otherwise, if the target of a call is specified explicitly, the call is qualified x.f (a)

17 Qualified or unqualified? Are the following feature calls, with their feature names underlined, 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 Hands-On qualified unqualified qualified unqualified qualified

18 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  There are two main reasons for this rule: 1.A client may not be aware of the restrictions on the attribute value and interdependencies with other attributes => class invariant violation (Example?) 2.Guess! (Hint: uniform access principle) OK Error ?

19 Entity: the final definition  variable attribute  constant attribute 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 Read-write entities / variables Read-only entities

20 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 Hands-On Current is not a variable and 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 be assigned to the same reason for other.y Current is not a variable and thus can not be used in creation

21 Structured programming  In structured programming instructions can be combined only in three ways (constructs): s_1 c s_2 s_1s_2 c s sequential composition conditionalloop TrueFalse True  Each of these blocks has a single entry and exit and is itself a (possibly empty) compound Compound Condition

22 Conditional  Basic syntax: if c then s_1 else s_2 end  Could c be an integral expressions?  No. c is a boolean expression (e.g., entity, query call of type BOOLEAN)  Are these valid conditionals? Condition Compound if c then s_1 end if c then end if c then else end Yes, else is optionalYes, s_1 could be empty. Yes, s_1 and s_2 could be both empty.

23 Calculating function’s value f (max : INTEGER ; s : STRING): STRING do if s.is_equal (“Java”) then Result := “J**a” else if s.count > max then Result := “ ” end Calculate the value of:  f (3, “Java”)  f (20, “Immatrikulationsbestätigung”)  f (6, “Eiffel”) → “J**a” → “ ” → Void Hands-On

24 Write a routine... ... that computes the maximum of two integers max (a, b : INTEGER) : INTEGER ... that increases time by one second inside class TIME class TIME hour, minute, second : INTEGER second_forth do... end... end Hands-On

25 Comb-like conditional If there are more than two alternatives, you can use the syntax: if c_1 then s_1 elseif c_2 then s_2... elseif c_n then s_n else s_e end instead of: if c_1 then s_1 else if c_2 then s_2 else... if c_n then s_n else s_e end... end Condition Compound

26 Multiple choice If all the conditions have a specific structure, you can use the syntax: inspect expression when const_1 then s_1 when const_2 then s_2... when const_n1.. const_n2 then s_n else s_e end Integer or character expression Integer or character constant Compound Interval

27 Lost in conditions Rewrite the following multiple choice:  using a comb-like conditional  using nested conditionals inspect user_choice when 0 then print (“Hamburger”) when 1 then print (“Coke”) else print (“Not on the menu!”) end if user_choice = 0 then print (“Hamburger”) elseif user_choice = 1 then print (“Coke”) else print (“Not on the menu !”) end if user_choice = 0 then print (“Hamburger”) else if user_choice = 1 then print (“Coke”) else print (“Not on the menu!”) end Hands-On

28 Loop: Basic form Compound Boolean expression Compound Syntax: from initialization until exit_condition loop body end

29 Compilation error? Runtime error? f (x, y : INTEGER): INTEGER do from until (x // y) loop "Print me!" end Hands-On Compilation error: integer expression instead of boolean Compilation error: expression instead of instruction Correct f (x, y : INTEGER) : INTEGER local i : INTEGER do from i := 1 until (True) loop i := i * x * y end Correct, but non-terminating f do from until False loop end

30 Simple loop How many times will the body of the following loop be executed? Hands-On In Eiffel we usually start counting from 1 10 i : INTEGER... from i := 1 until i > 10 loop print (“ I will not say bad things about assistants”) i := i + 1 end … from i := 10 until i < 1 loop print (“ I will not say bad things about assistants”) end Caution! Loops can be infinite! ∞

31 What does this function do? (n : INTEGER) : INTEGER require n >= 0 local i : INTEGER do from i := 2 Result := 1 until i > n loop Result := Result * i i := i + 1 end f Hands-On factorial

32 invariant inv Loop: More general form Compound Optional Boolean expression Compound variant var Syntax: from initialization until exit_condition loop body end Integer expression Optional

33 Invariant and variant Loop invariant (do not confuse with class invariant)  holds before and after the execution of loop body  captures how the loop iteratively solves the problem: e.g. “to calculate the sum of all n elements in a list, on each iteration i (i = 1..n) the sum of first i elements is obtained” Loop variant  integer expression that is nonnegative after execution of from clause and after each execution of loop clause and strictly decreases with each iteration  a loop with a correct variant can not be infinite (why?)

34 What are the invariant and variant of the “factorial” loop? from i := 2 Result := 1 invariant ? until i > n loop Result := Result * i i := i + 1 variant ? end i = 2; Result = 1 = 1!i = 3; Result = 2 = 2!i = 4; Result = 6 = 3! Invariant and variant Hands-On Result = factorial (i - 1) n – i + 2

35 Writing loops Implement a function that calculates Fibonacci numbers, using a loop fibonacci (n : INTEGER) : INTEGER -- n-th Fibonacci number require n_non_negative : n >= 0 ensure first_is_zero : n = 0 implies Result = 0 second_is_one : n = 1 implies Result = 1 other_correct : n > 1 implies Result = fibonacci (n - 1) + fibonacci (n - 2) end Hands-On

36 Writing loops (solution) fibonacci (n : INTEGER) : INTEGER local a, b, i : INTEGER do if n <= 1 then Result := n else from a := 0 b := 1 i := 1 until i = n loop Result := a + b a := b b := Result i := i + 1 end Hands-On invariant variant a = fibonacci (i - 1) b = fibonacci (i ) n - i

37 Summary  Attributes, formal arguments, and local variables  Scope  Control structures