Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Chair of Software Engineering Today’s learning goals  Entities vs. objects  Object creation  Executing a system 2

3 Chair of Software Engineering 3 Entity vs. object (COURSE) Generating class MEMORY Fields In the class text: an entity joe: STUDENT In memory, during execution: an object (ASSISTANT) (STUDENT) (MARK) (PROFESSOR)

4 Chair of Software Engineering class INTRODUCTION_TO_PROGRAMMING inherit COURSE feature execute is -- Teach `joe’ programming. do -- ??? joe.solve_all_assignments end joe: STUDENT -- A first year computer science student end 4 INTRODUCTION_TO_PROGRAMMING

5 Chair of Software Engineering 5 Initial state of a reference In an instance of INTRODUCTION_TO_PROGRAMMING, may we assume that joe is attached to an instance of STUDENT? joe ( STUDENT ) reference ( INTRODUCTION_ TO_PROGRAMMING ) Where does this one come from? This object has been created (by someone else...) MEMORY

6 Chair of Software Engineering 6 By default Initially, joe is not attached to any object: its value is a void reference. joe void reference ( INTRODUCTION_ TO_PROGRAMMING )

7 Chair of Software Engineering 7 States of a reference During execution, a reference can be:  Attached to a certain object  Void

8 Chair of Software Engineering 8 To denote a void reference: use Void keyword To create a new object in memory and attach it to x: use create keyword create x To find out if x is void: use the expressions x = Void (true iff x is void) x /= Void (true iff x is attached) States of a reference

9 Chair of Software Engineering 9 Those mean void references! The basic mechanism of computation is feature call x. f (a, …) Since references may be void, x might be attached to no object The call is erroneous in such cases! Apply feature f To object to which x is attached Possibly with arguments

10 Chair of Software Engineering 10 Why do we need to create objects? Shouldn’t we assume that a declaration joe: STUDENT creates an instance of STUDENT and attaches it to joe?

11 Chair of Software Engineering 11 Those wonderful void references! (PERSON) spouse Married persons: (PERSON) spouse Unmarried person:

12 Chair of Software Engineering 12 Those wonderful void references! Last next reference is void to terminate the list. ( STOP ) next ( STOP )

13 Chair of Software Engineering 13 Type of a created object Every entity is declared with a certain type: stop1: SIMPLE_STOP A creation instruction create stop1 produces, at run time, an object of type SIMPLE_STOP.

14 Chair of Software Engineering Creation procedures 14 Instruction create x will initialize all the fields of the new object attached to x with default values What if we want some specific initialization? E.g., to make object consistent with its class invariant? class STOP … station: STATION invariant station /= Void station Use creation procedure: create stop1.set_station (station1) Incorrect!

15 Chair of Software Engineering 15 STOP May be used as a regular command and also as a creation procedure List zero or more creation procedures class STOP create set_station feature station: STATION -- Station which this stop represents next: SIMPLE_STOP -- Next stop on the same line set_station (s: STATION) -- Associate this stop with s. require station_exists: s /= Void ensure station_set: station = s link (s: SIMPLE_STOP) -- Make s the next stop on the line. ensure next_set: next = s invariant station_exists: station /= Void end Is established by set_station

16 Chair of Software Engineering 16 Object creation: summary To create an object:  If class has no create clause, use basic form, create x.  If the class has a create clause listing one or more procedures, use create x.make (…) where make is one of the creation procedures, and “(…)” stands for arguments if any.

17 Chair of Software Engineering 17 Executing a system Root creation procedure may:  Create new objects  Call features on them, which may create other objects  and so on... Executing a system consists of creating a root object, which is an instance of a designated class from the system, called its root class, using a designated creation procedure of that class, called its root creation procedure.

18 Chair of Software Engineering Some acrobatics class DIRECTOR create prepare_and_play feature acrobat1, acrobat2, acrobat3: ACROBAT friend1, friend2: ACROBAT_WITH_BUDDY author1: AUTHOR curmudgeon1: CURMUDGEON prepare_and_play is do author1.clap (4) friend1.twirl (2) curmudgeon1.clap (7) acrobat2.clap (curmudgeon1.count) acrobat3.twirl (friend2.count) friend1.buddy.clap (friend1.count) friend2.clap (2) end 18 Hands-On What’s wrong with the feature prepare_and_play? What entities are used in this class?

19 Chair of Software Engineering Some acrobatics 19 class DIRECTOR create prepare_and_play feature acrobat1, acrobat2, acrobat3: ACROBAT friend1, friend2: ACROBAT_WITH_BUDDY author1: AUTHOR curmudgeon1: CURMUDGEON prepare_and_play is do 1create acrobat1 2create acrobat2 3create acrobat3 4create friend1.make_with_buddy (acrobat1) 5create friend2.make_with_buddy (friend1) 6create author1 7create curmudgeon1 end Which entities are still void after execution of line 4? Which of the classes mentioned here have creation procedures? Why is this procedure necessary? Hands-On


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

Similar presentations


Ads by Google