Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 2 23 September 2008.
Published byModified over 4 years ago
Presentation on theme: "Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 2 23 September 2008."— Presentation transcript:
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 2 23 September 2008
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?
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)
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
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
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
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
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
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
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
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
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.)