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

Slides:



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

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.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Object Oriented Programming: Classes and Objects.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Road Map Introduction to object oriented programming. Classes
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 4: Objects.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 2 23 September 2008.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 3 29 September 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
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 Lecture 9: Abstraction.
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 10.
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 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 6.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Recitation 1 Programming for Engineers in Python.
Ranga Rodrigo. Class is central to object oriented programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
SE-1010 Dr. Mark L. Hornick 1 Introduction to Object-Oriented Programming (OOP) Part 1.
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 1.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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 9.
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 6.
Salman Marvasti Sharif University of Technology Winter 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Implementing Classes
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Week 2 - Wednesday CS 121.
Variables and Primative Types
Other Kinds of Arrays Chapter 11
Week 6 Object-Oriented Programming (2): Polymorphism
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Unit 3: Variables in Java
Handout-2(a) Basic Object Oriented Concepts
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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

2 News (Reminder) Mock exam next week!  Monday exercise groups: November 8  Tuesday exercise groups: November 9  You have to be present  The week after we will discuss the results  Assignment 7 due on November 16

3 Today  Abstractions  Uniform Access Principle  Naming conventions  Exporting features

4 Abstraction To abstract is to capture the essence behind the details and the specifics. The client is interested in:  a set of services that a software module provides, not its internal representation  what a service does, not how it does it hence, the class abstraction hence, the feature abstraction  Object-oriented programming is all about finding right abstractions  However, the abstractions we choose can sometimes fail, and we need to find new, more suitable ones.

5 Abstraction ”A simplification of something much more complicated that is going on under the covers. As it turns out, a lot of computer programming consists of building abstractions. What is a string library? It's a way to pretend that computers can manipulate strings just as easily as they can manipulate numbers. What is a file system? It's a way to pretend that a hard drive isn't really a bunch of spinning magnetic platters that can store bits at certain locations, but rather a hierarchical system of folders-within-folders containing individual files that in turn consist of one or more strings of bytes.“ (extract from )

6 Discussing abstractions What abstractions were used in the temperature converter from assignment 4?  Why it is better to have a class for TEMPERATURE than to store the value in an INTEGER variable? Hands-On  How was the Celsius value obtained? What about the Kelvin value? Did you see that difference in the class TEMPERATURE_APPLICATION ?

7 Finding the right abstractions (classes) Suppose you want to model your room: class ROOM feature -- to be determined end Your room probably has thousands of properties and hundreds of things in it:

8 Finding the right abstractions (classes) size location material messy? door shape computer bed desk furniture etc Therefore, we need a first abstraction: What do we want to model? In this case, we focus on the size, the door, the computer and the bed.

9 Finding the right abstractions (classes) To model the size, an attribute of type DOUBLE is probably enough, since all we are interested in is it‘s value: class ROOM feature size: DOUBLE -- Size of the room. end

10 Finding the right abstractions (classes) Now we want to model the door. If we are only interested in the state of the door, i.e. if it is open or closed, a simple attribute of type BOOLEAN will do: class ROOM feature size: DOUBLE -- Size of the room. is_door_open: BOOLEAN -- Is the door open or closed?... end

11 Finding the right abstractions (classes) But what if we are also interested in what our door looks like, or if opening the door triggers some behavior?  Is there a daring poster on the door?  Does the door squeak while being opened or closed?  Is it locked?  When the door is being opened, a message will be sent to my cell phone In this case, it is better to model a door as a separate class!

12 Finding the right abstractions (classes) class ROOM feature size: DOUBLE -- Size of the room in square meters. door: DOOR -- The room’s door. end

13 Finding the right abstractions (classes) class DOOR feature is_locked: BOOLEAN -- Is the door locked? is_open: BOOLEAN -- Is the door open? is_squeaking: BOOLEAN -- Is the door squeaking? has_daring_poster: BOOLEAN -- Is there a daring poster on the door? open -- Opens the door do -- Implementation of open, including sending a message end -- more features… end

14 Finding the right abstractions (classes) How would you model… … the computer? … the bed? Hands-On How would you model an elevator in a building?

15 Finding the right abstractions (features) (BANK_ACCOUNT) deposits withdrawals 800 (BANK_ACCOUNT) deposits withdrawals balance invariant: balance = total (deposits) – total (withdrawals) Which one would you choose and why?

16 Uniform access principle The client is interested in what a service does, not how it does it. It doesn’t matter for the client, whether you store or compute, he just wants to obtain the balance. Features should be accessible to clients the same way, no matter whether they are implemented by storage or computation my_account.balance

17 Features: the full story (again…) Command Query Feature Function No result Feature Memory Computation Client view (specification) ‏ Internal view (implementation) ‏ Returns result Attribute Procedure Memory Computation Routine No result Returns result

18 Two kinds of queries Attribute  from the client’s viewpoint it is a query  call is an expression  from the implementation’s viewpoint uses memory Function  from the client’s viewpoint is a query  call is an expression  from the implementation’s viewpoint uses computation

19 Naming conventions Names for classes: PASSENGER, STUDENT, NUMERIC, STORABLE Names for queries: balance, name, first_element, list_of_students  for boolean queries: full, after, is_empty, is_best_choice Names for commands: run, do_nothing, pimp_my_exersice_session Composed of English words using underscore Full English words, but short

20 How do you like these names? class SOLVE_QUADRATIC_EQUATION feature solve (a, b, c: REAL) do... end get_dscrm (a, b, c: REAL): REAL do... end how_many_solutions_there_are: INTEGER first_solution, second_solution: REAL end Hands-On

21 I like these better! class QUADRATIC_EQUATION_SOLVER feature solve (a, b, c: REAL) do... end discriminant (a, b, c: REAL): REAL do... end solution_count: INTEGER first_solution, second_solution: REAL end

22 Exporting features a1.f, a1.g: valid in any client a1.h: invalid everywhere (including in A’s text!) a1.j: valid in B, C and their descendants (invalid in A!) ‏ a1.m: valid in B, C and their descendants, as well as in A and its descendants. Status of calls in a client with a1 of type A: class A feature f... g... feature {NONE} h, i... feature {B, C} j, k, l... feature {A, B, C} m, n… end

23 Compilation error? class PERSON feature name: STRING feature {BANK} account: BANK_ACCOUNT feature {NONE} loved_one: PERSON think do print (“Thinking of ” + loved_one.name) end lend_100_franks do loved_one.account.transfer (account, 100) end Hands-On OK: unqualified call OK: exported to all OK: unqualified call Error: not exported to PERSON

24 The strange case of the stolen exam class PROFESSOR create make feature make (an_exam_draft: STRING) do exam_draft := an_exam_draft end feature exam_draft: STRING end

25 For your eyes only class ASSISTANT create make feature make (a_prof: PROFESSOR) do prof := a_prof end feature prof: PROFESSOR feature review_draft do -- review prof.exam_draft end

26 Exploiting a hole in information hiding class STUDENT create make feature make (a_prof: PROFESSOR; an_assi: ASSISTANT) do prof := a_prof assi := an_assi end feature prof: PROFESSOR assi: ASSISTANT feature stolen_exam: STRING do Result := prof.exam_draft end

27 Don’t try this at home! you: STUDENT your_prof: PROFESSOR your_assi: ASSISTANT stolen_exam: STRING create your_prof.make ( “top secret exam!”) create your_assi.make (your_prof) create you.make (your_prof, your_assistant) stolen_exam := you.stolen_exam

28 Fixing the issue class PROFESSOR create make feature make (a_exam_draft: STRING) do exam_draft := a_exam_draft end feature exam_draft: STRING end {PROFESSOR, ASSISTANT} Hands-On

29 The export status does matter! class STUDENT create make feature make (a_prof: PROFESSOR; a_assi: ASSISTANT) do prof := a_prof assi := a_assi end feature prof: PROFESSOR assi: ASSISTANT feature stolen_exam: STRING do Result := prof.exam_draft end Invalid call! Result := assi.prof.exam_draft Invalid call!

30 Information hiding vs. creation routines class PROFESSOR create make feature {None} make (an_exam_draft: STRING) do... end Can I create an object of type PROFESSOR as a client? After creation, can I invoke feature make as a client?

31 Controlling the export status of creation routines class PROFESSOR create {COLLEGE_MANAGER} make feature {None} make (an_exam_draft: STRING) do... end Can I create an object of type PROFESSOR as a client? After creation, can I invoke feature make as a client? What if I have create {NONE}make instead of create {COLLEGE_MANAGER} make ?

32 Exporting attributes Exporting an attribute only means giving read access x.f := 5 Attributes of other objects can be changed only through commands  protecting the invariant  no need for getter functions!

33 Example class TEMPERATURE feature celsius_value: INTEGER make_celsius (a_value: INTEGER) require above_absolute_zero: a_value >= - Celsius_zero do celsius_value := a_value ensure celsius_value_set := celsius_value = a_value end... end

34 Assigners If you like the syntax x.f := 5 you can declare an assigner for f  In class TEMPERATURE celsius_value: INTEGER assign make_celsius  In this case t.celsius_value := 36 is a shortcut for t.make_celsius (36) ... and it won’t break the invariant!