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

1
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

I ntroduction to Programming – Lecture 7 1 Chair of Softw are Engineering

Introduction to Programming

Bertrand Meyer

Last revised 5 November 2004

I ntroduction to Programming – Lecture 7 2 Chair of Softw are Engineering

Lecture 7: References, assignment, and the object structure

I ntroduction to Programming – Lecture 7 3 Chair of Softw are Engineering

Announcement: lab sessions

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

slide-2
SLIDE 2

2

I ntroduction to Programming – Lecture 7 4 Chair of Softw are Engineering

Announcement: classroom exercise

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.

I ntroduction to Programming – Lecture 7 5 Chair of Softw are Engineering

Reminder: field study

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.

I ntroduction to Programming – Lecture 7 6 Chair of Softw are Engineering

Classes and objects

At run time: objects (software machines) In the program text: classes Each class describes a set of possible run-time

  • bjects.
slide-3
SLIDE 3

3

I ntroduction to Programming – Lecture 7 7 Chair of Softw are Engineering

Object structure

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) station next segment (METRO_STOP) (SEGMENT) upper_ left (METRO_STATION) lower_ right (POSITION) x 23.5 y

  • 34.9

(POSITION)

I ntroduction to Programming – Lecture 7 8 Chair of Softw are Engineering

Cycles in the reference structure

O1

“Almaviva”

name landlord loved_one

“Figaro” “Susanna”

O3 O2

landlord loved_one name landlord loved_one name

I ntroduction to Programming – Lecture 7 9 Chair of Softw are Engineering

Strings are objects

The nam e field is a reference field

O1

“Almaviva”

name landlord loved_one ‘A’ ‘l’ ‘m’ ‘a’ ‘v’ ‘i’ ‘v’ ‘a’

slide-4
SLIDE 4

4

I ntroduction to Programming – Lecture 7 10 Chair of Softw are Engineering

Fields reflect attributes of the class

class POSITION feature – Access x: REAL

  • - Horizontal position

y: REAL

  • - 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

x : = xval y : = yval

ensure x_set: x = xval y_set: y = yval end end

An attribute Another attribute

Attributes are features of the class

I ntroduction to Programming – Lecture 7 11 Chair of Softw are Engineering

Setting fields (in routines of the class)

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

I ntroduction to Programming – Lecture 7 12 Chair of Softw are Engineering

What you may do

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 Station bounding square size

slide-5
SLIDE 5

5

I ntroduction to Programming – Lecture 7 13 Chair of Softw are Engineering

Feature calls

  • 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 set (x + dx, y + dy) ensure ... [ Please com plete!] ... end

  • From another class, e.g. METRO_STATION

adjust_positions is do upper_left . set (x – size/ 2, y + size/ 2) ... end upper_left: POSITION

Unqualified call Qualified call

I ntroduction to Programming – Lecture 7 14 Chair of Softw are Engineering

The client relation

Class METRO_STATION, having a feature upper_left: POSITION (and calls of the form upper_left . set (...) ) is a client of class POSITION

I ntroduction to Programming – Lecture 7 15 Chair of Softw are Engineering

Client and inheritance, graphically

Inheritance Client

slide-6
SLIDE 6

6

I ntroduction to Programming – Lecture 7 16 Chair of Softw are Engineering

What clients may do

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

I ntroduction to Programming – Lecture 7 17 Chair of Softw are Engineering

What clients may not do

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 . x : = 3 ... end end NOT PERMITTED!

I ntroduction to Programming – Lecture 7 18 Chair of Softw are Engineering

Use procedures:

upper_left . set (3, upper_left . y) upper_left . set_x (3)

slide-7
SLIDE 7

7

I ntroduction to Programming – Lecture 7 19 Chair of Softw are Engineering

An object

set set_x set_y x y

has an interface

I ntroduction to Programming – Lecture 7 20 Chair of Softw are Engineering

An object

set set_x set_y x y

has an implementation

I ntroduction to Programming – Lecture 7 21 Chair of Softw are Engineering

Information hiding

set set_x set_y x y

slide-8
SLIDE 8

8

I ntroduction to Programming – Lecture 7 22 Chair of Softw are Engineering

The Principle of Uniform Access Features should be accessible to clients in the same way whether implemented by storage or by computation

I ntroduction to Programming – Lecture 7 23 Chair of Softw are Engineering

Uniform Access: an example

balance = list_of_deposits.total – list_of_withdrawals.total

list_of_deposits list_of_withdrawals balance list_of_deposits list_of_withdrawals (A2) (A1)

I ntroduction to Programming – Lecture 7 24 Chair of Softw are Engineering

The Principle of Uniform Access Features should be accessible to clients in the same way whether implemented by storage or by computation

slide-9
SLIDE 9

9

I ntroduction to Programming – Lecture 7 25 Chair of Softw are Engineering

Feature categories by role

Command Query Feature Procedure Attribute Function

No result Returns result Computation Memory

I ntroduction to Programming – Lecture 7 26 Chair of Softw are Engineering

Feature categories by implementation

Procedure Attribute Function Routine

Returns result No result

Feature

Memory Computation

I ntroduction to Programming – Lecture 7 27 Chair of Softw are Engineering

Feature categories

Command Query Feature Procedure Attribute Function

No result Returns result Computation Memory

Routine

Returns result No result

Feature

Memory Computation

slide-10
SLIDE 10

10

I ntroduction to Programming – Lecture 7 28 Chair of Softw are Engineering

Entities

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

I ntroduction to Programming – Lecture 7 29 Chair of Softw are Engineering

Reference game: classes

class PERSON create make feature name: STRI NG friend, enemy: PERSON station: STATI ON set_friend (f: PERSON) require f / = Void ensure friend = f set_enemy… set_station… make (n: STRI NG)

  • - Initialize with name n.

require n / = Void ensure name = n end class STATI ON feature name: STRI NG make (n: STRI NG)

  • - Initialize with name n.

require n / = Void ensure name = n end indexing description: “List items” class LI NKABLE feature item: PERSON right: LI ST_I TEM end

I ntroduction to Programming – Lecture 7 30 Chair of Softw are Engineering

Reference game: example program

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

slide-11
SLIDE 11

11

I ntroduction to Programming – Lecture 7 31 Chair of Softw are Engineering

Reference game: real program

create michela.make ("MI CHELA") create stephanie.make ("STEPHANI E") create till.make ("TI LL") create volkan.make ("VOLKAN") create sebastien.make ("SEBASTIEN") create matthias.make ("MATTHI AS") create robert.make ("ROBERT") create rolf.make ("ROLF") create stephan.make ("STEPHAN") create sebastian.make ("SEBASTIAN") create haldenegg.make ("HALDENEGG") create paradeplatz.make ("PARADEPLATZ") create buerkliplatz.make ("BUERKLI PLATZ") create buchegg.make ("BUCHEGG") create bellevue.make ("BELLEVUE") create persons michela.set_station (haldenegg) michela.set_friend (rolf) michela.set_enemy (till) persons.extend (rolf) persons.extend (sebastian) stephanie.set_station (haldenegg) rolf.set_station (bellevue) rolf.set_friend (persons.i_th (1).item) stephanie.set_friend (rolf) stephanie.set_enemy (rolf) persons.extend (stephanie) persons.extend (michela) persons.i_th (1).item.set_friend (stephanie) persons.i_th (1).item.set_enemy (rolf) persons.i_th (persons.count - 1).item.set_station (paradeplatz) persons.i_th (persons.count).item.enemy.set_friend (persons.i_th (2)) persons.extend (volkan) persons.extend (matthias) persons.i_th (persons.count).item.set_station (paradeplatz) persons.start persons.forth persons.forth persons.forth persons.remove

I ntroduction to Programming – Lecture 7 32 Chair of Softw are Engineering

Reference game: colors

Links between people (next in the persons list): YELLOW friend: BLUE enemy: RED station: GREEN item (in list elements): ORANGE Source of reference is hand, target of reference is waist!

I ntroduction to Programming – Lecture 7 33 Chair of Softw are Engineering

End of lecture 7