Professional Documents
Culture Documents
PROGRAMMING LOGIC
AND DESIGN
COMPREHENSIVE
JOYCE FARRELL
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Programming Logic and Design, © 2018 Cengage Learning®
Comprehensive
ALL RIGHTS RESERVED. No part of this work covered by the
Ninth Edition
copyright herein may be reproduced or distributed in any form
Joyce Farrell
or by any means, except as permitted by U.S. copyright law,
Senior Product Director: without the prior written permission of the copyright owner.
Kathleen McMahon
For product information and technology assistance, contact us at
Product Team Leader: Kristin McNary
Cengage Learning Customer & Sales Support, 1-800-354-9706
Associate Product Manager: Kate Mason
For permission to use material from this text or product,
Senior Content Developer: Alyssa Pratt submit all requests online at www.cengage.com/permissions
Senior Content Project Manager: Further permissions questions can be emailed to
permissionrequest@cengage.com
Jennifer Feltri-George
Manufacturing Planner: Julio Esperas
Art Director: Diana Graham Library of Congress Control Number: 2016959742
Production Service/Composition: ISBN: 978-1-337-10207-0
SPi Global
Cover Photo: Cengage Learning
Colormos/Photodisc/Getty Images 20 Channel Center Street
Boston, MA 02210
USA
Pref ace ���������� ����� � ����� ��� � ����� ��� ��� ������ � ����� xvi
Pref ace ���������� ����� � ����� ��� � ����� ��� ��� ������ � ����� xvi
CHAPTER 3 U n der s t an din g Structure ��� �� ��� � ��� �� ��� ��� � �� ��� � 87
The Disadvantages of Unstructured Spaghetti Code��������������88
Understanding the Three Basic Structures ��������������������������90
The Sequence Structure ������������������������������������������������90
The Selection Structure��������������������������������������������������91
The Loop Structure ������������������������������������������������������92
Combining Structures ���������������������������������������������������93
Using a Priming Input to Structure a Program�����������������������99
Understanding the Reasons for Structure��������������������������� 106
Recognizing Structure���������������������������������������������������� 107
Structuring and Modularizing Unstructured Logic ��������������� 110
Chapter Summary ��������������������������������������������������������� 115
Key Terms��������������������������������������������������������������������� 115
Exercises ��������������������������������������������������������������������� 117
CONTENTS
CHAPTER 4 M ak in g Decis ions ��� � �� ��� ���� �� ��� � ��� �� ��� ��� � �� �� 124
The Selection Structure�������������������������������������������������� 125
Using Relational Comparison Operators ���������������������������� 129
Avoiding a Common Error with Relational Operators��������� 133
Understanding AND Logic������������������������������������������������ 134 ix
Nesting AND Decisions for Efficiency ���������������������������� 137
Using the AND Operator ���������������������������������������������� 139
Avoiding Common Errors in an AND Selection������������������ 141
Understanding OR Logic ������������������������������������������������� 143
Writing OR Selections for Efficiency ������������������������������ 145
Using the OR Operator ������������������������������������������������ 147
Avoiding Common Errors in an OR Selection������������������� 147
Understanding NOT Logic������������������������������������������������ 153
Avoiding a Common Error in a NOT Expression ��������������� 154
Making Selections within Ranges ������������������������������������� 155
Avoiding Common Errors When Using Range Checks ������� 157
Understanding Precedence When Combining
AND and OR Operators��������������������������������������������������� 160
Understanding the case Structure ���������������������������������� 163
Chapter Summary ��������������������������������������������������������� 165
Key Terms��������������������������������������������������������������������� 166
Exercises ��������������������������������������������������������������������� 167
CHAPTER 5 Lo o pin g � ������ ����� ��� � �� ��� ���� �� ��� � ��� �� ��� ��� � �� �� 176
Appreciating the Advantages of Looping ��������������������������� 177
Using a Loop Control Variable������������������������������������������ 179
Using a Definite Loop with a Counter������������������������������ 179
Using an Indefinite Loop with a Sentinel Value ���������������� 181
Understanding the Loop in a Program’s Mainline Logic������ 183
Nested Loops ��������������������������������������������������������������� 185
Avoiding Common Loop Mistakes ������������������������������������ 190
Mistake: Failing to Initialize the Loop Control Variable ������ 190
Mistake: Neglecting to Alter the Loop Control Variable������ 191
Mistake: Using the Wrong Type of Comparison When
Testing the Loop Control Variable ������������������������������� 192
Mistake: Including Statements Inside the Loop Body
that Belong Outside the Loop ������������������������������������� 194
Using a for Loop ���������������������������������������������������������� 199
Using a Posttest Loop ��������������������������������������������������� 201
Recognizing the Characteristics Shared
by Structured Loops ���������������������������������������������������� 203
CONTENTS
CHAPTER 6 Ar r ays ��� ������ ����� ��� � �� ��� ���� �� ��� � ��� �� ��� ��� � �� �� 227
Storing Data in Arrays���������������������������������������������������� 228
How Arrays Occupy Computer Memory��������������������������� 228
How an Array Can Replace Nested Decisions��������������������� 231
Using Constants with Arrays ������������������������������������������� 238
Using a Constant as the Size of an Array������������������������ 238
Using Constants as Array Element Values ���������������������� 239
Using a Constant as an Array Subscript ������������������������� 239
Searching an Array for an Exact Match������������������������������ 240
Using Parallel Arrays������������������������������������������������������ 244
Improving Search Efficiency������������������������������������������ 248
Searching an Array for a Range Match������������������������������ 250
Remaining within Array Bounds���������������������������������������� 255
Understanding Array Size��������������������������������������������� 255
Understanding Subscript Bounds ���������������������������������� 255
Using a for Loop to Process an Array������������������������������ 258
Chapter Summary ��������������������������������������������������������� 260
Key Terms��������������������������������������������������������������������� 261
Exercises ��������������������������������������������������������������������� 261
CHAPTER 7 F ile Han dlin g and Appl i cati ons��� ��� �� ��� ��� � �� �� 272
Understanding Computer Files ���������������������������������������� 273
Organizing Files ��������������������������������������������������������� 274
Understanding the Data Hierarchy������������������������������������ 275
Performing File Operations ��������������������������������������������� 277
Declaring a File Identifier��������������������������������������������� 277
Opening a File������������������������������������������������������������ 278
Reading Data from a File and Processing It��������������������� 278
Writing Data to a File��������������������������������������������������� 281
Closing a File������������������������������������������������������������� 281
CONTENTS
CHAPTER 9 Advan ced M o dul ari z ati on Techni ques ����� � �� �� 366
The Parts of a Method ��������������������������������������������������� 367
Using Methods with no Parameters ���������������������������������� 368
Creating Methods that Require Parameters������������������������ 371
Creating Methods that Require Multiple Parameters ��������� 377
Creating Methods that Return a Value������������������������������� 379
Using an IPO Chart������������������������������������������������������ 384
Passing an Array to a Method������������������������������������������ 386
Overloading Methods������������������������������������������������������ 394
Avoiding Ambiguous Methods ��������������������������������������� 397
Using Predefined Methods ���������������������������������������������� 400
Method Design Issues: Implementation Hiding, Cohesion,
and Coupling��������������������������������������������������������������� 402
Understanding Implementation Hiding ���������������������������� 402
Increasing Cohesion ��������������������������������������������������� 403
Reducing Coupling������������������������������������������������������ 404
Understanding Recursion������������������������������������������������ 405
CONTENTS
CHAPTER 10 Object -Or ien t ed Programmi ng ����� ��� ��� ��� ��� �� 420
xii
Principles of Object-Oriented Programming������������������������ 421
Classes and Objects ��������������������������������������������������� 421
Polymorphism ������������������������������������������������������������ 424
Inheritance ���������������������������������������������������������������� 426
Encapsulation ������������������������������������������������������������ 426
Defining Classes and Creating Class Diagrams ������������������ 428
Creating Class Diagrams ��������������������������������������������� 430
The Set Methods��������������������������������������������������������� 433
The Get Methods��������������������������������������������������������� 434
Work Methods������������������������������������������������������������ 435
Understanding Public and Private Access��������������������������� 437
Organizing Classes��������������������������������������������������������� 440
Understanding Instance Methods ������������������������������������� 441
Understanding Static Methods������������������������������������������ 447
Using Objects ��������������������������������������������������������������� 448
Passing an Object to a Method ������������������������������������� 449
Returning an Object from a Method ������������������������������� 450
Using Arrays of Objects ���������������������������������������������� 453
Chapter Summary ��������������������������������������������������������� 455
Key Terms��������������������������������������������������������������������� 456
Exercises ��������������������������������������������������������������������� 458
Programming Logic and Design, Comprehensive, Ninth Edition, provides the beginning
programmer with a guide to developing structured program logic. This textbook assumes
no programming language experience. The writing is nontechnical and emphasizes
good programming practices. The examples are business examples; they do not assume
mathematical background beyond high school business math.
Additionally, the examples illustrate one or two major points; they do not contain so
many features that students become lost following irrelevant and extraneous details. The
examples in this book have been created to provide students with a sound background in
logic, no matter what programming languages they eventually use to write programs. This
book can be used in a stand-alone logic course that students take as a prerequisite to a
programming course, or as a companion book to an introductory programming text using
any programming language.
Don’t Do It
FLOWCHARTS, figures, This logic is structured,
but flawed. When the user
and illustrations provide start
inputs the eof value, it will
tested. Instead, a result is calculated and displayed one last time before the loop-controlling
test is made again. If the program was written to recognize eof when originalNumber is 0,
then an extraneous answer of 0 will be displayed before the program ends. Depending on
the language you are using and on the type of input being used, the results might be worse:
The program might terminate by displaying an error message or the value output might
be indecipherable garbage. In any case, this last output is superfluous—no value should be
doubled and output after the eof condition is encountered.
As a general rule, a program-ending test should always come immediately after an input
statement because that’s the earliest point at which it can be evaluated. Therefore, the best
solution to the number-doubling problem remains the one shown in Figure 3-16—the
structured solution containing the priming input statement.
Computer memory consists of millions of numbered locations where data can be stored. The memory
location of myNumber has a specific numeric address, but when you write programs, you seldom need
to be concerned with the value of the memory address; instead, you use the easy-to-remember name
you created. Computer programmers often refer to memory addresses using hexadecimal notation,
or base 16. Using this system, they might use a value like 42FF01A to refer to a memory address.
Despite the use of letters, such an address is still a number. Appendix A contains information about the
hexadecimal numbering system.
1. NOTES provide
A program with syntax errors can execute but might produce incorrect
results.
additional information—
2. Although the syntax of programming languages differs, the same program
logic can be expressed in different languages. for example, another
3. Most simple computer programs include steps that location
perform input,in the book that
processing, and output.
expands on a topic, or a
common error to avoid.
a program with no syntax errors can execute, but might produce incorrect results.
The false statement is #1. A program with syntax errors cannot execute;
Exercises
Review Questions
32
1. Computer programs also are known as ____________.
a. data c. software
b. hardware d. information
2. The major computer operations include ____________.
a. input, processing, and output
b. hardware and software
c. sequence and looping
d. CHAPTER 4
spreadsheets, word processing, and data communications Making Decisions
3. Visual Basic, C++, and Java are all examples of computer ____________.
a. operating systems c. hardware
b. programming languages Programming
d. machine languages Exercises
4. A programming language’s rules are its ____________. 1. Assume that the following variables contain the values shown:
a. syntax c.170format numberBig = 100 wordBig = "Constitution"
b. logic d. options numberMedium = 10 wordMedium = "Dance"
numberSmall = 1 wordSmall = "Toy”
5. The most important task of a compiler or interpreter is to ____________.
For each of the following Boolean expressions, decide whether the statement is
a. create the rules for a programming language true, false, or illegal.
b. translate English statements into a language such as Java a. numberBig > numberSmall
c. translate programming language statements into machine languageb. numberBig < numberMedium
d. execute machine language programs to perform useful tasks c. numberMedium = numberSmall
6. Which of the following is temporary, internal storage? d. numberBig = wordBig
a. CPU c. keyboard e. numberBig = "Big"
3. Design a flowchart or pseudocode for a program that accepts three numbers from
a user and displays a message if the sum of any two numbers equals the third.
PERFORMING MAINTENANCE
Exercises
exercises ask students to modify
working logic based on new
c. Modify the softball program so that it also computes a gross production
requested specifications.
average (GPA) for each player.This
A GPA is calculated by multiplying a player’s
on-base percentage by 1.8, then adding the player’s slugging percentage, and xxi
activity then
mirrors real-world
dividing by four. tasks
that
10. students arechart
Draw the hierarchy likelyand to
design the logic for a program for Arnie’s 85
encounter in their first programming
Appliances. Design a program that prompts the user for a refrigerator model
name and the interior height, width, and depth in inches. Calculate the
jobs.refrigerator capacity in cubic feet by first multiplying the height, width, and
depth to get cubic inches, and then dividing by 1728 (the number of cubic inches
in a cubic foot). The program accepts model names continuously until “XXX” is
entered. Use named constants where appropriate. Also use modules, including
one that displays End of job after the sentinel is entered for the model name.
Performing Maintenance
1. A file named MAINTENANCE02-01.txt is included with your downloadable
student files. Assume that this program is a working program in your
organization and that it needs modifications as described in the comments (lines
that begin with two slashes) at the beginning of the file. Your job is to alter the
program to meet the new specifications.
2. Your downloadable files for Chapter 2 include a file named DEBUG02-04.jpg that
contains a flowchart with syntax and/or logical errors. Examine the flowchart,
and then find and correct all the bugs.
Game Zone
1. For games to hold your interest, they almost always include some random,
unpredictable behavior. For example, a game in which you shoot asteroids loses
some of its fun if the asteroids follow the same, predictable path each time you
play. Therefore, generating random values is a key component in creating most
Language: English
TAKEN DURING
T R A V E L S I N A F R I C A.
t
Drawn by J. G. Wilkinson Esqre. from a Sketch taken Drawn on Stone & Printed by P. Gauci, 9, North Cres .
by the late John Davidson. re
Bedford Sq .
VIEW OF WADNOON.
From the roof of Sheik Beyrook’s House
NOTES
TAKEN DURING
T R A V E L S I N A F R I C A,
BY THE LATE
LONDON:
P R I N T E D B Y J . L . C O X A N D S O N S , 7 5 , G R E AT Q U E E N S T R E E T,
LINCOLN’S-INN FIELDS.
1839.
A D V E R T I S E M E N T.
Page
A P P E N D I X.
Extracts from the Correspondence of Mr. Davidson, during his Residence
in Morocco; with an Account of his further progress in the Desert 181
Account of Mr. Davidson’s Death, extracted from the Journal of the Royal
Geographical Society 202
History of Abou, extracted from the Journal of the Royal Geographical
Society 208
Mr. Hay’s Letter, referred to in the Notes, and Reply thereto 215
[Illustrations]
VIEW OF WADNOON.
Style of Buildings of Wadnoon.
THE RIVER DRAHA.
N O T E S
TAKEN DURING
T R A V E L S I N A F R I C A.