Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008."— Presentation transcript:

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

2 Chair of Software Engineering 2 Today’s learning goals  A bit of logic  Understanding contracts (preconditions, postconditions, and class invariants)

3 Chair of Software Engineering Logic  Tautology  True for all truth assignments  a or (not a)  not (a and (not a))  (a and b) or ((not a) or (not b))  Contradiction  False for all truth assignments  a and (not a) 3

4 Chair of Software Engineering Logic  Satisfiable  True for at least one truth assignment  Equivalent  u and v are equivalent if they are satisfied under exactly the same truth assignments, or if u = v is a tautology 4

5 Chair of Software Engineering 5 Truth tables Does the following equivalence hold? (P implies Q) = (not P implies not Q) Hands-On

6 Chair of Software Engineering 6 Tautology / contradiction / satisfiable?  P or Q satisfiable  P and Q satisfiable  P or (not P) tautology  not P satisfiable  Integer (i) implies (i = 0) tautology Hands-On

7 Chair of Software Engineering 7 Useful stuff De Morgan laws  not (P or Q) = (not P) and (not Q)  not (P and Q) = (not P) or (not Q) Implications  P implies Q= (not P) or Q  P implies Q = (not Q) implies (not P) Equality on Boolean expressions  (P = Q) = (P implies Q) and (Q implies P)

8 Chair of Software Engineering 8 Existential and universal quantification  There exists a human whose name is Bill Gates  h: Human | h.name = “Bill Gates”  All persons have a name  p: Person | p.name /= Void  Some people are students  p: Person | p.is_student  The age of any person is at least 0  p: Person | p.age >= 0  Nobody likes Rivella not (  p: Person | p.likes (Rivella))

9 Chair of Software Engineering From the lecture… Semi-strict operators ( and then, or else )  a and then b  has same value as ( a and b ) if a and b are defined, and has value False whenever a has value False.  a or else b  has same value as ( a or b ) if a and b are defined, and has value True whenever a has value True. 9

10 Chair of Software Engineering 10 Assertions balance_non_negative: balance >= 0 Assertion Assertion tag (not required, but recommended) Condition (required)

11 Chair of Software Engineering clap (n: INTEGER) -- Clap n times and update count. require not_too_tired: count <= 10 n_positive: n > 0 Property that a feature imposes on every client A feature with no require clause is always applicable, as if the precondition reads require always_OK: True 11 Precondition

12 Chair of Software Engineering Property that a feature guarantees on termination A feature with no ensure clause always satisfies its postcondition, as if the postcondition reads ensure always_OK: True clap (n: INTEGER) -- Clap n times and update count. require not_too_tired: count <= 10 n_positive: n > 0 ensure count_updated: count = old count + n 12 Postcondition

13 Chair of Software Engineering 13 Obligations and guarantees  Precondition  Obligation on client: I must not call unless …  Guarantee for supplier: I know that … when I start executing  Postcondition  Obligation on supplier: I must establish … when I terminate  Guarantee for client: I can assume … after the call

14 Chair of Software Engineering 14 Invariant Property that is true of the current object at any observable point A class with no invariant clause has a trivial invariant always_OK: True class ACROBAT … invariant count_non_negative: count >= 0 end

15 Chair of Software Engineering 15 Pre- and postcondition example Add pre- and postconditions to: square_root (i: INTEGER): REAL -- Square root of `i’ -- with precision `eps’ (given somewhere else). do... end Hands-On

16 Chair of Software Engineering 16 One possible solution square_root (i: INTEGER): REAL is -- Square root of `i’ -- with precision `eps’ (given somewhere else) require i_positive: i >= 0 ensure correct_result: i – eps <= Result * Result and i + eps >= Result * Result end

17 Chair of Software Engineering 17 Hands-on exercise  Add invariants to classes ACROBAT_WITH_BUDDY and CURMUDGEON.  Add preconditions and postconditions to feature make_with_buddy in ACROBAT_WITH_BUDDY. Hands-On


Download ppt "Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 6 7 October 2008."

Similar presentations


Ads by Google