[go: up one dir, main page]

0% found this document useful (0 votes)
35 views65 pages

Object-Oriented Design and High-Level Programming Languages

The document discusses object-oriented design and high-level programming languages, outlining the goals of understanding object-oriented design processes, essential elements of object-oriented languages, different programming paradigms, and how constructs of top-down and object-oriented design are implemented in languages. It provides details on the object-oriented design process including brainstorming, filtering, defining scenarios and responsibilities, and gives examples using a computer address book problem to illustrate the process.

Uploaded by

Muhammad Umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views65 pages

Object-Oriented Design and High-Level Programming Languages

The document discusses object-oriented design and high-level programming languages, outlining the goals of understanding object-oriented design processes, essential elements of object-oriented languages, different programming paradigms, and how constructs of top-down and object-oriented design are implemented in languages. It provides details on the object-oriented design process including brainstorming, filtering, defining scenarios and responsibilities, and gives examples using a computer address book problem to illustrate the process.

Uploaded by

Muhammad Umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 65

Chapter 8

Object-Oriented
Design and
High-Level
Programming
Languages
Chapter Goals

• Distinguish between functional design and


object-oriented design
• Describe the stages of the object-oriented
design process
• Apply the object-oriented design process
• Name, describe, and give examples of the three
essential ingredients of an object-oriented
language

2
Chapter Goals

• Describe the translation process and distinguish


between assembly, compilation, interpretation,
and execution
• Name four distinct programming paradigms and
name a language characteristic of each
• Define the concepts of a data type and strong
typing

3
Chapter Goals

• Understand how the constructs of top-down and


object-oriented design are implemented in
programming languages

4
Object-Oriented Design

Object-oriented Design
A problem-solving methodology that produces a
solution to a problem in terms of self-contained
entities called objects
Object
A thing or entity that makes sense within the
context of the problem
For example, a student, a car, time, date

5
Object-Oriented Design

World View of OOD


Problems are solved by
– isolating the objects in a problem,
– determining their properties and actions
(responsibilities), and
– letting the objects collaborate to solve a
problem
What? Say again!

6
Object-Oriented Design

An analogy: You and your friend fix dinner


Objects: you, friend, dinner
Class: you and friend are people
People have name, eye color, …
People can shop, cook, …
Instance of a class: you and friend are instances of
class People, you each have your own name
and eye color, you each can shop and cook
You collaborate to fix dinner
7
Object-Oriented Design

Class (or object class)


A description of a group of similar objects
Object (instance of a class)
A concrete example of the class
Classes contain fields that represent the
properties (name, eye color) and
behaviors (responsibilities) (shop, cook) of the class
Method
A named algorithm that defines behavior (shop, cook)

8
Object-Oriented Design

Top-Down Design
decomposes problems into tasks
Object-Oriented Design
decomposes problems into
collaborating objects

Yes, but how?

9
Object-Oriented Design

Steps
1. isolate the real-world objects in the
problem
2. abstract the objects with like properties
into groups (classes)
3. determine the responsibilities of the
group in interacting with other groups

10
Object-Oriented Design
Methodology
Four stages to the decomposition process
– Brainstorming to locate possible classes
– Filtering the classes to find duplicates or
remove unnecessary ones
– Scenarios are tried to be sure we understand
collaborations
– Responsibility algorithms are designed for all
actions that classes must exhibit
11
Brainstorming

A group problem-solving technique that


involves the spontaneous contribution of
ideas from all members of the group
– All ideas are potential good ideas
– Think fast and furiously first, and ponder later
– A little humor can be a powerful force

Brainstorming is designed to produce a list


of candidate classes
12
Filtering

Determine which are the core classes in the


problem solution
There may be two classes in the list that
have many common attributes and
behaviors
There may be classes that really don’t
belong in the problem solution

13
Scenarios

Assign responsibilities to each class


There are two types of responsibilities
– What a class must know about itself
(knowledge responsibilities)
– What a class must be able to do (behavior
responsibilities)

14
Scenarios

Encapsulation
The bundling of data and actions in such a
way that the logical properties of the data
and actions are separated from the
implementation details
Each class encapsulates its data but
shares their values through knowledge
responsibilities
15
Responsibility Algorithms

The algorithms must be written for the


responsibilities

– Knowledge responsibilities usually just return


the contents of one of an object’s variables
– Action responsibilities are a little more
complicated, often involving calculations

16
Computer Example

Let’s examine the problem-solving process


for creating an address list
Brainstorming and filtering
– Circling the nouns and underlining the verbs is
a good way to begin

17
Responsibility Algorithms

Person Class
Initialize Tells name to initialize itself

name.initialize()
Write "Enter phone number; press return."
Get telephone number
Write "Enter email address; press return."
Get email address
Tells name to print itself
Print

name.print()
Write "Telephone number: " + telephoneNumber
Write "Email address: " + emailAddress

18
Responsibility Algorithms
Name Class

Initialize

"Enter the first name; press return."


Read firstName
"Enter the last name; press return."
Read lastName

Print

Print "First name: " + firstName


Print "Last name: " + lastName

19
Object Oriented Problem Solving
and Implementation Phases

20
Translation Process

A program written in a high-level language


must be translated into machine code
The machine code is then executed
Compilers and Interpreters are software
tools employed to help with the translation
process

21
Compilers

High-level language
A language that provides a richer (more
English-like) set of instructions
Compiler
A program that translates a high-level
language program into machine code

22
Compilers

How does this differ from


the assembly process?

23
Interpreters

Interpreter
A translating program that translates and executes
the statements in sequence
– Assembler or compiler produce machine code as
output, which is then executed in a separate step
– An interpreter translates a statement and then
immediately executes the statement
– Interpreters can be viewed as simulators

24
Java

• Introduced in 1996 and became instantly


popular
• Portability was of primary importance
• Java is compiled into a standard machine
language called Bytecode
• A software interpreter called the JVM
(Java Virtual Machine) takes the Bytecode
program and executes it
25
Portability

Portability
The ability of a program to be run on different machines

Compiler portability
A program in a standardized language can be compiled
and run on any machine that has the appropriate compiler

Bytecode portability
A program translated into Bytecode can be run on
any machine that has a JVM

Do you understand the difference?


26
Portability

27
Portability

28
Programming Language Paradigms

Imperative Paradigm
Program describes the processing
Declarative Paradigm
Program describes the result
Each of these major paradigms have distinct
subparadigms.

29
Programming Language
Paradigms
Imperative
–Procedural
• Characterized by sequential instructions
• A program in which statements are grouped into a
hierarchy of subprograms
• Fortran, C, C++
–Object-oriented model
• Program consists of a set of
objects and the interactions among
the objects.
• Python, Java, Smalltalk, Simula

30
Programming Language Paradigms

Declarative
–Functional
• Based on the mathematical concept of a function
• Lisp, Scheme, and ML

–Logic
• Based on principles of symbolic logic
• Types of statements
– declares facts about objects and relationships
– defines rules about objects
– asks questions about objects
• PROLOG

31
Scheme

#;> (* 3 4)
12
#;> (+ (* 5 4)(+ 1 4))
25
#;> (length '(2 4 6 8 10))
5
#;> (max 2 5 1 3)
5

32
PROLOG
Pets to owners
owns(mary,bo).
owns(ann,kitty).
owns(bob,riley). States
facts
owns(susy,charlie).

?-owns(mary,bo)
yes
Asks
?-owns(bo,mary) questions
no
?-owns(susy,bo)
33 no
Prolog

?-owns(ann, Cat). Upper case is


variable; lower
Cat =
case is
kitty constant
?-owns(Name,charlie).
Name = susy

34
Programming Language
Paradigms

C++ is a procedural language with some object-


oriented features

Java is an object-oriented language with some


procedural features

35
Functionality of High-Level
Languages
We examine procedural and object-oriented
languages in the rest of this chapter by looking at
the functionality provided in these languages

We give examples in different languages to show


how syntax used to provide the functionality

36
Functionality of Imperative
Languages
Sequence
Executing statements in sequence until an instruction is
encountered that changes this sequencing
Selection
Deciding which action to take
Iteration (looping)
Repeating an action
Do these concepts sound familiar?
Let's review them
37
Boolean Expressions

Boolean expression
A sequence of identifiers, separated by compatible
operators, that evaluates to true or false
A Boolean expression can be
– A Boolean variable
– An arithmetic expression followed by a relational
operator followed by an arithmetic expression
– A Boolean expression followed by a Boolean
operator followed by a Boolean expression
38
Strong Typing

Data type
A description of the set of values and the basic set
of operations that can be applied to values of the
type
Strong typing
The requirement that only a value of the proper
type can be stored into a variable

39
Data Types

Integer numbers
Real numbers
Characters
Boolean values
Strings

40
Declarations

Declaration
A statement that associates an identifier with a
variable, an action, or some other entity within the
language that can be given a name; the
programmer can refer to that item by name
Reserved word
A word in a language that has special meaning
Case-sensitive
Uppercase and lowercase letters are considered the
same
41
Declaration Example
Assignment statement

Assignment statement
An action statement (not a declaration) that says to
evaluate the expression on the right-hand side of
the symbol and store that value into the place
named on the left-hand side

43
Input/Output Structures

Pseudocode algorithms used the expressions


Read or Get and Write or Print
High-level languages view input data as a stream
of characters divided into lines
Key to the processing
The data type determines how characters are to
be converted to a bit pattern (input) and how a bit
pattern is to be converted to characters (output)

44
Input/Output Structures

Read name, age, hourlyWage

name is a string;
age is an integer;
hourlyWage is a real

The data must be a string, an integer, and a


real in that order.

45
Input/Output Structures

46
Control Structures

Control structures
An instruction that determines the order in
which other instructions in a program are
executed
Can you name the ones we defined in the
functionality of pseudocode?

47
Selection Statements
The if statement allows the program to test the state of the
program variables using a Boolean expression

48
Looping Statements

49
Subprogram Statements

We can give a section of code a name and


use that name as a statement in another
part of the program
When the name is encountered, the
processing in the other part of the program
halts while the named code is executed

Remember?

50
Subprogram Statements

51
Nested Logic
Set sum to 0 // Initialize sum
Set posCount to 0 // Initialize event
WHILE (posCount <= 10) // Test event
Read a value
IF (value > 0) // Update event?
Set posCount to posCount + 1
// Update event
Set sum to sum + value
// Statement(s) following loop

IF within a WHILE
52
Asynchronous Processing

Asynchronous processing
Not synchronized with the program's action
– Clicking has become a major form of input
to the computer
– Mouse clicking is not within the sequence
of the program
– A user can click a mouse at any time during
the execution of a program
53
Functionality of OOPs
Encapsulation
A language feature that enforces information
hiding
Classes
Different meanings in different places (See next
slide)
Inheritance
A property that allows a class to inherit the data
and actions of another class
Polymorphism
An ability to handle the ambiguity of duplicate
names
54
Functionality of OOPs
Object (problem-solving phase)
An entity or thing that is relevant in the context of a
problem
Object class (class) (problem-solving phase)
A description of a group of objects with similar
properties and behaviors
Class (implementation phase) A pattern for an object
and provides mechanism for encapsulating the
properties and actions of object class.
Object ( implementation phase) An instance of a
class
55
Class Definition

A class encapsulates both data and actions

public class Person // Name the class


// Declare Class variables
Name name
String telephone
String email

56
Class Definition

// Declare Class Methods


Initialize() // Code for Initialize
public Print() // Code for Print
public Name GetName()
RETURN Name
public String GetEmail()
RETURN email
public String GetTelephone()
RETURN telephone

57
Class Definition

58
Class Definition
Name aName = new Name()
aName.Initialize("Frank", "Jones")
Person aPerson = new Person()
aPerson.Initialize(aName, telephone, email)
aPerson.Print()
Write "Name: ", aPerson.GetName().Print()
Write " Telephone: ", aPerson.GetTelephone()
Write " Email: ", a Person.GetEmail()

59
Class Definition
To get an object of a class, we must ask that one be
created (instantiated). The new operator does this
for us
Person myPerson = new Person()
Student myStudent = new Student()
myPerson.Initialize(…)
myStudent.Initialize(…)
myPerson.Print()
myStudent.Print()

60
Inheritance and Polymorphism
Inheritance
Is a property of object-oriented languages in which
classes can inherit data and methods from other classes.
This relationship is an “is-a” relationship.
A superclass is a class being inherited from; a derived
class is a class doing the inheriting
Polymorphism
The ability of a language to have duplicate method names
in an inheritance hierarchy and to apply the method that is
appropriate for the object to which the method is applied.

61
G
h
u
l
a

What is Inheritance
m

I
s
h
a
q Planes
-Wings,
K
h Generic properties -engines,
a of all planes -Cockpit
n
+ take off
I + land
n
s
+ navigate
t
i
t
u
t
e

S
p
r Specific properties Fighter Planes Commercial Planes
i of particular types Allattributes and All attributes and
n
g of planes methods of Plane methods of Plane

2 +Fire +Cabin Lightings


0
1 Weapon +Inflight
2 +Self Entertainment 6
defence 2
Polymorphism
Inheritance and Polymorphism

Inheritance and polymorphism work together


How?
They combine to allow the programmer to build
useful hierarchies of classes that can be put into a
library to be reused in different applications

64
Top-Down Design

Top-down Solution
Data structures needed in solution are
determined
Subprograms are written to manipulate the
the data structures
Main program declares data structure
Main program calls to the subprograms,
passing data structures as parameters

65

You might also like