Einführung in die Programmierung Introduction to Programming Prof. Dr

Slides:



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

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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Introduction to C Programming
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
1 Introductions to: - Each other - Object-Oriented Programming (OOP) - Java CSSE 220—Object-Oriented Software Development Rose-Hulman Institute of Technology.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Fundamentals of Software Development 1Slide 1 Acrobat Games – Debriefing: Interfaces For each type of object, what operations can that object do?For each.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
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 4.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
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.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Values, variables and types © Allan C. Milne v
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Fundamentals of Software Development 1Slide 1 Acrobat Games: An introduction to object-oriented design We need 11 volunteers – –BasicAcrobat x 3 – –ProudAcrobat.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
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.
Chapter 10 Programming Fundamentals with JavaScript
User-Written Functions
Names and Attributes Names are a key programming language feature
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
COMP 170 – Introduction to Object Oriented Programming
Chapter 2 - Introduction to C Programming
Agenda Warmup AP Exam Review: Litvin A2
Chapter 2 - Introduction to C Programming
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CS 139 – Programming Fundamentals
Chapter 2 - Introduction to C Programming
Week 3 Object-based Programming: Classes and Objects
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 2 - Introduction to C Programming
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
The structure of programming
Chapter 2 - Introduction to C Programming
Course Overview PART I: overview material PART II: inside a compiler
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
C++ Object Oriented 1.
Presentation transcript:

Einführung in die Programmierung Introduction to Programming Prof. Dr Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3

Today We will revisit classes, features and objects. We will see how program execution starts. We will play a role game.

Static view A program consists of a set of classes. Features are declared in classes. They define operations on objects created from classes. Queries answer questions. The answer is provided in a variable called Result. Commands execute actions. They do not return any result, so there is no variable called Result that we can use. Another name for a class is type. Class and Type are not exactly the same, but they are close enough for now, and we will learn the difference later on.

Declaring the type of an object The type of any object you use in your program must be declared somewhere. Where can such declarations appear in a program? in feature declarations formal argument types return type for queries functions attributes in the local clauses of routines - Note that the type of an attribute is understood as the type of query. Here is where you declare objects that only the routine needs and knows about. 4

Declaring the type of an object class DEMO feature procedure_name (a1: T1; a2, a3: T2) -- Comment local l1: T3 do … end function_name (a1: T1; a2, a3: T2): T3 attribute_name: T3 formal argument type local variable type return type return type

Hands-On Exercise: Find the classes / objects class game feature map_name: string -- Name of the map to be loaded for the game last_player: player -- Last player that moved players: player_list -- List of players in this game. ... The names are all lower-case on purpose 6

Dynamic view At runtime (ie., during the program execution), we have a set of objects (instances) created from the classes (types). The creation of an object implies that a piece of memory is allocated in the computer to represent the object itself. Objects interact with each other by calling features on each other.

Who are Adam and Eve? Who creates the first object? The runtime creates a so-called root object. The root object creates other objects, which in turn create other objects, etc. You define the type of the root object in the project settings. How is the root object created? The runtime calls a creation procedure of the root object. You define this creation procedure in the project settings. The application exits at the end of this creation procedure. 10

Changing the root class - You can show the project settings. 11

Static view vs. dynamic view Queries (attributes and functions) have a result type. When executing the query, you get an object of that type. Routines have formal arguments of certain types. During the execution you pass objects of the same (or compatible) type as actual arguments to a routine call. Local variables are declared in their own section, associating names with types. During the execution, local variables may hold different values of their respective types at different points in time. If you want you can explain briefly what “compatible” means (in terms of inheritance) 12

Hands-On Acrobat game We will play a little game now. Some of you will act as objects. When you get created, please stand up and stay standing during the game There will be different roles Acrobat Acrobat with Buddy Author Curmudgeon Director The companion slides with the role descriptions should be printed 4-up and separated into individual cards. It may be helpful if the students wear name tags if you do this before you learn their names. In the original, this is the first code they have seen, but in our case this is different. 13

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. Emphasize that there are different types of acrobats and all of them have a similar set of features. 14

You are an ACROBAT class ACROBAT feature clap (n: INTEGER) do -- Clap `n’ times and adjust `count’. end twirl (n: INTEGER) -- Twirl `n’ times and adjust `count’. count: INTEGER 15

You are an acrobat with a buddy 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.

You are an ACROBAT_WITH_BUDDY class ACROBAT_WITH_BUDDY inherit ACROBAT redefine twirl, clap, count end create make feature make (p: ACROBAT) do -- Remember `p’ being -- the buddy, i.e. store -- value of `p’ in `buddy’ clap (n: INTEGER) do -- Clap `n’ times and -- forward to buddy. end twirl (n: INTEGER) -- Twirl `n’ times and count: INTEGER -- Ask buddy and return -- his answer. buddy: ACROBAT 17

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.

You are an AUTHOR class AUTHOR inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) do -- Clap `n’ times say thanks and bow. end twirl (n: INTEGER) -- Twirl `n’ times say thanks and bow.

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.

You are a CURMUDGEON class CURMUDGEON inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) do -- Say “I refuse”. end twirl (n: INTEGER)

I am the root object I got created by the runtime by executing my creation feature.

I am a DIRECTOR I got created by the runtime by executing my creation feature. class DIRECTOR create prepare_and_play feature do -- See following slides. end

Let’s play 24

I am the root object prepare_and_play local acrobat1, acrobat2, acrobat3 : ACROBAT partner1, partner2: ACROBAT_WITH_BUDDY author1: AUTHOR curmudgeon1: CURMUDGEON do create acrobat1 create acrobat2 create acrobat3 create partner1.make (acrobat1)‏ create partner2.make (partner1)‏ create author1 create curmudgeon1 author1.clap (4)‏ partner1.twirl (2)‏ curmudgeon1.clap (7)‏ acrobat2.clap (curmudgeon1.count)‏ acrobat3.twirl (partner2.count)‏ partner1.buddy.clap (partner1.count)‏ partner2.clap (2) end Declare the local variables, explain that you need names for the objects to call features on them. - Create the objects and explain that you need to create them. The role cards are given to students during the creation. Students need to stand up when their objects are created. Go through the commands. At the end, don’t tell the students to sit down and let themselves to find out what’s wrong. Introduce the concept of memory management and (automatic) garbage collection. 25

Concepts seen Eiffel Game Classes with features Telling person to behave according to a specification Inheritance All people were some kind of ACROBAT Interface Queries and commands that are applicable Objects People Creation People stand up Entities Names for the people Polymorphism A name can refer to different kind of ACROBATs Dynamic binding Telling people by name to do the same has different outcome After the exercise, the instructor should point out what has been seen. The list includes objects classes encapsulation state interfaces inheritance polymorphism commands feature calls arguments return values chains of feature calls (possible) infinite recursion errors (see Curmudgeon) ???? creation entities references Moreover, it emphasizes a fruitful metaphor for learning objects: Objects are something like people. Ask students to identify at least one instance of each concept. 26

Concepts seen Eiffel Game Command call Telling people to do something Query call Asking a question to a person Arguments E.g. how many times to clap Return value E.g. count in ACROBAT_WITH_BUDDY Chains of feature calls E.g. partner1.buddy.clap (2) 27