Einführung in die Programmierung Introduction to Programming

Similar documents
Einführung in die Programmierung Introduction to Programming

Introduction to Eiffel

Object-Oriented Software Construction

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming

Lecture 13: Introduction to inheritance and genericity

Universe Type System for Eiffel. Annetta Schaad

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Lecture 14: More Inheritance & Genericity

Using the Xcode Debugger

Problem Solving with C++

EiffelStudio the Eiffel IDE

Introduction to Inheritance

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Software and Programming 1

Einführung in die Programmierung Introduction to Programming

Java and C# in Depth

Classes, Objects, and OOP in Java. June 16, 2017

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

COMP 202 Java in one week

F I N A L E X A M I N A T I O N

Syntax of Eiffel: a Brief Overview

Object-Oriented Software Construction

CPS122 Lecture: Defining a Class

Commands, and Queries, and Features. Syntax of Eiffel: a Brief Overview. Escape Sequences. Naming Conventions

Software and Programming 1

CSCI 1060U Programming Workshop

Spell Checker for EiffelStudio

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

Assignment 4: Object creation

Object Oriented Programming with Java

Introduction to Programming System Design CSCI 455x (4 Units)

Building custom components IAT351

Introduce C# as Object Oriented programming language. Explain, tokens,

Anatomy of a Class Encapsulation Anatomy of a Method

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Mastering the Visual LISP Integrated Development Environment

Chapter 3 How to use HTML5 and CSS3 with ASP.NET applications

CS313D: ADVANCED PROGRAMMING LANGUAGE

Traffic 3.1 Examples for Eiffel Beginners

Lecture 21: Undo/Redo

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Software Design and Analysis for Engineers

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Key Differences Between Python and Java

Absolute C++ Walter Savitch

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

C#: advanced object-oriented features

Introduction to Programming Using Java (98-388)

Java Programming Tutorial 1

Java Programming. Manuel Oriol, March 22nd, 2007

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Use the scantron sheet to enter the answer to questions (pages 1-6)

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

Chapter 2 Elementary Programming

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

3 Getting Started with Objects

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Python Unit

Basics of Java Programming. Hendrik Speleers

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

A web-based IDE for Java

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Final Exam Practice. Partial credit will be awarded.

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CS 231 Data Structures and Algorithms, Fall 2016

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

C# MOCK TEST C# MOCK TEST I

CSCE 314 Programming Languages. Type System

Constructor in Java. What is a Constructor? Rules for create a Java Constructor

Problem Grade Total

Inheritance. Transitivity

Computer Programming : C++

Eiffel: Analysis, Design and Programming. ETH Zurich, September-December Exception handling

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CSE 113 A. Announcements - Lab

News in RSA-RTE 10.1 updated for sprint Mattias Mohlin, January 2018

The architecture of Eiffel software 3.1 OVERVIEW classes clusters systems

1.00/1.001 Tutorial 1

CE221 Programming in C++ Part 1 Introduction

Practical C++ Programming

Multiple & Repeated! Inheritance!

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Fundamentals of Programming Session 8

Multiple & Repeated Inheritance

Chapter 1. Introduction

C#: advanced object-oriented features

Transcription:

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

Organizational Assignments Testat One assignment per week Will be put online on Monday, due on Tuesday the next week You have to hand in n - 1 out of n assignments Must include the last one Show serious effort You have to hand in two mock exams Military service or illness -> contact assistant Group mailing list Is everybody subscribed? 2

Today Give you the intuition behind object-oriented (OO) programming Teach you about formatting your code Distinguishing between commands and queries feature declaration and feature call Understanding feature call chains Getting to know the basics of EiffelStudio 3

Classes and objects Classes are pieces of software code. Several classes make up a program. Objects are instances of classes. Classes define operations applicable to their instances. Example: A class STUDENT can define operations applicable to all its instances, such as subscribing to a course, registering for an exam, etc. This means that all class STUDENT s instances (such as the students Bob, Mike, Steve, etc.) will be able to subscribe themselves to a course, to register for an exam, etc. Only operations defined in a class can be applied to its instances. 4

Features A feature is an operation that programs may apply to certain classes of objects. A feature can be called on an object. This object is called the target of the feature call. Examples next_message.s computer.shut_down telephone.ring A feature call can have arguments. Examples ( recipient ) next_message.s_to ( 3 ) computer.shut_down_after ( Loud telephone.ring_several (10, 5

Features: Exercise Class BANK_ACCOUNT defines and implements the following operations: deposit (i: INTEGER) withdraw (i: INTEGER) close If b: BANK_ACCOUNT (b is an instance of class BANK_ACCOUNT) which of the following feature calls are possible: b.deposit (10) b.deposit b.close b.close ( Now ) b.open b.withdraw (100.50) b.withdraw (0) 6

Class text class PREVIEW inherit TOURISM feature explore -- Show city info. do Class names Feature declaration Comment Feature names Paris display Louvre spotlight Instructions Feature body 7

Style rule For indentation, use tabs, not spaces Use this property to highlight the structure of the program, particularly through indentation Tabs class PREVIEW inherit TOURISM feature explore -- Show city info -- and route. do Paris display Louvre spotlight Line8 highlight Route1 animate 8

More style rules Class name: all upper-case Period in feature call: no space before or after Names of predefined objects: start with uppercase letters New names (for objects you define) start with lowercase letters class PREVIEW inherit TOURISM feature explore -- Show city info -- and route. do Paris display Louvre spotlight Line8 highlight Route1 animate 9

Even more style rules For features, use full names, not abbreviations Always choose identifiers that clearly identify the inted role Use words from natural language (preferably English) for the names you define class PREVIEW inherit TOURISM feature explore -- Show city info -- and route. do Paris.display Louvre spotlight Line8 highlight Line8.remove_all_sections Route1 animate For multi-word identifiers, use underscores 10

Exercise: style rules Format this class: class bank_account feature deposit (sum: INTEGER) -- Add `sum' to the account. do balance := balance + sum balance: INTEGER 11

Exercise: solution class BANK_ACCOUNT feature deposit (sum: INTEGER) -- Add `sum' to the account. do balance := balance + sum balance: INTEGER 12

Commands and queries A feature can be a: Command: a feature that may modify an object Query: a feature that accesses an object 13

Queries Goal: obtain properties of objects Always return a value. Should not modify any objects. Examples What is the name of a person? What is the age of a person? What is the id of a student? Is a student registered for a particular course? Are there any places left in a certain course? other examples? 14

Commands Goal: produce a change on an object, or several Does not return a value. May modify objects. Examples Register a student to a course Assign an id to a student Record the grade a student got in an exam other examples? 15

Exercise: query or command? What is the balance of a bank account? Withdraw some money from a bank account Who is the owner of a bank account? Who are the clients of a bank whose deposits are over 100,000 CHF? Change the account type of a client How much money can a client withdraw at a time? Set a minimum limit for the balance of accounts Is Steve Jobs a client of Credit Suisse? 16

Command-query separation principle Asking a question shouldn t change the answer i.e. a query 17

Query or command? class DEMO feature command procedure_name (a1: T1; a2, a3: T2) -- Comment do query function_name (a1: T1; a2, a3: T2): T3 -- Comment do Result := query attribute_name: T3 -- Comment predefined local variable denoting the result no result body result body result no body 18

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

Feature declaration vs. feature call You declare a feature when you write it into a class. set_name (a_name: STRING) -- Set name to a_name. do name := a_name You call a feature when you apply it to an object. a_person.set_name ( Peter ) 20

General form of feature call instructions object1.query1.query2.command (object2.query3.query4, object3) target arguments Targets and arguments can contain feature calls themselves. Where are query1, query2, query3 and query4 defined? Where is command defined? 21

Unqualified vs. qualified feature calls It is possible to leave out the target in a feature call. Such a call is called unqualified. The implicit target will be the current object. A qualified feature call has a target. The current object in a feature is always the instance of the surrounding class. assign_same_name (a_name: STRING; a_other_person: PERSON) -- Set a_name to this person and a_other_person. do a_other_person.set_name(a_name) set_name(a_name) person1.assign_same_name( Hans, person2) In this call the current object will be person1. qualified call unqualified call: Current.set_name(a_name) 22

EiffelStudio EiffelStudio is a software tool (IDE) to develop Eiffel programs. Integrated Development Environment Help & Resources Online tour in the help of EiffelStudio http://www.eiffel.com/ http://docs.eiffel.com/ http://www.ecma- international.org/publications/files/ecma- ST/ECMA-367.pdf 23

Components editor context tool clusters pane features pane compiler project settings... 24

Editor Syntax highlighting Syntax completion Class name completion (SHIFT+CTRL+Space) Smart indenting Block indenting or unindenting (TAB and SHIFT+TAB) Block commenting or uncommenting (CTRL+K and SHIFT+CTRL+K) Infinite level of Undo/Redo (reset after a save) Quick search features (first CTRL+F to enter words then F3 and SHIFT+F3) 25

Compiler Uses incremental compilation freezing: Generates C code from the whole system and then compiles it to machine code. This code is used during development. Initially the system is frozen. melting: Generates bytecode for the changed parts of the system. This is much faster than freezing. This code is used during development. finalizing: Creates an executable production version. Finalization performs extensive time and space optimizations. 26

Debugger: setup The system must be melted/frozen (finalized systems cannot be debugged). Set / delete breakpoints An efficient way of adding breakpoints consists in dropping a feature in the context tool. Click in the margin to enable/disable single breakpoints. Use the toolbar debug buttons to enable or disable all breakpoints globally. 27

Debugger: run Run the program by clicking on the Run button. Pause by clicking on the Pause button or wait for a triggered breakpoint. Analyze the program: Use the call stack pane to browse through the call stack. Use the object tool to inspect the current object, the locals and arguments. Run the program or step over / into the next statement. Stop the running program by clicking on the Stop button. 28

Advanced Material The following slides contain advanced material and are optional. 29

Outline Syntax comparison: Eiffel vs Java Naming in Eiffel Feature comments: Less is better (sometimes...) 30

Eiffel vs Java: Class declaration class ACCOUNT class Account { } 31

Eiffel vs Java: Inheritance class ACCOUNT inherit ANY public class Account } exts Object { 32

Eiffel vs Java: Feature redefinition class ACCOUNT inherit ANY redefine out feature out: STRING do Result := abc public class Account exts Object { String tostring() { return abc ; } } 33

Eiffel vs Java: Precursor call class ACCOUNT inherit ANY redefine out feature out: STRING do Result := Precursor {ANY} public class Account exts Object { String tostring() { return super(); } } 34

Eiffel vs Java: Deferred deferred class ACCOUNT feature deposit (a: INT) deferred abstract class Account { abstract void deposit(int a); } 35

Eiffel vs Java: Frozen frozen class ACCOUNT inherit ANY final class Account } exts Object { 36

Eiffel vs Java: Expanded expanded class ACCOUNT int, float, double, char 37

Eiffel vs Java: Constructors class ACCOUNT create make feature make do public class Account { } public Account() {} 38

Eiffel vs Java: Constructor overloading class ACCOUNT create make, make_amount feature make do make_amount (a: INT) do public class Account { public Account() {} public Account(int a) {} } 39

Eiffel vs Java: Overloading class public class Printer { PRINTER public print(int i) {} feature print_int (a: INTEGER) public print(float f) {} do public print(string s) print_real (a: REAL) {} do print_string (s: STRING) do } 40

Eiffel vs Java: Exception Handling class public class Printer { PRINTER public print(int i) { feature print_int (a: INTEGER) try { do throw new Exception() (create EXCEPTION).raise } rescue catch(exception e) { retry } } } 41

Eiffel vs Java: Conditional class PRINTER feature print do if True then else public class Printer { public print() { if (true) { } else { } } } 42

Eiffel vs Java: Loop 1 print local i: INTEGER do from i := 1 until i >= 10 loop i := i + 1 public class Printer { public print() { for(int i=1;i<10;i++) { } } } 43

Eiffel vs Java: Loop 2 print local i: INTEGER do from i := 1 until i >= 10 loop i := i + 1 public class Printer { public print() { int i=1; while(i<10) { i++; } } } 44

Eiffel vs Java: Loop 3 print do from list.start until list.after loop list.item.print list.forth public class Printer { public print() { for(element e: list) { e.print(); } } } ECMA commitee is discussing about foreach for Eiffel 45

Eiffel Naming: Classes Full words, no abbreviations (with some exceptions) Classes have global namespace Name clashes arise Usually, classes are prefixed with a library prefix Traffic: TRAFFIC_ EiffelVision2: EV_ Base is not prefixed 46

Eiffel Naming: Features Full words, no abbreviations (with some exceptions) Features have namespace per class hierarchy Introducing features in parent classes, can clash with features from descants 47

Eiffel Naming: Locals / Arguments Locals and arguments share namespace with features Name clashes arise when a feature is introduced, which has the same name as a local (even in parent) To prevent name clashes: Locals are prefixed with l_ Some exceptions like i exist Arguments are prefixed with a_ 48

Feature comments: Version 1 tangent_ from (p: POINT): LINE -- Return the tangent line to the current circle -- going through the point p, if the point -- is outside of the current circle. require outside_circle: not has (p) Example is from http://dev.eiffel.com/style_guidelines 49

Feature comments: Version 2 tangent_ from (p: POINT): LINE -- The tangent line to the current circle -- going through the point p, if the point -- is outside of the current circle. require outside_circle: not has (p) 50

Feature comments: Version 3 tangent_ from (p: POINT): LINE -- Tangent line to current circle from point p -- if the point is outside of the current circle. require outside_circle: not has (p) 51

Feature comments: Version 4 tangent_ from (p: POINT): LINE -- Tangent line to current circle from point p. require outside_circle: not has (p) 52

Feature comments: Final version tangent_ from (p: POINT): LINE -- Tangent from p. require outside_circle: not has (p) 53

Feature comments: More information tangent_ from (p: POINT): LINE -- Tangent from p. -- -- `p : The point from -- `Result : The tangent line -- -- The tangent is calculated using the -- following algorithm: -- require outside_circle: not has (p) 54

Feature comments: Inherited comments tangent_ from (p: POINT): LINE -- <Precursor> require outside_circle: not has (p) 55

Ideas for future sessions Inheritance concepts: Single/Multiple/Non-conforming CAT Calls (Covariance and generics) Once/Multiple inheritance vs. Static Exception handling Design by contract in depth Void-safety Modeling concepts Best practices in Eiffel A look at ECMA specification of Eiffel 56