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.”