chapter01 - E

advertisement
An Object-Oriented Approach to
Programming Logic and Design
Fourth Edition
Chapter 1
An Overview of Computer
Programming
Objectives
In this chapter, you will learn about:
• Computer components and operations
• Simple program logic
• The evolution of programming models
• The steps in the programming process
• Pseudocode and flowcharts
• Program comments
• Programming and user environments
An Object-Oriented Approach to Programming Logic and Design
2
Understanding Computer
Components and Operations
• Computer system
– Combination of all components required to process and
store data using a computer
• Two major computer system components
– Hardware
• Physical devices associated with the computer
– Software
•
•
•
•
Computer instructions that tell the hardware what to do
Programs (sets of instructions) written by programmers
Application software: programs applied to a task
System software: programs that manage computer resources
An Object-Oriented Approach to Programming Logic and Design
3
Understanding Computer
Components and Operations (cont’d)
• Three major hardware and software operations
– Input
– Processing
– Output
• Input operation
– A means for data to enter a computer through an input
device such as a mouse or keyboard
An Object-Oriented Approach to Programming Logic and Design
4
Understanding Computer
Components and Operations (cont’d)
• Processing data operation
–
–
–
–
Organizing or sorting data
Checking for accuracy
Performing mathematical operations
Tasks occur in central processing unit (CPU)
• Output operation
– Means to view, print, or store data using output devices
• Printer or monitor
• Storage devices (e.g., disks, flash media)
• Information: data that has been processed
An Object-Oriented Approach to Programming Logic and Design
5
Understanding Computer
Components and Operations (cont’d)
• Programming language
– Instructions controlling data manipulation
– Examples include Visual Basic, C#, C++, or Java
• Syntax
– Rules governing word usage and punctuation for a
programming language
– Syntax errors: mistakes in a language’s usage
• Program code
– Instructions written in a programming language
An Object-Oriented Approach to Programming Logic and Design
6
Understanding Computer
Components and Operations (cont’d)
• Computer memory (RAM)
– Temporary, internal storage
• Executable statements
– Statements that carry out a program’s actions
• Compiler or interpreter
– Translate high level programming language statements into
low-level machine or binary language
– Point out syntax errors
An Object-Oriented Approach to Programming Logic and Design
7
Understanding Computer
Components and Operations (cont’d)
• Compiler
– Translates entire program before execution
• Interpreter
– Translates an instruction just prior to execution
• Source code
– Program statements written in a programming language
• Object code
– Code translated into machine language statements
An Object-Oriented Approach to Programming Logic and Design
8
Understanding Simple Program Logic
• Logic errors
– Program may execute but produces incorrect results
– Can be avoided by
• Writing program instructions in specific sequence
• Removing extraneous instructions
• Including all required instructions
– Example of logic errors in cake-making instructions
Stir
Add two eggs
Add a gallon of gasoline
Bake at 350 degrees for 45 minutes
Add three cups of flour
An Object-Oriented Approach to Programming Logic and Design
Danger:
Do not follow these cakebaking instructions!
9
Understanding Simple Program Logic
(cont’d)
• Computer programs include steps that perform
input, processing, and output
• Sample instructions written in English-like language
– Program doubles any given number
– Requires three operations: input, processing, and output
input myNumber
myAnswer = myNumber * 2
output myAnswer
An Object-Oriented Approach to Programming Logic and Design
 input
 processing
 output
10
Understanding the Evolution
of Programming Models
• Modern computer programs
– Around since 1940s
• Oldest programming languages
– Programmers worked with memory addresses
– Memorized awkward codes associated with machine
languages
– Written as one piece
An Object-Oriented Approach to Programming Logic and Design
11
Understanding the Evolution
of Programming Models (cont’d)
• Newer programming languages
– Look like natural language
– Use meaningful names for memory locations
– Allow creation of self-contained modules or program
segments
• Can be pieced together
• Can be reused
An Object-Oriented Approach to Programming Logic and Design
12
Understanding the Evolution
of Programming Models (cont’d)
• Major program development techniques
– Procedural programming
• Focuses on actions that are carried out
• Breaks down processes into manageable subtasks
– Object-oriented programming – focuses on objects
– Objects: entities that have attributes, behaviors, and states
• Attributes: object features
• Behaviors: what object does
• States: set of all values of attributes
– Primary difference is focused on early planning stages
An Object-Oriented Approach to Programming Logic and Design
13
Understanding the Evolution
of Programming Models (cont’d)
• Object-oriented approach
– Defining the objects needed to accomplish a task
– Each object maintains its own data and carries out tasks
– “Natural” way of thinking about the world
• Types of object-oriented approach applications
– Computer simulations
• Mimic real-world activities
• Users apply programs to tasks after the programs are written
– Graphical user interfaces (GUIs)
• Users interact with program in graphical environment
An Object-Oriented Approach to Programming Logic and Design
14
Understanding the Steps
in the Programming Process
• System program
– A group of many programs
• Object-oriented approach involves
– Analyzing the system
– Designing the system
– Writing the programs
• Programmer may do all tasks or use systems analysts
and/or software testers
An Object-Oriented Approach to Programming Logic and Design
15
Understanding the Steps
in the Programming Process (cont’d)
• Types of software testing
– Black box
• Tester provides input and checks for valid output
– White box
• Tester looks at code, tests all logical paths
An Object-Oriented Approach to Programming Logic and Design
16
Analyzing the Program or System
• Programmers provide a service to users
• Object-oriented analysis (OOA)
–
–
–
–
Understand the users’ needs
Needs are often not well defined
Can be difficult and time consuming
Frequently, several program revisions are necessary to
satisfy the user
An Object-Oriented Approach to Programming Logic and Design
17
Designing the System
• Designers using object-oriented design (OOD)
– Envision the objects needed
– Determine objects’ attributes and behaviors
– Decide the relationship between objects
• Describe how objects communicate with and react to each
other
• Expressed as verb phrases
• Examples of relationships: “Has a,” “is a,” “creates a”
• Class
– A general category that describes entities
– May be reused from other programs or modified
An Object-Oriented Approach to Programming Logic and Design
18
Writing and Testing Programs
• Writing a program involves several subtasks
–
–
–
–
Developing the program logic
Coding the program
Translating the program into machine language
Testing the program
An Object-Oriented Approach to Programming Logic and Design
19
Writing and Testing Programs (cont’d)
• Planning the logic
–
–
–
–
Heart of the programming process
Also referred to as developing an algorithm
Decide steps to include and their order
Planning tools
• Flowcharts and pseudocode
• Both use English-like code
– Language syntax not a concern
– Desk-checking
• Reviewing program logic on paper
An Object-Oriented Approach to Programming Logic and Design
20
Writing and Testing Programs (cont’d)
• Coding the program
– Writing program statements in a programming language
– Object-oriented languages
• C++, C#, Java, Visual Basic, SmallTalk, OO COBOL, and Simula
• Create objects and establish communication between them
• Language chosen determines syntax
– Coding usually less difficult than planning step
An Object-Oriented Approach to Programming Logic and Design
21
Writing and Testing Programs (cont’d)
• Use software to translate program into machine
language
– Many programming languages
– Computer knows only machine language (1s and 0s)
• Compilers or interpreters
– Translate English-like, high-level programming language
into low-level machine language
An Object-Oriented Approach to Programming Logic and Design
22
Writing and Testing Programs (cont’d)
• Syntax error
– Occurs when translator cannot translate the code
– Causes: misspellings, illegal grammar, non-existent words
– Programmer must correct code and recompile the program
before it can execute
Creating an executable program
Figure 1-1
An Object-Oriented Approach to Programming Logic and Design
23
Writing and Testing Programs (cont’d)
• Testing the program
– Program that is free of syntax errors is not necessarily free
of logical errors
• Must test for logical errors
– May require entering of sample data
• Select test data carefully
• Error discovery may require changes to program logic
An Object-Oriented Approach to Programming Logic and Design
24
Writing and Testing Programs (cont’d)
• After the program is written and tested
– Ready to be used
• Additional tasks associated with programs written for
organizations
– Preparing manuals
– Training users
– Converting existing data to new system’s format
• Conversion
– Actions an organization take to switch to a new program
– Can take months or years to accomplish
An Object-Oriented Approach to Programming Logic and Design
25
Writing and Testing Programs (cont’d)
• Maintenance
– Process of making required changes after program is put
into production
• Reasons why maintenance may be necessary
–
–
–
–
–
Fixing previously undiscovered errors
Updating values such as a tax rate
Changing format of input data
Input data may no longer be available
User wants additional functionality
An Object-Oriented Approach to Programming Logic and Design
26
Using Pseudocode and Flowcharts
• Pseudocode
– English-like representation of logical program steps
– Looks like programming language but is not
– Flexible planning tool
• Example
start
input myNumber
myAnswer = myNumber * 2
output myAnswer
stop
• Starting and ending statements often used
• Punctuation and syntax are not important
An Object-Oriented Approach to Programming Logic and Design
27
Drawing Flowcharts
• Flowchart
– Pictorial representation of logical program steps
– Helps programmer visualize how statements connect
– Uses geometric shapes connected with arrows (flowlines)
• Common flowchart symbols
–
–
–
–
–
Input symbols (parallelograms)
Processing symbols (rectangles)
Output symbols (parallelograms)
Terminal symbols (lozenges – flattened ovals)
Decision symbols (diamonds)
An Object-Oriented Approach to Programming Logic and Design
28
Drawing Flowcharts (cont’d)
Flowchart and pseudocode of a program
that doubles a number if it is less than 10
Figure 1-3
An Object-Oriented Approach to Programming Logic and Design
29
Drawing Flowcharts (cont’d)
• Software applications with flowcharting tools
–
–
–
–
Microsoft Word
Microsoft PowerPoint
Visio
Visual Logic
An Object-Oriented Approach to Programming Logic and Design
30
Understanding Program Comments
• Program comments
– Non-executing statements in a program added for
documentation
• Programmers write comments for themselves and
others who read program
• Minimally, comments should include author, date,
and purpose of the program
• C++, Java, and C# use two forward slashes
// This is a comment
An Object-Oriented Approach to Programming Logic and Design
31
Annotation symbol used for comments on a flowchart
Three-sided box
with dashed line
connects to the
step it explains
An Object-Oriented Approach to Programming Logic and Design
Figure 1-4
32
Understanding Programming
and User Environments
• Flowcharts created by hand or with software
• Pseudocode written by hand or with a word processor
– Plain text editor
– Text editor in an integrated development environment (IDE)
• Text editor
–
–
–
–
Program used to create simple text files
Similar to a word processor, but less robust
Example: Notepad in Microsoft Windows
Complete program does not require much disk space
An Object-Oriented Approach to Programming Logic and Design
33
Understanding Programming and
User Environments (cont’d)
• Integrated development environment (IDE)
– Software package that includes an editor, compiler, and
other programming tools
• Advantages of using IDE
–
–
–
–
Uses colors to display various language components
Highlights syntax errors visually
Employs automatic statement completion
Provides tools to step through a program to find errors
• Disadvantages of using IDE
– Requires much more storage space than plain text editor
An Object-Oriented Approach to Programming Logic and Design
34
Understanding User Environments
• A user might execute program in different
environments: command line, GUI
• The logical process is the same regardless of the
environment
Executing a number-doubling program in a
command-line environment
Figure 1-7
An Object-Oriented Approach to Programming Logic and Design
Executing a number-doubling
program in a GUI environment
Figure 1-8
35
Summary
• Hardware and software accomplish three major
operations: input, processing, and output
• For a program to work properly, you must develop
correct logic
• Logical errors are more difficult to locate than syntax
errors
• Developing a system involves analyzing the system,
designing it, and writing the programs
• Writing programs involves logic planning, coding the
program, translating into machine language, and testing
An Object-Oriented Approach to Programming Logic and Design
36
Summary (cont’d)
• Flowcharts and pseudocode are tools for planning a
program’s logic
• Pseudocode is an English-like representation of a
program’s logical steps
• Flowchart is a pictorial representation of a program
• Program comments document a program
• Plain text editor or an integrated development
environment (IDE) can be used to type a program
• A program’s data input can be at a command line or
a graphical user interface (GUI)
An Object-Oriented Approach to Programming Logic and Design
37
Download