© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.

Slides:



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

© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Intermediate Code Generation
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
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 Lecture 3: Abstract Data Types.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Object-Oriented Software Construction Bertrand Meyer 2nd ed., Prentice Hall, 1997.
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 9 Imperative and object-oriented languages 1.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 8: More on inheritance.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 4: Objects.
-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 OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 20: Some design principles.
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.
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 OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 16, 26 May Advanced Topics in Object Technology 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 OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Chair of Software Engineering ATOT - Lecture 3, 7 April Advanced Topics in Object Technology Bertrand Meyer.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Linear Systems The definition of a linear equation given in Chapter 1 can be extended to more variables; any equation of the form for real numbers.
Ranga Rodrigo. Class is central to object oriented programming.
10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
© 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.
© 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 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Section 2.2 More about Solving Equations. Objectives Use more than one property of equality to solve equations. Simplify expressions to solve equations.
Copyright © 2015, 2011, 2007 Pearson Education, Inc. 1 1 Chapter 8 Rational Exponents, Radicals, and Complex Numbers.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
ADDING AND SUBTRACTING RATIONAL EXPRESSIONS: TO ADD OR SUBTRACT RATIONAL EXPRESSIONS USE THE ADDITION PROPERTY:
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
2-Step Equations What??? I just learned 1-step! Relax. You’ll use what you already know to solve 2-step equations.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
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.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
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
Multiplying, Dividing, and Simplifying Radicals
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Introduction to the Lab
Presentation transcript:

© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice Hall). Included here by permission of ISE, for the benefit of IDC students. Other uses, duplication or distribution require permission from ISE. For more material see

© Bertrand Meyer and Yishai Feldman Finding the Classes: Pitfalls u The Grand Mistake: a class that isn't u “This class performs…” u Imperative names u Single-routine classes u Premature classification u No-command classes u Mixed abstractions

© Bertrand Meyer and Yishai Feldman Class Name Rule A class name must always be either: u a noun, possibly qualified: TREE, LINKED_LIST, LINE_DELETION u an adjective (only for a deferred class describing a structural property): COMPARABLE, NUMERIC

© Bertrand Meyer and Yishai Feldman Side Effects A function produces a concrete side effect if its body contains any of the following: u an assignment, assignment attempt, or creation instruction whose target is an attribute; u a procedure call. An abstract side effect is a concrete side effect that can change the value of a non- secret query.

© Bertrand Meyer and Yishai Feldman Referential Transparency An expression e is referentially transparent if it is possible to exchange any subexpression with its value without changing the value of e. attr: INTEGER sneaky: INTEGER is do attr := attr + 1 end attr := 0 ; if attr /= 0 then print ("Something bizarre!") end

© Bertrand Meyer and Yishai Feldman Commnad-Query Separation Principle Functions should not produce abstract side effects.

© Bertrand Meyer and Yishai Feldman Example: Reading an Integer u C Style: getint () getint ()  getint () 2 * getint ()  getint () + getint () u Eiffel Style: advance, last_integer input: FILE input. advance input. last_integer = input. last_integer 2 * input. last_integer = input. last_integer + input. last_integer

© Bertrand Meyer and Yishai Feldman Example: Random Numbers u Create a new sequence: !!rand. make (seed) u Advance to next value: rand. forth u Obtain current value: xx := rand. item

© Bertrand Meyer and Yishai Feldman Abstract and Concrete State max is -- The highest value of items in the list require not empty local original_index: INTEGER do original_index := index from start ; Result := item until is_last loop forth ; Result := Result. max (item) end go (original_index) end

© Bertrand Meyer and Yishai Feldman Example: Complex Numbers class COMPLEX feature  Feature declarations for: infix "+", infix "–", infix "  ", infix "/", add, subtract, multiply, divide, x, y, rho, theta,  feature {COMPLEX} -- implementation cartesian_ready: BOOLEAN polar_ready: BOOLEAN private_x, private_y, private_rho, private_theta: REAL invariant cartesian_ready or polar_ready polar_ready implies (0 <= private_theta and private_theta <= Two_pi) -- cartesian_ready implies (private_x and private_ y are up to date) -- polar_ready implies (private_rho and private_theta are up to date) end

© Bertrand Meyer and Yishai Feldman Changing Representations (1) prepare_cartesian is -- Make cartesian representation available do if not cartesian_ready then check polar_ready end -- (Because the invariant requires at least one of the -- two representations to be up to date) private_x := private_rho  cos (private_theta) private_y := private_rho  sin (private_theta) cartesian_ready := True -- Here both cartesian_ready and polar_ready are true: -- Both representations are available end ensure cartesian_ready end

© Bertrand Meyer and Yishai Feldman Changing Representations (2) prepare_polar is -- Make polar representation available do if not polar_ready then check cartesian_ready end -- (Because the invariant requires at least one of the -- two representations to be up to date) private_rho := sqrt (private_x ^ 2 + private_y ^ 2) private_theta := atan2 (private_y, private_x) polar_ready := True -- Here both cartesian_ready and polar_ready are true: -- Both representations are available end ensure polar_ready end

© Bertrand Meyer and Yishai Feldman Creating Complex Numbers make_cartesian (a, b: REAL) is -- Initialize with abscissa a, ordinate b. do private_x := a ; private_ y := b cartesian_ready := True ; polar_ready := False ensure cartesian_ready ; not polar_ready end

© Bertrand Meyer and Yishai Feldman Complex Addition add (other: COMPLEX) is -- Add the value of other. do prepare_cartesian; polar_ready := False private_x := x + other. x ; private_y = y + other. y ensure x = old x + other. x ; y = old y + other. y cartesian_ready ; not polar_ready end

© Bertrand Meyer and Yishai Feldman Complex Division divide (z: COMPLEX) is -- Divide by z. require z. rho /= 0 do prepare_polar ; cartesian_ready := False private_rho := rho / other. rho private_theta = (theta – other. theta) \\ Two_pi ensure rho = old rho / other. rho theta = (old theta — other. theta) \\ Two_pi polar_ready ; not cartesian_ready end

© Bertrand Meyer and Yishai Feldman Queries with Side Effects x: REAL is -- Abscissa do prepare_cartesian ; Result := private_x end theta: REAL is -- Angle do prepare_polar ; Result := private_theta end

© Bertrand Meyer and Yishai Feldman Operand and Option Arguments  An operand argument to a routine represents an object on which the routine will operate.  An option argument represents a mode of operation.  An argument is an option if, assuming the client had not supplied its value, it would have been possible to find a reasonable default.  In the evolution of a class, operands tend to remain the same, but options may be added and removed.

© Bertrand Meyer and Yishai Feldman Operand Principle The arguments of a routine should only include operands (no options). my_document. print (printer_name, paper_size, color_or_not, postscript_level, print_resolution) my_document. set_printing_size ("A4") my_document. set_color my_document. print  

© Bertrand Meyer and Yishai Feldman A Checklist

© Bertrand Meyer and Yishai Feldman Class Size: Shopping List Advice When considering the addition of a new exported feature to a class, observe the following rules: S1The feature must be relevant to the data abstraction represented by the class. S2It must be compatible with the other features of the class. S3It must not address exactly the same goal as another feature of the class. S4It must maintain the invariant of the class.