Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008.

Similar presentations


Presentation on theme: "Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008."— Presentation transcript:

1 Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008

2 Chair of Software Engineering 2 Learning goals  You’ll have an idea how program execution works  You understand what an object and a class are  You will see an analogy for objects and classes

3 Chair of Software Engineering 3 Object-orientied programming?  The general idea: At runtime, a program consists of objects that have properties and interact (similar to people).  An object has features, that can be called by other objects:  Queries: answering a question  Commands: executing actions  Features are defined in classes  An object has a type that is described in a class

4 Chair of Software Engineering 4 Who are Adam and Eve?  An object needs to be created by another object  In Eiffel:  An object is defined as the root object  This object creates other objects, which in turn create other objects, etc.

5 Chair of Software Engineering 5 Playing a game  Assume:  I am the root object  My behavior is defined in a class DIRECTOR class DIRECTOR create prepare_and_play feature -- Main execution prepare_and_play is do -- See later end

6 Chair of Software Engineering 6 You are an ACROBAT When you are asked to Clap, you will be given a number. Clap your hands that many times. When you are asked to Twirl, you will be given a number. Turn completely around that many times. When you are asked for Count, announce how many actions you have performed. This is the sum of the numbers you have been given to date.

7 Chair of Software Engineering 7 You are an ACROBAT_WITH_BUDDY When you are given this card, you will get someone else as your Buddy. When you are asked to Clap, you will be given a number. Clap your hands that many times. Pass the same instruction to your Buddy. When you are asked to Twirl, you will be given a number. Turn completely around that many times. Pass the same instruction to your Buddy. If you are asked for Count, ask your Buddy and answer with the number he tells you.

8 Chair of Software Engineering 8 You are an AUTHOR When you are asked to Clap, you will be given a number. Clap your hands that many times. Say “Thank You.” Then take a bow (as dramatically as you like). When you are asked to Twirl, you will be given a number. Turn completely around that many times. Say “Thank You.” Then take a bow (as dramatically as you like). When you are asked for Count, announce how many actions you have performed. This is the sum of the numbers you have been given to date.

9 Chair of Software Engineering 9 You are a CURMUDGEON When given any instruction (Twirl or Clap), ignore it, stand up and say (as dramatically as you can) “I REFUSE” If you are asked for Count, always answer with 0. Then sit down again if you were originally sitting.

10 Chair of Software Engineering 10 Let’s play the game  See blackboard

11 Chair of Software Engineering 11 ACROBAT class ACROBAT feature clap (n: INTEGER) is do -- Clap `n’ times and adjust `count’. end twirl (n: INTEGER) is do -- Twirl `n’ times and adjust `count’. end count: INTEGER end Hands-On What are the commands/queries?

12 Chair of Software Engineering 12 ACROBAT_WITH_BUDDY class ACROBAT_WITH_BUDDY inherit ACROBAT redefine twirl, clap, count end create make feature make (p: ACROBAT) is do -- Remember `p’ being the buddy. end clap (n: INTEGER) is do -- Clap `n’ times and forward to buddy. end … (continued on next page)‏

13 Chair of Software Engineering 13 ACROBAT_WITH_BUDDY (2) ‏ … (continued from last page)‏ twirl (n: INTEGER) is do -- Twirl `n’ times and forward to buddy. end count: INTEGER is do -- Ask buddy and return his answer end buddy: ACROBAT end What are the commands/queries? What features are defined? Hands-On

14 Chair of Software Engineering 14 AUTHOR class AUTHOR inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) is do -- Clap `n’ times and bow. end twirl (n: INTEGER) is do -- Twirl `n’ times and bow. end Hands-On What features are defined?

15 Chair of Software Engineering 15 CURMUDGEON class CURMUDGEON inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) is do -- Say “I refuse”. end twirl (n: INTEGER) is do -- Say “I refuse”. end

16 Chair of Software Engineering 16 Concepts seen Asking a question to a personExpression Telling person to behave according to a specification Class E.g. how many times to clapArguments Telling a person to do something Instruction Telling different people to do the same has different outcomes Polymorphism What queries What commands Interface PeopleObjects GameEiffel

17 Chair of Software Engineering 17 Concepts seen All people were some kind of ACROBAT Inheritance Persons need to be born and need to be named Creation E.g. partner1.buddy.clap (2)Chains of feature calls Names for the peopleEntities E.g. count in ACROBAT_WITH_BUDDY Return value GameEiffel

18 Chair of Software Engineering 18 Summary  To call a feature on another object, the caller must know the callee directly or indirectly. Direct: object.command Indirect: object1.query1.command  To be able to execute a command, the generating class must define it.  By looking at the class you can find out these things.


Download ppt "Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008."

Similar presentations


Ads by Google