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

Slides:



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

Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
© 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 5 6 October 2008.
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 Lecture 3: Abstract Data Types.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
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 Lecture 2: Dealing with Objects I.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Principles of Object-Oriented Software Development The language Eiffel.
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 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 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 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 Lecture 9: Abstraction.
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 Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
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 12: More on references and.
Chair of Software Engineering 1 Introduction to Programming Tuples and Agents Exercise Session 1 and 2 December 2008.
Eiffel: Analysis, Design and Programming Bertrand Meyer Chair of Software Engineering.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
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 3.
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 9.
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 20: Multiple inheritance.
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.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
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 6.
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Chair of Software Engineering Complement to lecture 11 : Levenshtein.
© 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 6.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
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
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

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

2 News (Reminder) Mock exam next week!  Monday exercise groups: November 7  Tuesday exercise groups: November 8  You have to be present  The week after we will discuss the results  Assignment 7 due on November 15

3 Today  Inheritance  Genericity  Exercise: practicing contracts

4 Inheritance Principle: Describe a new class as extension or specialization of an existing class (or several with multiple inheritance) If B inherits from A :  As modules: all the services of A are available in B (possibly with a different implementation)  As types: whenever an instance of A is required, an instance of B will be acceptable (“is-a” relationship)

5 Let's play Lego! BRICK LEGO_BRICK LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED

6 Class BRICK deferred class BRICK feature width: INTEGER depth: INTEGER height: INTEGER color: COLOR volume: INTEGER deferred end

7 Class LEGO_BRICK class LEGO_BRICK inherit BRICK feature number_of_nubs: INTEGER do Result :=... end volume: INTEGER do Result :=... end Inherit all features of class BRICK. New feature, calculate all nubs Implementation of volume.

8 Class LEGO_BRICK_SLANTED The feature volume is going to be redefined (=changed). The feature volume comes from LEGO_BRICK class LEGO_BRICK_SLANTED inherit LEGO_BRICK redefine volume end feature volume: INTEGER do Result :=... end

9 Class LEGO_BRICK_WITH_HOLE class LEGO_BRICK_WITH_HOLE inherit LEGO_BRICK redefine volume end feature volume: INTEGER do Result :=... end The feature volume is going to be redefined (=changed). The feature volume comes from LEGO_BRICK

10 Inheritance Notation volume++ BRICK LEGO_BRICK LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED volume* volume+ * volume++ Notation: Deferred * Effective + Redefinition ++

11 Deferred  Deferred  Deferred classes can have deferred features.  A class with at least one deferred feature must be declared as deferred.  A deferred feature does not have an implementation yet.  Deferred classes cannot be instantiated and hence cannot contain a create clause.

12 Effective  Effective  Effective classes do not have deferred features (the “standard case”).  Effective routines have an implementation of their feature body.

13 Precursor  If a feature was redefined, but you still wish to call the old one, use the Precursor keyword. volume: INTEGER do Result := Precursor -... end

14 A more general example of using Precursor -- Class A routine (a_arg1 : TYPE_A): TYPE_R do … end -- Class C routine (a_arg1 : TYPE_A): TYPE_R local l_loc : TYPE_R do -- pre-process l_loc := Precursor {B} (a_arg1 ) -- Not allowed: l_loc := Precursor {A} (a_arg1 ) -- post-process end routine + + A routine ++ + B routine ++ + C

15 Today  Inheritance  Genericity  Exercise: practicing contracts

16 Genericity  Genericity lets you parameterize a class. The parameters are types. A single class text may be reused for many different types.

17 Genericity LIST_OF_ CARS SET_OF_ CARS LINKED_LIST_ OF_CARS LIST_OF_ CITIES LIST_OF_ PERSONS Abstraction Specialization Type parameterization Genericity Inheritance

18 A generic list Formal generic parameter Actual generic parameter class LIST [ G ] feature extend (x : G )... last : G... end To use the class: obtain a generic derivation, e.g. cities : LIST [ CITY ]

19 ] -> RESOURCE A generic list with constraints class STORAGE [G inherit LIST [G] feature consume_all do from start until after loop item.consume forth end constrained generic parameter The feature item is checked for conformance with RESOURCE. We can assume this. The feature item is of type G. We cannot assume consume.

20 Type-safe containers  Using genericity you can provide an implementation of type safe containers. x: ANIMAL animal_list: LINKED_LIST [ANIMAL] a_rock: MINERAL animal_list.put (a_rock) -- Does this rock?

21 Definition: Type We use types to declare entities, as in x : SOME_TYPE With the mechanisms defined so far, a type is one of:  A non-generic class e.g.METRO_STATION  A generic derivation, i.e. the name of a class followed by a list of types, the actual generic parameters, in brackets (also recursive) e.g.LIST [ARRAY [METRO_STATION ]] LIST [LIST [CITY ]] TABLE [STRING, INTEGER]

22 So, how many types can I possibly get? Two answers, depending on what we are talking about:  Static types Static types are the types that we use while writing Eiffel code to declare types for entities (arguments, locals, return values)  Dynamic types Dynamic types on the other hand are created at run- time. Whenever a new object is created, it gets assigned to be of some type.

23 Static types class EMPLOYEE feature name: STRING birthday: DATE end class DEPARTMENT feature staff: LIST [EMPLOYEE] end bound by the program text: EMPLOYEE STRING DATE DEPARTMENT LIST[G] becomes LIST[EMPLOYEE]

24 Object creation, static and dynamic types class TEST_DYNAMIC _CREATION feature ref_a: A; ref_b: B -- Suppose B, with creation feature make_b, -- inherits from A, with creation feature make_a do_something do create ref_a.make_a -- Static and dynamic type is A create {B} ref_a.make_b -- Static type is A, dynamic type is B end

25 Dynamic types: another example class SET[G] feature powerset: SET[SET[G]] is do create Result -- More computation… end i_th_power (i: INTEGER): SET[ANY] require i >= 0 local n: INTEGER do Result := Current from n := 1 until n > i loop Result := Result.powerset n := n + 1 end Dynamic types from i_th_power : SET[ANY] SET[SET[ANY]] SET[SET[SET[ANY]]] … From

26 Today  Inheritance  Genericity  Exercise: practicing contracts

27 Specification of a card game A deck is initially made of 36 cards Every card in the deck represents a value in the range Every card also represents 1 out of 4 possible colors The colors represented in the game cards are: red (‘R’), white (‘W’), green (‘G’) and blue (‘B’) As long as there are cards in the deck, the players can look at the top card and remove it from the deck

28 Class CARD create make make (a_color: CHARACTER, a_value: INTEGER) -- Create a card given a color and a value. require... ensure... color: CHARACTER -- The card color. value: INTEGER -- The card value. Hands-On

29 Class CARD: which colors are valid? is_valid_color (a_color: CHARACTER): BOOLEAN -- Is `a_color’ a valid color? require... ensure... Hands-On

30 Class CARD: which ranges are valid? is_valid_range (n: INTEGER): BOOLEAN -- Is `n’ in the acceptable range? require... ensure... invariant... Hands-On

31 Class CARD create make: reloaded make (a_color: CHARACTER, a_value: INTEGER) -- Create a card given a color and a value. require... ensure... color: CHARACTER -- The card color. value: INTEGER -- The card value. Hands-On

32 Class DECK create make make -- Create a deck with random cards. require... ensure... feature {NONE} –- Implementation card_list: LINKED_LIST [CARD] -- Deck as a linked list of cards. Hands-On

33 Class DECK queries top_card: CARD -- The deck’s top card. is_empty: BOOLEAN -- Is Current deck empty? do … end count: INTEGER -- Number of remaining cards in the deck. do … end Hands-On

34 Removing the top card from DECK remove_top_card -- Remove the top card from the deck. require... ensure... Hands-On

35 The class invariant invariant... Hands-On