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

Slides:



Advertisements
Similar presentations
1
Advertisements

Final and Abstract Classes
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Programming Language Concepts
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
Lecture 15 Linked Lists part 2
Solve Multi-step Equations
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Turing Machines.
1 Refactoring with Contracts Shmuel Tyszberowicz School of Computer Science The Academic College of Tel Aviv Yaffo Maayan Goldstein School of Computer.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Modern Programming Languages, 2nd ed.
EU market situation for eggs and poultry Management Committee 20 October 2011.
Semantic Analysis and Symbol Tables
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
XML and Databases Exercise Session 3 (courtesy of Ghislain Fourny/ETH)
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
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 5.
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 5.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
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 4.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

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

2 Today Reference types vs. expanded types Assignment Basic types Local variables Qualified and unqualified calls Entities and variables: summary

3 Two kinds of types Reference types: value of any entity is a reference. Example: s : STATION Expanded types: value of an entity is an object. Example: p : POINT s (STATION ) p reference (SOME_CLASS ) (POINT )

4 Who can reference what? Objects of expanded types can contain references to other objects… … but they cannot be referenced by other objects! c (SOME_CLASS ) (COUPLE ) (HUMAN) (OTHER_CLASS ) 10

5 Reference equality a = b ? (VECTOR ) a b (VECTOR ) (VECTOR ) b a True False

6 Expanded entities equality Entities of expanded types are compared by value! a = b ? True a (SOME_CLASS ) (POINT ) b (POINT )

7 Expanded entities equality (SOME_CLASS ) (HUMAN) 32 John (HUMAN) b a 30 Jane (HUMAN) 32 John (HUMAN) 30 Jane a = b ? False Hands-On (COUPLE ) 10 (COUPLE ) 10

8 Expanded entities equality Hands-On (HUMAN) 32 John (HUMAN) 30 Jane a = b ? True (SOME_CLASS ) b a (COUPLE ) 10 (COUPLE ) 10

9 Assignment Assignment is an instruction (What other instructions do you know?) Syntax: a := b where a is a variable (e.g., attribute) and b is an expression (e.g. argument, query call); a is called the target of the assignment and b – the source. Semantics: after the assignment a equals b (a = b); the value of b is not changed by the assignment.

10 Reference assignment (VECTOR ) a 0.0 (VECTOR ) b a := b a references the same object as b: a = b

11 Expanded assignment a (POINT ) b (POINT ) a := b The value of b is copied to a, but again: a = b

12 Assignment Explain graphically the effect of an assignment: Hands-On (HUMAN) 32 John (HUMAN) a 30 Jane (HUMAN) 25 Dan (HUMAN) 24 Lisa (COUPLE ) 10 a := b b (COUPLE ) 4 4 Here COUPLE is an expanded class, HUMAN is a reference class

13 Attachment More general term than assignment Includes: Assignment a := b Passing arguments to a routine f (a: SOME_TYPE) is do … end f (b) Same semantics

14 Dynamic aliasing a, b: VECTOR … create b.make (1.0, 0.0) a := b now a and b reference the same object (are two names or aliases of the same object) any change to the object attached to a will be reflected, when accessing it using b any change to the object attached to b will be reflected, when accessing it using a (VECTOR ) a b x y

15 Dynamic aliasing What are the values of a.x, a.y, b.x and b.y after executing instructions 1-4? a, b: VECTOR … create a.make (-1.0, 2.0) 1create b.make (1.0, 0.0) 2a := b 3b.set_x (5.0) 4a.set_y (-10.0) Hands-On (VECTOR ) a b x y

16 Where do expanded types come from? To get an expanded type, declare a class with keyword expanded: expanded class COUPLE feature -- Access man, woman: HUMAN years_together: INTEGER end Now all the entities of type COUPLE will automatically become expanded: pitt_and_jolie: COUPLE Expanded Reference ?

17 Basic types So called basic types (BOOLEAN, INTEGER, NATURAL, REAL, CHARACTER, STRING) in Eiffel are classes – just like all other types Most of them are expanded… a := b … and immutable (they do not contain commands to change the state of their instances)… a := a.plus (b)instead ofa.add (b) 5 b 3 a 5 a 5 b a + b Another name for the same feature

18 Basic types … their only privilege is to use manifest constants to construct their instances: b: BOOLEAN x: INTEGER c: CHARACTER s: STRING … b := True x := 5 -- instead of create x.make_five c := c s := I love Eiffel

19 Strings are a bit different Strings in Eiffel are not expanded… s: STRING … and not immutable s := I love Eiffel s.append ( very much!) Ilov sarea count e

20 Initialization Default value of any reference type is Void Default values of basic expanded types are: False for BOOLEAN 0 for numeric types (INTEGER, NATURAL, REAL) null character (its code = 0) for CHARACTER Default value of a non-basic expanded type is an object, whose fields have default values of their types (COUPLE ) 0

21 Initialization What is the default value for the following classes? expanded class POINT feature x, y: REAL end class VECTOR feature x, y: REAL end STRING Hands-On 0.0 (POINT) x y Void

22 Expanded classes must be creatable in the default way expanded class POINT create make feature make do x := 5.0; y := 5.0 end... end But you can use a trick expanded class POINT inherit ANY redefine default_create feature default_create do x := 5.0; y := 5.0 end Custom initialization for expanded types Incorrect

23 Some variables are only used by a certain routine (examples from your last assignment?) Declare them as local: feature f (arg1: A; …) require... local x, y: B z: C do … end ensure... end Local variables

24 The scope of names Attributes: are declared anywhere inside a feature clause, but outside other features are visible anywhere inside the class Formal arguments: are declared after the feature name are only visible inside the feature body and its contracts Local variables: are declared in a local clause inside the feature declaration are only visible inside the feature body

25 Compilation error? (1) class PERSON feature name: STRING set_name (a_name: STRING) do name := a_name end exchange_names (other: PERSON) local s: STRING do s := other.name other.set_name (name) set_name (s) end print_with_semicolon do create s.make_from_string (name) s.append(;) print (s) end Hands-On Error: this variable was not declared

26 Compilation error? (2) Hands-On class PERSON feature …-- name and set_name as before exchange_names (other: PERSON) local s: STRING do s := other.name other.set_name (name) set_name (s) end print_with_semicolon local s: STRING do create s.make_from_string (name) s.append(;) print (s) end OK: two different local variables in two routines

27 Compilation error? (3) class PERSON feature …-- name and set_name as before s: STRING exchange_names (other: PERSON) do s := other.name other.set_name (name) set_name (s) end s: STRING print_with_semicolon do create s.make_from_string (name) s.append(;) print (s) end Hands-On Error: an attribute with the same name was already defined

28 Compilation error? (4) class PERSON feature …-- name and set_name as before exchange_names (other: PERSON) do s := other.name other.set_name (name) set_name (s) end print_with_semicolon do create s.make_from_string (name) s.append(;) print (s) end s: STRING end Hands-On OK: a single attribute used in both routine

29 Local variables vs. attributes Which one of the two correct versions (2 and 4) do you like more? Why? Describe the conditions under which it is better to use a local variable instead of an attribute and vice versa Hands-On

30 Result Inside every function you can use the predefined local variable Result (you neednt and shouldnt declare it) The return value of a function is whatever value the Result variable has at the end of the function execution At the beginning of routines body Result (as well as regular local variables) is initialized with the default value of its type Every regular local variable is declared with some type; and what is the type of Result? Its the function return type!

31 Compilation error? (5) class PERSON feature …-- name and set_name as before exchange_names (other: PERSON) do Result := other.name other.set_name (name) set_name (Result) end name_with_semicolon: STRING do create Result.make_from_string (name) Result.append(;) print (Result) end Hands-On Error: Result can not be used in a procedure

32 In object-oriented computation each routine call is performed on a certain object Inside the routine we can access this object using the predefined entity Current Current (STATION ) x.connect (y) connect (other: STATION) do … road.connect (Current, other) end What is the type of Current?

33 If the target of a feature call is Current, it is common to omit it: Current.f (a) f (a) Qualified vs. unqualified Such a call is unqualified Otherwise, if the target of a call is specified explicitly, the call is qualified x.f (a)

34 Qualified or unqualified? Are the following feature calls qualified or unqualified? What are the targets of these calls? 1)x.y 2)x 3)f (x.a) 4)x.y.z 5)x (y.f (a.b)) 6)f (x.a).y (b) 7)Current.x Hands-On qualified unqualified qualified unqualified qualified

35 Assignment to attributes Direct assignment to an attribute is only allowed if an attribute is called in an unqualified way: y := 5 x.y := 5 Current.y := 5 There are two main reasons for this rule: 1.A client may not be aware of the restrictions put on the attribute value and interdependencies with other attributes => class invariant violation (Example?) 2.Guess! (Hint: uniform access principle) OK Error ?

36 Entity: the final definition variable attribute constant attribute Only a variable can be used in a creation instruction and in the left part of an assignment An entity in program text is a name that directly denotes an object. More precisely: it is one of attribute name formal argument name local variable name Result Current Variables Read-only entities

37 Find 5 errors class VECTOR feature x, y: REAL copy_from (other: VECTOR) do Current := other end copy_to (other: VECTOR) do create other other.x := x other.y := y end reset do create Current end Hands-On Current is not a variable and can not be assigned to other is a formal argument (not a variable) and thus can not be used in creation other.x is a qualified attribute call (not a variable) and thus can not be assigned to the same reason for other.y Current is not a variable and thus can not be used in creation

38 Clients and suppliers If class C mentions class A in its text (e.g., declares an entity of type A), then C is a client of A A is a supplier of C A class can be a supplier (and a client) of itself (youll see an next week) AC C

39 Clients and suppliers Hands-On How many suppliers does class COUPLE have? What are they? class COUPLE feature man, woman: HUMAN years_together: INTEGER print_wedding_invitation local s: STRING p: INVITATION_PRINTER do s := We are happy to invite you to the wedding of + man.name + and + woman.name + ! create p p.print (s, 50) end