Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 8 14. October 2008.

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.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
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 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.
Road Map Introduction to object oriented programming. Classes
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 Exercise Session 5.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Using Objects and Properties
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 6 7 October 2008.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
OOP Languages: Java vs C++
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Eiffel Naeem Esfahani University of Tehran "Man cannot discover new oceans unless he has the courage to lose sight of the shore." -- Andre Gide.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
ECE122 Feb. 22, Any question on Vehicle sample code?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
Objects and Classes Mostafa Abdallah
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Chair of Software Engineering Void safety these slides contain advanced material and are optional.
Topic: Classes and Objects
Road Map Introduction to object oriented programming. Classes
C Basics.
Programming Language Concepts (CIS 635)
Einführung in die Programmierung Introduction to Programming Prof. Dr
PHP.
Unit 6 - Variables - Fundamental Data Types
Einführung in die Programmierung Introduction to Programming Prof. Dr
In this class, we will cover:
Einführung in die Programmierung Introduction to Programming Prof. Dr
C# Revision Cards Data types
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008

Chair of Software Engineering Today’s learning goals  Reference types vs. expanded types  Equality  Assignment  Basic types 2

Chair of Software Engineering 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 )

Chair of Software Engineering 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

Chair of Software Engineering Reference equality a = b ? ( VECTOR ) a b ( VECTOR ) ( VECTOR ) b a True False

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

Chair of Software Engineering 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

Chair of Software Engineering Expanded entities equality Hands-On (HUMAN) 32 „John“ (HUMAN) 30 „Jane“ a = b ? True ( SOME_CLASS ) b a ( COUPLE ) 10 ( COUPLE ) 10

Chair of Software Engineering 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.

Chair of Software Engineering Reference assignment ( VECTOR ) a 0.0 ( VECTOR ) b a := b a references the same object as b: a = b

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

Chair of Software Engineering 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

Chair of Software Engineering 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

Chair of Software Engineering Dynamic aliasing a, b: VECTOR … create b (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

Chair of Software Engineering 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 (-1.0, 2.0) 1create b (1.0, 0.0) 2a := b 3b.set_x (5.0) 4a.set_y (-10.0) Hands-On

Chair of Software Engineering 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 ?

Chair of Software Engineering 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

Chair of Software Engineering 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”

Chair of Software Engineering 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

Chair of Software Engineering 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

Chair of Software Engineering 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