Introduction to Eiffel

Similar documents
Einführung in die Programmierung Introduction to Programming

Introduction to Programming Using Java (98-388)

Universe Type System for Eiffel. Annetta Schaad

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

Syntax of Eiffel: a Brief Overview

ETHZ D-INFK Final Exam Booklet

Mock exam 1. ETH Zurich. Date: 9./10. November 2009

Assignment 7: Inheritance

Assignment 4: Object creation

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Multiple & Repeated Inheritance

Multiple & Repeated! Inheritance!

The Eiffel language. Slides partly based on :

Example Test Questions for Inheritance

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

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

Assignment 2 - Specifications and Modeling

Assignment 4: Object creation

Assertions. Assertions - Example

Test-Driven Development (TDD)

Assertions, pre/postconditions

Singleton Pattern: Motivation. Design Patterns: Singleton and Iterator. What are design patterns? Shared Data through Inheritance

Singleton Pattern Creational

Singleton Pattern Creational. » Ensure a class has only one instance» Provide a global point of access

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Java Object Oriented Design. CSC207 Fall 2014

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.

Einführung in die Programmierung Introduction to Programming

A web-based IDE for Java

Einführung in die Programmierung Introduction to Programming

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

Advanced Systems Programming

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Control flow in Eiffel

A Eiffel: The Essentials

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Introduction to Inheritance

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

CLASS DESIGN. Objectives MODULE 4

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Chapter 4 Defining Classes I

In-Class Exercises. ETH Zurich. November

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Generic Collection Class: Motivation (2) Use of Generic Parameters Iterator and Singleton Patterns

STUDENT LESSON A5 Designing and Using Classes

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Adding Contracts to C#

Java Identifiers, Data Types & Variables

Java Inheritance. Classes implement the concept of ADT:

4. Java language basics (4) Func%on July 19, 2015

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Introduction to C++ with content from

Java: introduction to object-oriented features

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

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

2. The object-oriented paradigm!

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

In-Class Exercises. ETH Zurich. ETH students recently designed a special kind of oven for cooking potatoes. Here are some facts about such an oven:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Use of Generic Parameters Iterator and Singleton Patterns

Final Exam Practice. Partial credit will be awarded.

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

C# Programming for Developers Course Labs Contents

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Design Patterns: State, Bridge, Visitor

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Short Notes of CS201

Programming Exercise 14: Inheritance and Polymorphism

Software Design and Analysis for Engineers

CS201 - Introduction to Programming Glossary By

Data Structures (list, dictionary, tuples, sets, strings)

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

JAVA BASICS II. Example: FIFO

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Programming II (CS300)

BON Business Object Notation Based on slides by Prof. Paige

EXAM Computer Science 1 Part 1

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 251 Intermediate Programming Methods and Classes

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

CS 251 Intermediate Programming Methods and More

CSCI 355 Lab #2 Spring 2007

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

CSCI 355 LAB #2 Spring 2004

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

C++ Important Questions with Answers

Inheritance. OOP: Inheritance 1

VARIABLES AND TYPES CITS1001

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

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

Last class. -More on polymorphism -super -Introduction to interfaces

Midterm Exam CS 251, Intermediate Programming March 12, 2014

Review. these are the instance variables. these are parameters to the methods

Transcription:

Introduction to Eiffel Martin Nordio, Christian Estler ETH Zurich Distributed Software Engineering Lab 1

Overview Part 1: Language Constructs Ø Basics: definition, if then else, expressions, loops and across, creation procedures Ø Inheritance: redefinition and multiple inheritance Ø Exception Handling Ø Once Routines Ø Style rules Ø Generics Ø Information Hiding Part 2: Contracts Ø Preconditions, postconditions and invariants Ø Contracts in inheritance Part 3: Tuples and Agents 2

Preparation Go to: http://codeboard.io If you don t have an account yet, please sign-up and sign-in before doing the exercises. Once you re done with a programming exercise, submit your solution. 3

Part 1: Language constructs 1.1 BASICS 4

Class declaration: Eiffel vs Java: ACCOUNT public Account { 5

Constructors ACCOUNT create make, make_balance feature make do make_balance (i: INTEGER) do public Account { public Account() {... public Account (int b) {... 6

Constructors ACCOUNT create make, make_balance, make_name feature make do make_balance (i: INTEGER) do make_name (s: STRING) do public Account { public Account() {... public Account (int b) {... public Account (string s) {... Constructors can have any name; use the create clause to declare a routine as constructor 7

Overloading PRINTER feature print_int (a_int: INTEGER) do print_real (a_real: REAL) do print_string (a_str: STRING) do public Printer { public void print(int i) {... public void print(float f) {... public void print(string s) {... Eiffel does not support overloading! 8

Creating Objects BANK feature pay_bill local b1: ACCOUNT do create b1.make public Bank { public void paybill() { Account b1 = new Account(); 9

Creating Objects BANK feature pay_bill local b1, b2: ACCOUNT do create b1.make create b2.make_balance (2) public Bank { public void paybill() { Account b1 = new Account(); Account b2 = new Account (2); Create objects using the create keyword; declare the local variables in the local clause 10

Let s code Go to: h'ps://codeboard.io/projects/16 Task: create a local ACCOUNT object in the constructor of the APPLICATION Task: modify the creaeon procedure of ACCOUNT to print a confirmaeon that an account was created Task: write a new creaeon procedure in ACCOUNT that lets you create an account with an inieal balance; use it from APPLICATION 11

Creating Objects: default create MAIN BANK feature root local b1: BANK do create b1 - corresponds to - create b1.default_create b1.pay_bill feature pay_bill do All es inherit from ANY (Object in Java). If no creation procedure is specified, default_create is used (inherited from ANY) 12

Creating Objects: default create BANK inherit ANY redefine default_create create default_create feature The routine default_create can be redefined 13

Let s code Go to: h'ps://codeboard.io/projects/16 Task: override the default_create in CUSTOMER to print a confirmaeon message Task: create a customer object in the APPLICATION Task: write a creaeon procedure for CUSTOMER that takes, name, first_name and age as arguments; use it to create a customer 14

Features ACCOUNT feature -- Initialization make do make_balance (i: INTEGER) do make_name (s: STRING) do feature - Basic operations deposit (i: INTEGER) do withdraw (i: INTEGER) do transfer (b: ACCOUNT) do feature - Access balance: INTEGER do public Account { public Account() {... public Account (int b) {... public Account (string s) {... public void deposit (int i) {... public void withdraw (int i) {... public void transfer(account b). public int balance() {... The feature clause is used to group routines and for information hiding (see 1.8) 15

Expressions and Conditionals feature foo do if b and (c or d) then x := 5 public foo() { if (b & (c d)) { x = 5;... foo do if b and then (c or else d) then public foo() { if (b && (c d)) {... 16

Let s code Go to: h'ps://codeboard.io/projects/16 Task: write a condieon that only allows to withdraw money if the balance if sufficient; otherwise print an error message; make two withdraws that show the regular and the excepeonal behavior 17

Return and breaks B feature foo: INTEGER do Result := 5 public B { public int foo() { return 5; Eiffel does not support neither breaks, continues nor return 18

Loops print local i: INTEGER do from i := 1 until i >= 10 loop i := i + 1 public Printer { public void print() { for(int i=0;i<10;i++) {... 19

Loops: Example 2 print local i: INTEGER do from i := 1 until i >= 10 loop i := i + 1 public Printer { public void print() { int i=0; while(i<10) { i++; 20

Let s code Go to: h'ps://codeboard.io/projects/24 Task: implement the print_log funceonality for in the ACCOUNT; complete ACCOUNT to log deposits and withdraws 21

Loops: Traversing a list print_using_from do from list.start until list.after loop list.item.print list.forth public Printer { public void print() { for(element e: list) { e.print(); print_using_across do across list as e loop e.item.print 22

Basic Types Eiffel: BOOLEAN CHARACTER INTEGER INTEGER_64 REAL DOUBLE STRING Java: boolean char, byte short, int long float double String 23

Part 1: Language constructs 1.2 INHERITANCE 24

Deferred Class (abstract ) deferred ACCOUNT feature deposit (a_num: INT) deferred abstract Account { abstract void deposit(int a); A must be deferred if it has at least one deferred routine. A can be deferred without any deferred routines. 25

Simple Inheritance ACCOUNT inherit ANY public Account exts Object { 26

Let s code Go to: h'ps://codeboard.io/projects/25 Task: create a deferred PERSON; move the properees name and age from CUSTOMER into the deferred PERSON; make sure the program behavior did not change 27

Feature redefinition ACCOUNT inherit ANY redefine out feature out: STRING do Result := abc public Account exts Object { String tostring() { return abc ; All routines that are redefined must be listed in the inherit clause. 28

Precursor call ACCOUNT inherit ANY redefine out feature out: STRING do Result := Precursor {ANY public Account exts Object { String tostring() { return super(); 29

Multiple Inheritance A feature foo do B feature foo do Option 1: C inherit A B rename foo as foo_b Class C will have two features foo and foo_b Option 2: C inherit A B undefine foo foo from B becomes deferred; implemented in C by foo from A 30

Let s code Go to: h'ps://codeboard.io/projects/26 Task: redefine the print_self rouene in B to print the correct message Task: redefine the print_self rouene in C to print the correct message; what happens when you try to compile? Task: resolve the conflict that was created due to muleple inheritance (hint: there are the 2 opeons to do that? 31

Structure of inherit clause inherit A rename undefine redefine B rename undefine redefine A redefine clause must structured in the order rename, undefine, redefine. 32

Frozen / frozen routine frozen ACCOUNT inherit ANY final Account exts Object { ACCOUNT feature frozen deposit (a_num: INT) do Account { final void deposit(int a) { A frozen cannot be inherited; a frozen routine cannot be redefined. 33

Expanded expanded MY_INT int, float, double, char 34