10/10/2007

1

Introduction to Programming Bertrand Meyer

Exercise Session 3 Ilinca Ciupa, Hermann Lehner 2 October 2007 Chair of Software Engineering

Assignments

2

ƒ Deadline for assignment 1 over ƒ Deadline for assignment 2 on Thursday

Chair of Software Engineering

1

10/10/2007

Today

3

Purpose of the exercise session: ƒ Gi Give you the h intuition i i i b hi d object-oriented behind bj i d (OO) programming ƒ Teach you about formatting your code ƒ Answer questions to Assignment 2

Chair of Software Engineering

A class text Softwar e machine Operations

Featur e name

Commen t

class Extend an PREVIEW existing inherit class TOURISM feature explore is -- Show city info and route. do “To be filled in ((by y you!)” y ) end end

4

Feature declaratio n

Pseudocod Keywords have a special meaning: class, inherit, feature,e is, do, end.

Chair of Software Engineering

2

10/10/2007

Da style rules!

5

ƒ For indentation, use tabs, not spaces ƒ Use this property to highlight the structure of the program, particularly through indentation • CLASS NAMES: ALL UPPERCASE • Period in feature call: no space before or after Chair of Software Engineering

Exercise: format

6

class bank_account bank account feature deposit (sum: INTEGER) is -- Add `sum' to the account. do balance := balance + sum end balance: INTEGER end

Chair of Software Engineering

3

10/10/2007

Solution: format

7

class BANK_ACCOUNT feature deposit (sum: INTEGER) is -- Add `sum' to the account. do balance := balance + sum end balance: INTEGER end Chair of Software Engineering

Features, commands and queries

8

Feature: an operation available on a certain class of objects Three kinds: ƒ Command ƒ Query ƒ Creation procedure (seen later)

Chair of Software Engineering

4

10/10/2007

Defining and classifying features

9

A feature is an operation that programs may apply to certain classes of objects. • A feature that accesses an object is a query • A feature that may modify an object is a command

Chair of Software Engineering

Command-query separation principle

10

“A ki “Asking a question i shouldn’t h ld ’ change h the h answer””

i.e. “query”

Chair of Software Engineering

5

10/10/2007

Queries

11

Goal: obtain properties of objects Sh ld not modify Should dif the h object, bj or any other h Examples, for “route” objects: ƒ ƒ ƒ ƒ

What is the origin (first station) of Route1? What is the end point of Route1? How many steps does Route1 use? Which stations does Route1 traverse?

Chair of Software Engineering

Queries – other examples ƒ ƒ ƒ ƒ ƒ

12

What is the name of a person? What is the age of a person? Wh iis the What h id off a student? d ? Is a student registered for a particular course? Are there any places left in a certain course?

Can you think of any other examples?

Chair of Software Engineering

6

10/10/2007

Commands

13

Goal: produce a change on an object, or several Examples, for “route” objects:

ƒ Animate Route1 ƒ Prepend (add at the beginning) a segment to Route1 ƒ Append (add at the end) a segment to Route1.

Chair of Software Engineering

Commands – other examples

14

ƒ Register a student to a course ƒ Assign an id to a student ƒ Record R d the h grade d a student d got in i an exam

Can you think of any other examples?

Chair of Software Engineering

7

10/10/2007

Exercise: query or command?

ƒ ƒ ƒ ƒ ƒ ƒ ƒ ƒ

15

What is Wh i the h balance b l off a bank b k account? ? Withdraw some money from a bank account Who is the owner of a bank account? Get all 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?

Chair of Software Engineering

Syntax and semantics

16

The syntax of a program is the structure and form of its text. The semantics of a program is the set of properties of its potential executions.

Chair of Software Engineering

8

10/10/2007

Syntax and semantics

17

ƒ Syntax is the way you write a program: characters grouped into words grouped into bigger structures ƒ Semantics is the effect you expect from this program

Chair of Software Engineering

Exercise: Syntax or semantics?

18

Describe the syntax and semantics for… ƒ ƒ ƒ ƒ ƒ ƒ ƒ

Assignment A i Feature Query Expression Return Value Command Indentation

Chair of Software Engineering

9

10/10/2007

Solution: Syntax or semantics?

19

Describe the syntax and semantics ƒ Assignment A i Syntax: “v := e” Semantics: “The assignment statement assigns the value of the evaluation of expression ‘e’ e to variable ‘v’.”

Chair of Software Engineering

Solution: Syntax or semantics?

20

Describe the syntax and semantics ƒ Return R V Value l Syntax: “Result” Semantics: “The value of Result at the end of a feature-body feature body execution represents the return value of a feature.”

Chair of Software Engineering

10

10/10/2007

21

End exercise session 3

Chair of Software Engineering

11