1
play

1 Announcement: classroom exercise 4 First classroom exercise in - PDF document

1 Introduction to Programming Bertrand Meyer Last revised 5 November 2004 Chair of Softw are Engineering I ntroduction to Programming Lecture 7 2 Lecture 7: References, assignment, and the object structure Chair of Softw are


  1. 1 Introduction to Programming Bertrand Meyer Last revised 5 November 2004 Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 2 Lecture 7: References, assignment, and the object structure Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Announcement: lab sessions 3 � Every Friday (12 November - 17 December 2004) � IFW D31, 12: 00 a.m. - 2: 00 p.m. � During the second hour there will be two assistants answering your questions Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 1

  2. Announcement: classroom exercise 4 � First classroom exercise in next week’s exercise session. � Either on Monday, 22 November 2004, or Tuesday, 23 November 2004. Your assistant will indicate your date. � There is no weekly exercise in this week. Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Reminder: field study 5 � You are encouraged to take part in Marie-Helene Ng Cheong Vee’s field study about learning programm ing. � Just subm it your Eiffel log files regularly. � For any questions contact your assistant. Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Classes and objects 6 � At run time: objects (software machines) � In the program text: classes Each class describes a set of possible run-time objects. Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 2

  3. Object structure 7 An object is made of fields Each field is a value, which is either: � A basic value: integer, character, “real” number... (known as an expanded value) � A reference to another object ( METRO_STOP ) x next upper_ left segment 23.5 ( SEGMENT ) y station lower_ right ( POSITION ) -34.9 ( METRO_STOP ) ( METRO_STATION ) ( POSITION ) Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Cycles in the reference structure 8 O1 name “Almaviva” landlord loved_one O3 O2 name “Figaro” “Susanna” name landlord landlord loved_one loved_one Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Strings are objects 9 O1 ‘A’ name “Almaviva” ‘l’ landlord ‘m’ ‘a’ loved_one ‘v’ ‘i’ ‘v’ ‘a’ The nam e field is a reference field Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 3

  4. Fields reflect attributes of the class 10 class An attribute POSITION feature – Access x: REAL -- Horizontal position y: REAL Another attribute -- Vertical position feature – Element change set_position (xval, yval: REAL) is -- Set coordinates to ( ` xval', ` yval' ). require x_positive: xval > = 0 y_positive: yval > = 0 do Attributes are features of x : = xval the class y : = yval ensure x_set: x = xval y_set: y = yval end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Setting fields (in routines of the class) 11 class POSITION feature – Access x: REAL -- Horizontal position y: REAL -- Vertical position feature – Element change set (xval, yval: REAL) is -- Set coordinates to ( ` xval', ` yval' ). require x_positive: xval > = 0 y_positive: yval > = 0 do x : = xval y : = yval ensure x_set: x = xval y_set: y = yval end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 What you may do 12 size class METRO_STATI ON feature x, y: REAL Station -- Coordinates of metro station size: REAL -- Size of bounding square bounding square upper_left: POSITI ON -- Upper-left position of bounding square adjust_positions is -- Set positions of bounding square do upper_left . set ( x – size/ 2, y + size/ 2 ) ... end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 4

  5. Feature calls 13 � A feature of class POSITION : set (xval, yval: REAL) is ... do ... end � From class POSITION itself: move (dx, dy: REAL) is -- Move by dx horizontally, dy vertically require ... [ Please com plete!] ... do Unqualified call set ( x + dx, y + dy ) ensure ... [ Please com plete!] ... end � From another class, e.g. METRO_STATION Qualified call adjust_positions is do upper_left . set ( x – size/ 2, y + size/ 2 ) ... end upper_left : POSITION Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 The client relation 14 Class METRO_STATION , having a feature upper_left : POSITION (and calls of the form upper_left . set (...) ) is a client of class POSITION Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Client and inheritance, graphically 15 Client Inheritance Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 5

  6. What clients may do 16 class METRO_STATI ON feature x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square upper_left: POSITI ON -- Upper-left position of bounding square adjust_positions is -- Set positions of bounding square do upper_left . set ( x – size/ 2, y + size/ 2 ) ... end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 What clients may not do 17 class METRO_STATI ON feature x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square upper_left: POSITI ON -- Upper-left position of bounding square NOT PERMITTED! adjust_positions is -- Set positions of bounding square do upper_left . x : = 3 ... end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Use procedures: 18 upper_left . set (3, upper_left . y ) upper_left . set_x (3 ) Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 6

  7. An object has an interface 19 set x y set_x set_y Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 An object has an implementation 20 set x y set_x set_y Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Information hiding 21 set x y set_x set_y Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 7

  8. The Principle of Uniform Access 22 Features should be accessible to clients in the same way whether implemented by storage or by computation Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Uniform Access: an example 23 balance = list_of_deposits . total – list_of_withdrawals . total list_of_deposits (A1) list_of_withdrawals balance list_of_deposits (A2) list_of_withdrawals Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 The Principle of Uniform Access 24 Features should be accessible to clients in the same way whether implemented by storage or by computation Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 8

  9. Feature categories by role 25 Command Procedure No result Feature Function Computation Returns result Query Attribute Memory Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Feature categories by implementation 26 Procedure No result Routine Computation Returns result Feature Function Memory Attribute Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Feature categories 27 Command Procedure No result No result Routine Computation Returns Feature result Feature Function Computation Returns result Memory Query Attribute Memory Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 9

  10. Entities 28 An entity is a name in the program that denotes possible run-tim e values Some entities are constant Others are variable : � Attributes � Local variables Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Reference game: classes 29 class PERSON create class STATI ON feature make name: STRI NG feature name: STRI NG make (n: STRI NG) friend, enemy: PERSON -- Initialize with name n . station: STATI ON require n / = Void set_friend (f: PERSON) ensure require name = n f / = Void end ensure friend = f indexing set_enemy… description: “List items” set_station… class LI NKABLE feature item: PERSON make (n: STRI NG) right: LI ST_I TEM -- Initialize with name n . end require n / = Void ensure name = n end Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 Reference game: example program 30 create michela.make ("MICHELA") create stephanie.make ("STEPHANIE") stephanie.set_friend (michela) michela.set_enemy (michela) create persons persons.extend (michela) persons.extend (stephanie) persons.i_th (2).item.set_enemy (michela) persons.start persons.forth persons.remove Chair of Softw are Engineering I ntroduction to Programming – Lecture 7 10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend