Object-Oriented Design and High-Level Programming Languages
Object-Oriented Design and High-Level Programming Languages
Object-Oriented
Design and
High-Level
Programming
Languages
Chapter Goals
2
Chapter Goals
3
Chapter Goals
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
6
Object-Oriented Design
Top-Down Design
decomposes problems into tasks
Object-Oriented Design
decomposes problems into
collaborating objects
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
dog's
birth date
dogBirthdate
marriageDate
Description Instances
12
Object-Oriented Design
Create itself
Know the state of its fields
Compare itself to another date
Return a date a number of days hence
13
Object-Oriented Design
marriageDate
14
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
15
Brainstorming
17
Scenarios
18
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
19
Responsibility Algorithms
20
CRC Cards
CRC cards are a notational device to record information
about a class, what it must do and with whom it must
collaborate
21
Computer Example
22
Computer Example
23
CRC Cards
24
CRC Cards
25
CRC Cards
26
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
27
Responsibility Algorithms
Name Class
Initialize
28
Object Oriented Problem Solving
and Implementation Phases
29
Translation Process
30
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
31
Compilers
32
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
33
Java
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
36
Portability
37
Programming Language
Paradigms
Imperative Paradigm
Program describes the processing
Declarative Paradigm
Program describes the results
Each of these major paradigms have distinct
subparadigms
38
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
39
Programming Language
Paradigms
C++ is a procedural language with some object-
oriented features
40
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
41
Scheme
#;> (* 3 4)
12
#;> (+ (* 5 4)(+ 1 4))
25
#;> (length '(2 4 6 8 10))
5
#;> (max 2 5 1 3)
5
42
Scheme
43
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)
44 no
PROLOG
45
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
46
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
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
48
Boolean Expressions
49
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
50
Data Types
Integer numbers
Real numbers Give examples
of
Characters each
Boolean values
Strings
51
Integers
52
Reals
53
Characters
Do you remember
ASCII?
Extended ASCII?
UNICODE?
How many characters in Extended ASCII?
How many characters in UNICODE mapping?
What does a relational operator between two
characters mean?
54
Boolean and Strings
55
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
56
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
Named constant
A location in memory, referenced by an identifier,
that contains a data value that cannot be changed
Remember?
58
Input/Output Structures
59
Input/Output Structures
name is a string;
age is an integer;
hourlyWage is a real
60
Input/Output Structures
61
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?
62
Selection Statements
The if statement allows the program to test the state of the
program variables using a Boolean expression
63
Looping Statements
64
Subprogram Statements
Remember?
65
Subprogram Statements
66
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
67
Set weekCount to 1
WHILE (weekCount<= 52)
Set weekSum to 0
Set dayCount to 1
WHILE (dayCount <= 7)
Read rainfall
Set weekSum to weekSum + rainfall
Set dayCount to dayCount + 1
Write “Week “ + weekCount + “ total: “ +
weekSum
Set weekCount to weekCount +
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
70
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
71
Functionality of OOPs
Object class (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
Object ( implementation phase) An instance of a
class
72
Class Definition
73
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
74
Class Definition
75
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()
76
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()
77
Inheritance and Polymorphism
Inheritance
A construct that fosters reuse by allowing an
application to take an already-tested class and
derive a class from it that inherits the properties
the application needs
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
78
Inheritance and Polymorphism
79
Top-Down vs OO Designs
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
80
Top-Down vs OO Designs
Object-oriented Solution
ADTs needed in solution are determined
ADTs are written only if not in library
Data structure is encapsulated within the
class that implements the ADT
Main program is instructions to ADTs to
perform the necessary tasks
81
Ethical Issues
82
Ethical Issues
Computer Hoaxes and Scams
What is the principal difference between a
hoax and a scam?
What are the most common complaints of
Internet users about computer scams
and hoaxes?
What are the most serious crimes
perpetrated on the Web?
Why is it so difficult to police these schemes?
83
Who am I?
I am best known
for structured
programming.
Can you define
it?
I am also known
for my wit. Can
you recall some
of my witty
sayings?
84
Do you know?
85