43
Chapter 1 Program Design

Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Embed Size (px)

Citation preview

Page 1: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Chapter 1

Program Design

Page 2: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 2

Objectives

• In this chapter you will be able to:

• Describe the steps in the program development process

• To introduce current program design methodology

• Introduce procedural and object-oriented programming

• Introduce algorithms

• Describe program data

Page 3: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 3

Program Design

• Most good programs require good planning before they are written. This is called Program Design.

Page 4: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 4

Steps in Program Development

The steps in program design are similar to the steps in problem solving. They are:

1. Define the problem

2. Outline the solution

3. Develop the outline into an algorithm

4. Test the algorithm for correctness

5. Code the algorithm into a specific programming language

6. Run the program on the computer

7. Document and maintain the program

Page 5: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 5

Steps in Program Development1 Define the Problem

To help with initial analysis, the problem should be divided into three separate components:

– the inputs - What information is given to you to use to solve the problem?

– the outputs - What will the solution of the problem, look like

– the processing steps - What calculations (processes), will be used to change the input information into the desired output?

• Tool: Defining diagram

Page 6: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 6

2 Outline the Solution

• Break the problem into the steps of what must be done to solve the problem:

– The major processing steps involved

– The major subtasks (if any)

– The user interface (if any)

– The major control structures (e.g. repetition loops)

– The major variables and record structures

– The mainline logic

Tools: Hierarchy or Structure charts

Page 7: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 7

3 Develop the Outline into an Algorithm

• The solution outline developed in Step 2 is

expanded into an algorithm:

• It should list all the steps that need to be done, in

the correct order they need to be done in.

Tools: Pseudocode, Flowcharts, Nassi-

Schneiderman diagrams

Page 8: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 8

4 Test the Algorithm for Correctness

• In this step, you pretend to be a computer and execute the steps

in the algorithm. This is called "Desk Checking."

• This step is one of the most important in the development

of a program, and yet it is the step most often forgotten

• The main purpose of desk checking the algorithm is to

identify major logic errors early, so that they may be easily

corrected

Page 9: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 9

5 Code the Algorithm into a Specific Programming Language

• Once the algorithm, or complete outline works

correctly, you need to translate the algorithm into

a computer language that your computer

understands.

– This is only 5 to 10% of the total programming process!

Page 10: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 10

6 Run the Program on the Computer

• This step uses a program compiler and programmer-

designed test data to machine test the code for syntax

errors (at compile time) and logic errors (at run time)

• You enter the program into the computer's memory, enter the

input data, and have the computer execute the program. If you get

the correct answer to the problem, the program works! Otherwise,

look for errors in the program and try again.

Page 11: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 11

7 Document and Maintain the Program

• Warning! The Documentation of the program started by writing down "What is the Problem!" It continues through each step of the design process.

• Documentation involves:

– external documentation

– internal documentation that may have been coded in the program

Page 12: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 12

Program Design Methodology

• Recently, a number of different approaches to program design have emerged, the most common being:

– Procedure-driven

– Event-driven

– Data-driven

Page 13: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 13

Procedure-Driven Program Design

• The procedure-driven approach to

program design is based on the idea that

the most important feature of a program is

‘what’ it does – that is, its processes or

functions

Page 14: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 14

Procedure-Driven Program Design

Example

• A program to execute a sales order may

be divided into:

– order entry module

– data verification module

– Inventory update module

Page 15: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 15

Procedure-Driven Program Design

• In procedure-driven programming, the sequence of operations for an application is determined by a central controlling program (e.g., a main procedure).

– The program determines in advance what will be done and in which order. The program starts at the beginning, occasionally calls subroutines

Page 16: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 16

Event-Driven Program Design

• The event-driven approach to program design is based on the idea that an event or interaction with the outside world can cause a program to change from one known state to another

• In event-driven programming the code responds to a system-generated event, such as a button-push or a clock cycle.

Page 17: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 17

Data-Driven Program Design

• The data-driven approach to program design is based on the idea that the data in a program is more stable than the processes involved

• It begins with an analysis of the data and the relationships between the data, in order to determine the fundamental data structures

• The choice between procedure-driven, event-driven, or data-driven program design methodologies is usually determined by the selection of a programming language

Page 18: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 18

Procedural versus Object-Oriented Programming

• Procedural programming is based on a structured, top-down approach to writing effective programs

• In the top-down development of a program design:

1. a general solution to the problem is outlined first

2. the general solution is then broken down gradually into more

detailed steps until finally the most detailed levels have been

completed

Page 19: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 19

Modular Design

• Procedural programming also incorporates the concept of modular design, which involves grouping tasks together because they all perform the same function

• Modular design is connected directly to top-down development

Page 20: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 20

Object-Oriented Programming

• Object-oriented programming is also

based on decomposing the problem;

however, the primary focus is on the

things that make up the program

Page 21: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 21

Quick Quiz

1. Name and define the second step in program development.

• ANSWER: Outline the solution- is the initial outline usually in a rough draft of the solution which may include: major processing steps involved, major subtasks, user interface, major control structures, major variables and record structures, and the mainline logic.

Page 22: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 22

Quick Quiz

2. The __________-driven approach to programming is based on the idea that an interaction with the outside world can cause a program to change from one known state to another.

• ANSWER: event

Page 23: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 23

Quick Quiz

3. The choice between procedure-driven, event-driven, or data-driven program design methodologies is usually determined by the selection of a(n) __________.

• ANSWER: programming language

Page 24: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 24

Quick Quiz

4. __________ programming is based on a structured, top-down approach to writing effective programs.

• ANSWER: Procedural

Page 25: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 25

Quick Quiz

5. ___________ programming is also based on decomposing the problem; however, the primary focus is on the things that make up the program.

• ANSWER: Object-oriented

Page 26: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 26

An Introduction to Algorithms and Pseudocode

• A program must be systematically and

properly designed before coding begins

• This design process results in the

construction of an algorithm

Page 27: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 27

What Is an Algorithm?

• An algorithm is like a recipe: it lists the steps

involved in accomplishing a task

• It can be defined in programming terms as a

set of detailed, unambiguous and ordered

instructions developed to describe the

process necessary to produce the desired

output from a given input

Page 28: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 28

What Is Pseudocode?

• Pseudocode, flowcharts, and Nassi-Schneiderman diagrams are all popular ways of representing algorithms

• Flowcharts and Nassi-Schneiderman diagrams are covered in Appendices 1 and 2, while pseudocode has been chosen as the primary method of representing an algorithm because it is easy to read and write and allows the programmer to concentrate on the logic of the problem

• Pseudocode is really structured English

– It is English that has been formalized and abbreviated to look like high-level computer languages

Page 29: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 29

Understanding Program Data

• Information processed by the computer is data.

1. a single variable, such as an integer or a

character, or

2. a group item (sometimes called an aggregate),

such as an array, or a file

Page 30: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 30

Variables, Constants, and Literals

• A variable is the name given to a collection of memory cells, designed to store a particular data item

• It is called a variable because the value stored in those memory cells may change or vary as the program executes

• Example

total = num1 + num2

Page 31: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 31

Variables, Constants, and Literals

• A constant is a data item with a name and a value that remain the same during the execution of the program

• A literal is a constant whose name is the written representation of its value

Page 32: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 32

Data Types

• At the beginning of a program, the

programmer must clearly define the form

or type of data to be collected

• The data types can be elementary data

items or data structures

Page 33: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 33

Elementary Data Items

• An elementary data item is one containing a single variable that is always treated as a unit

• The most common elementary data types are:

— Integer: holds a whole number

— Real: holds a decimal value, or is a number with a fractional part

— Character : holds a symbol such as a through z, 1 through 9, or !, @,#,$,%,^, etc

— Boolean: holds either a value of true or false.

Page 34: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 34

Data Structures

• A data structure is an aggregate of other data items

• The data items that it contains are its components, which may be elementary data items or another data structure

• The most common data structures are:

— Records: a collection of data items or field related to each other

— File: a collection of records

— Array: a collection of data items, all of the same type!

— String: a collection of zero or more characters.

Page 35: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 35

Files

• There are two different methods of storing data on files:

– Sequential or text files

– Direct or random-access files

Page 36: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 36

Data Validation

• Data should always undergo a validation check before it is processed by a program

• Different types of data require different checks – for example:

– Correct type — Correct range

– Correct length — Completeness

– Correct date

Page 37: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 37

Summary

• The steps in program development were introduced and briefly described below:

1. Define the problem

2. Outline the solution

3. Develop the outline into an algorithm

4. Test the algorithm for correctness

5. Code the algorithm into a specific programming language

6. Run the program on the computer

7. Document and maintain the program

Page 38: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 38

Summary

• Three different approaches to program design were introduced, namely procedure-driven, event-driven, and data-driven

• An algorithm was defined as a set of detailed, unambiguous and ordered instructions developed to describe the processes necessary to produce the desired output from the given input

• Pseudocode is an English-like way of representing the algorithm

Page 39: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 39

Quick Quiz

1. A(n) _____________ lists the steps involved in accomplishing a task.

• ANSWER: algorithm

Page 40: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 40

Quick Quiz

2. ___________ is structured English that has been formalized and abbreviated to look like high-level computer languages.

• ANSWER: Pseudocode

Page 41: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 41

Quick Quiz

3. A(n) __________ is the name given to a collection of memory cells, designed to store a particular data item.

• ANSWER: variable

Page 42: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 42

Quick Quiz

4. Name three of the most common elementary data types.

• ANSWER: Integer, Real, Character, and Boolean

Page 43: Chapter 1 Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the

Simple Program Design, Fourth Edition Chapter 1 43

Quick Quiz

5. ___________ files may be opened to read or to write, but not both operations on the same file.

• ANSWER: Sequential