Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Next lecture Index References Contents | Following a practical course introduction, this lecture will focus on the transition from structured programming (in C, for instance) to object-oriented programming (in C#, for instance). We will discuss some the basic motivations in favor of object-oriented programming. In addition, we will review the inspiration from a theory about concepts and phenomena. |
From structured programming to object-oriented programming |
Structured Programming Slide Annotated slide Contents Index References Textbook | Structured programming stands as a contrast to unstructured programming. Unstructured programming is a reminiscence of low-level, machine language programming with explicit use of goto statements. |
|
Program Development by Stepwise Refinement is the title of a famous paper written by Niklaus Wirth in 1971. Niklaus Wirth is the inventor of the programming language Pascal. In this paper Wirth argues how to decompose a task in subtask, in a top-down fashion. |
|
A structured program: Hangman Slide Annotated slide Contents Index References Textbook |
In this lecture we start out by showing a structured program for the well-known Hangmang game.
In the hangman game the player is supposed to guess the letters of word or word phrase outline.
The game is similar to the TV-game Wheel of Fortune.
Each erroneous guess in Hangman is punished. The ultimate punishment is to be sent to the gallows.
At the end of the lecture, we will outline an object-oriented version of the Hangman program. |
Program: The main function of the Hangman program. |
|
Program: The function getPlayerName of main. |
|
Program: The function initHangman of main. |
|
Program: The function askUser of main. |
|
Program: The function playHangman of main. |
|
Program: The function InitGame of playHangman. |
|
Program: The function getPuzzle of playHangman. |
|
|
|
Exercise 1.2. How did you program the Hangman game? | This is an exercise for students who have a practical experience with the development of the Hangman program, or a similar game. Recall how you carried out the development of the program. To which degree did you adhere to top-down development by stepwise refinement? If you did not use this development approach, then please try to characterize how you actually did it. |
|
Observations about Structured Programming Slide Annotated slide Contents Index References Textbook |
Structured programming is not bad at all. Structured programming
is not necessarily ideal for large programs. Object-oriented programming
is strong with respect to structuring of large program. In addition,
object-oriented programming is based on conceptual reasoning, and strong metaphors. |
|
Towards Object-oriented Programming Slide Annotated slide Contents Index References Textbook | On this slide we list a few observations that bring us in the direction of object-oriented programming. |
The idea of abstract datatypes is central to object-oriented programming. On the following slide page we attempt to give you a basic idea behind abstract datatypes. |
Towards Object-oriented Programming |
Client, Servers, and Messages Slide Annotated slide Contents Index References Textbook | This is the first of a number of slides that will lead us in the direction of object-oriented programming. Each of these slides contribute a bit. At this moment, you may be in doubt how "these bits" work together. Please be patient, however. It will be clarified later in the material. |
|
The scenario of pizza ordering. The scenario focuses on a number of objects (persons) who communicate by message passing. |
Try the SVG-based animation here. To succeed, your brower must be SVG enabeled. In particular, SVG animations must be supported. This page has SVG examples, and explains how to set up Firefox with the Adobe SVG plugin. |
Responsibilities Slide Annotated slide Contents Index References Textbook | Responsibilities go hand in hand with contracts. Contracts are covered in the very end of this teaching material. |
Objects that manage too many responsibilities tend to be too complex. It is almost always better to distribute the responsibility on several "smaller objects". - The same is actually true for operations. |
Preconditions and postconditions is an important topic in the lecture about Contracts and Assertions. See here. |
|
Data-centered modularity Slide Annotated slide Contents Index References Textbook | In large program, modularity becomes a very important quality. |
The concept modularity: Modularity is the property of a computer program that measures the extent to which it has been composed out of separate parts called modules [Wikipedia] |
We distinguish between three different kinds of modularity. Procedural modularity is important in structure programming. Data-centered modularity is the thing to watch in object-oriented programming. |
|
Abstract Datatypes Slide Annotated slide Contents Index References Textbook |
The concept datatype: A datatype is a set of values with common properties. A datatype is a classification of data that reflects the intended use of the data in a program. | We first define a datatype, and after this an abstract datatype. The definitions given here are, to some degree, inspired by the free on-line dictionary of computing, FOLDOC at http://foldoc.org. Take a look at it! | |
The concept abstract datatype: An abstract datatype is a data type together with a set of operations on the values of the type. The operations hide and protect the actual representation of the data. |
Program: A specification of the abstract datatype stack. This an the other 'Programs' are examples of specifications of abstract datatypes. A specification tells what an abstract datatype is, NOT how it is implemented. You may skip these specifications, if you are not motivated right now... |
|
Program: A specification of the abstract datatype natural numbers. |
|
Program: A specification of the abstract datatype boolean. |
|
|
Reusability Slide Annotated slide Contents Index References Textbook |
In object-oriented programming, classes are meant to be reusable buildings blocks. Inheritance between classes is - in addition - an important mechanism that boosts reusability. |
The four items describe reusability challenges in general. |
Action on objects Slide Annotated slide Contents Index References Textbook |
In an objected-oriented program, most actions - as carried out by operations (instance methods) - happen on objects. |
In non-object-oriented program, actions can happen without targeting an object. In an object-oriented program, such action can be implemented by class methods. Class methods are known as static methods in C#. We act on an object obj by sending messages, see here. Either an outside object sends a message m to obj, or obj sends a message to itself. When an object receives a message, a it will attempt to find an operation which can answer the message. In some languages, the process of locating the answering operation, is non-trivial. |
Phenomena and Concepts |
Phenomena and Concepts Slide Annotated slide Contents Index References Textbook | We will now study concepts and phenomena, in order to get inspiration and guidance to our understanding of classes and objects. |
The concept phenomenon: A phenomenon is a thing that has definite, individual existence in reality or in the mind. Anything real in itself. | These are the definitions of 'phenomenon' and 'concept'. I have taken these from the PhD thesis of Jørgen Lindskov Knudsen and Kristine Stougaard Thomsen, Aarhus University. The thesis was published in the mid eighties. | |
The concept concept: A concept is a generalized idea of a collection of phenomena, based on knowledge of common properties of instances in the collection |
|
A concept is characterized by its name, its intension (= characteristic properties), and its extension (= the phenomena covered by the concept). Please pay attention to these words! |
Classification and exemplification Slide Annotated slide Contents Index References Textbook | On this page we focus on the connection between concepts and phenomena. |
The concept classify: To classify is to form a concept that covers a collection of similar phenomena. | ||
The concept exemplify: To exemplify is to focus on a phenomenon in the extension of the concept |
The relationships between concepts and phenomena. Given a concept we can identify the examples of phenomena in the extension of the concept. Given such an example, we can (the other way around) find the concept that classifies the sample phenomenon. |
Aggregation and Decomposition Slide Annotated slide Contents Index References Textbook | Aggregation and Decomposition is about the relationships between wholes and parts. |
The concept aggregate: To aggregate is to form a concept that covers a number of parts | ||
The concept decompose: To decompose is to split a concept into a number of parts |
An illustration of aggregation and decomposition. Notice that the relations between wholes and parts are in between concepts. Thus, aggregation and decomposition show how to form new concepts from existing concepts. |
Exercise 1.3. Aggregated Concepts | Take a look at the concepts which are represented by the phenomena in the room where you are located. Identify at least four aggregated concepts. Enumerate the concepts of the decomposition. |
Examples of Aggregation Slide Annotated slide Contents Index References Textbook |
An aggregation of a Bike in terms of Frame, Wheel, Brake, etc. This illustration does not capture the number of involved parts. Thus, the diagram does not capture the number of spokes per wheel, and the number of wheels per bike. The diamond shape is UML notation for aggregation. |
Generalization and Specialization Slide Annotated slide Contents Index References Textbook | Generalization and Specialization is yet another way to form new concepts from existing concepts. We have already studied aggregation and decomposition as ways of making new concepts from existing concepts. |
The concept generalization: Generalization forms a broader concept from a narrow concept | ||
The concept specialization: Specialization forms a narrow concept from a broader concept |
An illustration of generalization and specialization. |
Exercise 1.4. Concepts and Phenomena | The purpose of this exercise is to train your abilities to distinguish between concepts and phenomena. Decide in each of the following cases if the mentioned item is a concept or a phenomena:
For an item considered as a phenomenon, identify the underlying concept. |
Examples of Specialization Slide Annotated slide Contents Index References Textbook |
A generalization/specialization hierarchy of 'Means of Transport'. All the concepts in this diagram are specialized means of transport. Notice that all the nodes in the specialization trees are concepts - not individual phenomena. |
Exercise 1.5. University Concepts | In a university study, the study activities are usually structured in a number of semesters. There are two kinds of study activitities: projects and courses. At Aalborg University, there are currently two kinds of courses: Study courses (dk: studieenhedskurser) and project courses (dk: projektenhedskurser). Characterize the concepts of university study, study activity, semester, project, course, study course, and project course relative to Aggregation/Decomposition and Generalization/Specialization. |
Towards Object-oriented Programs |
An object-oriented program: Hangman Slide Annotated slide Contents Index References Textbook | As the end of the introductory lecture, we will approach an object-oriented Hangman program. |
|
The classes of a Hangman program. At the left hand side we see that PuzzleCollection is formed by Puzzle parts. Similarly, the HighscoreList is formed by HighScoreEntry parts. The HangManGame class if formed by three parts: PuzzleCollection, HighScoreList, and Player. Both file system and user interface aspects are "cloudy" in this diagram. |
An object-oriented program: Hangman Slide Annotated slide Contents Index References | Finally, in this lecture, we show incomplete sketches of the classes of the Hangman Game. The real important observation is the differences between the structured Hangman program, sketched earlier, and the object-oriented Hangman program outlined here. |
|
Program: The class Puzzle. |
|
Program: The class HighScoreEntry. |
|
Program: The class Player. |
|
Program: The class PuzzleCollection. |
|
Program: The class HighScoreList. |
|
Program: The class HangmanGame. |
|
|
Collected references Contents Index |
|
Chapter 1: Introduction to Object-oriented Programming
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:11:24