Download presentation
Presentation is loading. Please wait.
1
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 2 23 September 2008
2
Chair of Software Engineering 2 Organizational Assignments One assignment per week Will be put online on Friday (download it from the web site) Will be due on Thursday the next week (Assignment 1 was an exception) Asking questions Exercise sessions Email/come to my office Group mailing list Is everybody subscribed?
3
Chair of Software Engineering 3 Today Purpose of the exercise session: Give you the intuition behind object-oriented (OO) programming Teach you about formatting your code Get rolling with EiffelStudio & Traffic (Assignment 1)
4
Chair of Software Engineering Objects and classes Classes are pieces of software code One or several classes can 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
5
Chair of Software Engineering 5 Calling a feature on an object (1) next_message send computer shut_down telephone ring Every operation applies to an object and may take arguments
6
Chair of Software Engineering 6 Calling a feature on an object (2) next_message send_to (recipient) computer shut_down_after (3) telephone ring_several (10, Loud) Every operation applies to an object and may take arguments
7
Chair of Software Engineering 7 Exercise: class BANK_ACCOUNT 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) Hands-On
8
Chair of Software Engineering 8 class PREVIEW inherit TOURISM feature explore is -- Show city info and route. do “To be filled in (by you!)” end A class text Softwar e machine Extend an existing class Operations Featur e name Commen t Keywords have a special meaning: class, inherit, feature, is, do, end. Feature declaratio n Pseudocod e
9
Chair of Software Engineering 9 Da style rules! 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
10
Chair of Software Engineering 10 Exercise: format class bank_account feature deposit (sum: INTEGER) is -- Add `sum' to the account. do balance := balance + sum end balance: INTEGER end Hands-On
11
Chair of Software Engineering 11 Solution: format class BANK_ACCOUNT feature deposit (sum: INTEGER) is -- Add `sum' to the account. do balance := balance + sum end balance: INTEGER end Hands-On
12
Chair of Software Engineering Assignment 1 Take the remaining time to work on Assignment 1. Try to solve the tasks on your own. Ask me if you have any problems. (We’ll have such a session every week for each assignment.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.