1 chapter-01 programming methodologies procedural/structured design objected-oriented design

17
1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Upload: ada-wilkerson

Post on 29-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

1

Chapter-01

Programming Methodologies

• Procedural/Structured Design

• Objected-Oriented Design

Page 2: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

2

Object Autonomy? OOP

Page 3: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

3

Algorithm (Unstructured Version)/* Algorithm to read and count several triples of distinct numbers and print the largest number in each triple. */

1. Initialize count to 0

2. Read a triple x, y, z.

3. If x is the end-of-data flag then go to step 14.

4. Increment count by 1.

5. If x > y then go to step 9.

6. If y > z then go to step 12.

7. Display z.

8. Go to step 2.

9. If x < z then go to step 7.

10. Display x.

11. Go to step 2.

12. Display y.

13. Go to step 2.

14. Display count.

Note the spaghetti logic!

Page 4: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

4

Algorithm (Structured Version)

/* Algorithm to read and count several triples of distinct numbers and print the largest number in each triple. */

1. Initialize count to 0.

2. Read the first triple of numbers x, y, z.3. While x is not the end-of-data-flag do the following:

a. Increment count by 1.b. If x > y and x > z then display x.

Else if y > x and y > z then display y

Else display z.

c. Read the next triple x, y, z.

4. Display count.

Page 5: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

5

Different Programming:1. Spaghetti code - with

goto statements

2. Structured Programming -reduction of program into its constituent elements.

3. OOP -decomposes a problem into related subgroups

Each subgroup becomes a self-contained object that contains its own instructions and data that relates to that object.

Complexity is reduced and the programmer can manage large programs.

Page 6: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

Functional Decomposition

A technique for developing a program in which the problem is divided into more easily handled subproblems, the solutions of which create a solution to the overall problem.

In functional decomposition, we work from the abstract (a list of the major steps in our solution) to the particular (algorithmic steps that can be translated directly into code in C++ or another language).

Page 7: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

Functional DecompositionFOCUS is on actions and algorithms.

BEGINS by breaking the solution into a series of major steps. This process continues until each subproblem cannot be divided further or has an obvious solution.

UNITS are modules representing algorithms. A module is a collection of concrete and abstract steps that solves a subproblem. A module structure chart (hierarchical solution tree) is often created.

DATA plays a secondary role in support of actions to be performed.

Page 8: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

ComputeMileages

WriteTotal Miles

Module Structure Chart

Main

Get Data

Round To Nearest Tenth

Initialize Total MilesOpen Files

Page 9: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

Object-Oriented Design

A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.

Private data

<<

setf...

Private data

>>

get...

ignore

cin cout

setw

Page 10: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

More about OOD languages supporting OOD include: C++, Java,

Smalltalk, Eiffel, CLOS, and Object-Pascal

a class is a programmer-defined data type and objects are variables of that type

in C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream and fstream contain definitions of stream classes

a class generally contains private data and public operations (called member functions)

Page 11: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

Object-Oriented Design (OOD)

FOCUS is on entities called objects and operations on those objects, all bundled together.

BEGINS by identifying the major objects in the problem, and choosing appropriate operations on those objects.

UNITS are objects. Programs are collections of objects that communicate with each other.

DATA plays a leading role. Algorithms are used to implement operations on the objects and to enable interaction of objects with each other.

Page 12: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed 12

Two Programming Methodologies

Functional Object-Oriented Decomposition Design

FUNCTION

FUNCTION

FUNCTION

OBJECT

Operations

Data

OBJECT

Operations

Data

OBJECT

Operations

Data

Page 13: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed 13

What is an object?

OBJECT

Operations

Data

set of functions

internal state

Page 14: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed 14

An object contains data and operations

Private data:

accoutNumber

balance

OpenAccount

WriteCheck

MakeDeposit

IsOverdrawn

GetBalance

checkingAccount

Page 15: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

Why use OOD with large software projects?

objects within a program often model real-life objects in the problem to be solved

many libraries of pre-written classes and objects are available as-is for re-use in various programs

the OOD concept of inheritance allows the customization of an existing class to meet particular needs without having to inspect and modify the source code for that class--this can reduce the time and effort needed to design, implement, and maintain large systems

Page 16: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

Bazlur Rasheed

Two Approaches to Building Manageable Modules

Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded.

Identifies various objects composed of data and operations, that can be used together to solve the problem.

FUNCTIONALDECOMPOSITION

OBJECT-ORIENTED DESIGN

FOCUS ON: processes FOCUS ON: data objects

Page 17: 1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design

17

A commercial for OOP: Two programming paradigms

• Procedural: ( C, FORTRAN, and Pascal )

– Action-oriented — concentrates on the verbs of a problem's specification

– Programmers:

• Identify basic tasks to be performed to solve problem

• Implement the actions required to do these tasks as subprograms (procedures/functions/subroutines)

• Group these subprograms into programs/modules/libraries, which together make up a complete system for solving the problem

• Object-oriented: ( C++, Java, and Smalltalk)

– Focuses on the nouns of a problem's specification

– Programmers:• Determine what objects are

needed for a problem and how they should work together to solve the problem.

• Create types called classes made up of data members and function members to operate on the data. Instances of a type (class) are called objects.