Home / essay help / essay writing services / draw a structured flowchart or write pseudo code that describes the process of guessing a number

draw a structured flowchart or write pseudo code that describes the process of guessing a number

PROGRAMMING LOG IC

AND DES IGN

COMPREHENSIVE VERSION

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

PROGRAMMING LOGIC

AND DESIGN

COMPREHENSIVE VERSION

JOYCE FARRELL

Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States

SEV ENTH ED I T I ON

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest.

Programming Logic and Design,

Comprehensive version, Seventh Edition Joyce Farrell

Executive Editor: Marie Lee

Acquisitions Editor: Brandi Shailer

Senior Product Manager: Alyssa Pratt

Developmental Editor: Dan Seiter

Senior Content Project Manager: Catherine DiMassa

Associate Product Manager: Stephanie Lorenz

Associate Marketing Manager: Shanna Shelton

Art Director: Faith Brosnan

Text Designer: Shawn Girsberger

Cover Designer: Lisa Kuhn/Curio Press, LLC, www.curiopress.com

Image Credit: © Leigh Prather/Veer

Senior Print Buyer: Julio Esperas

Copy Editor: Michael Beckett

Proofreader: Kim Kosmatka

Indexer: Alexandra Nickerson

Compositor: Integra

© 2013 Course Technology, Cengage Learning.

ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored or used in any form or by any means— graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act—without the prior written permission of the publisher.

For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, www.cengage.com/support.

For permission to use material from this text or product, submit all requests online at cengage.com/permissions.

Further permissions questions can be emailed to permissionrequest@cengage.com.

Library of Congress Control Number: 2012930593

ISBN-13: 978-1-111-96975-2

Course Technology 20 Channel Center Street Boston, MA 02210 USA

Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: www.cengage.com/global

Cengage Learning products are represented in Canada by Nelson Education, Ltd.

To learn more about Course Technology, visit www.cengage.com/coursetechnology.

Purchase any of our products at your local college store or at our preferred online store: www.cengagebrain.com

Some of the product names and company names used in this book have been used for identification purposes only and may be trademarks or registered trademarks of their respective manufacturers and sellers.

Unless otherwise credited, all art © Cengage Learning, produced by Integra.

Course Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice.

Printed in the United States of America

1 2 3 4 5 6 7 16 15 14 13 12

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Brief Contents

Preface . . . . . . . . . . . . . . . . . . xviii

CHAPTER 1 An Overview of Computers and Programming . 1

CHAPTER 2 Elements of High-Qual ity Programs . . . . . . 37

CHAPTER 3 Understanding Structure . . . . . . . . . . . 83

CHAPTER 4 Making Decisions . . . . . . . . . . . . . 121

CHAPTER 5 Looping . . . . . . . . . . . . . . . . . 169

CHAPTER 6 Arrays . . . . . . . . . . . . . . . . . . 213

CHAPTER 7 Fi le Handl ing and Appl ications . . . . . . . 257

CHAPTER 8 Advanced Data Handl ing Concepts . . . . . 305

CHAPTER 9 Advanced Modularizat ion Techniques . . . . 355

CHAPTER 10 Object-Oriented Programming . . . . . . . 407

CHAPTER 11 More Object-Oriented Programming Concepts . . . . . . . . . . . . . . . . . 449

CHAPTER 12 Event-Driven GUI Programming, Mult ithreading, and Animation . . . . . . . 491

CHAPTER 13 System Modeling with the UML . . . . . . . 523

CHAPTER 14 Using Relat ional Databases . . . . . . . . 555

APPENDIX A Understanding Numbering Systems and Computer Codes . . . . . . . . . . . 601

APPENDIX B Flowchart Symbols . . . . . . . . . . . . 611

APPENDIX C Structures . . . . . . . . . . . . . . . . 612

APPENDIX D Solving Diff icult Structuring Problems . . . . 614

v

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

APPENDIX E Creating Print Charts . . . . . . . . . . . 624

APPENDIX F Two Variat ions on the Basic Structures—case and do-while . . . 626

Glossary . . . . . . . . . . . . . . . . . 633

Index . . . . . . . . . . . . . . . . . . . 653

B R I E F C ON T EN T S

vi

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Contents

Preface . . . . . . . . . . . . . . . . . . xviii

CHAPTER 1 An Overview of Computers and Programming . 1

Understanding Computer Systems . . . . . . . . . . . . . . . 2 Understanding Simple Program Logic . . . . . . . . . . . . . 5 Understanding the Program Development Cycle . . . . . . . . . 7 Understanding the Problem . . . . . . . . . . . . . . . . . 8 Planning the Logic . . . . . . . . . . . . . . . . . . . . . 9 Coding the Program . . . . . . . . . . . . . . . . . . . . 10 Using Software to Translate the Program into Machine Language . 10 Testing the Program . . . . . . . . . . . . . . . . . . . . 12 Putting the Program into Production . . . . . . . . . . . . . 13 Maintaining the Program . . . . . . . . . . . . . . . . . . 13

Using Pseudocode Statements and Flowchart Symbols . . . . . . 14 Writing Pseudocode . . . . . . . . . . . . . . . . . . . . 15 Drawing Flowcharts . . . . . . . . . . . . . . . . . . . . 16 Repeating Instructions . . . . . . . . . . . . . . . . . . . 17

Using a Sentinel Value to End a Program . . . . . . . . . . . . 19 Understanding Programming and User Environments . . . . . . . 22 Understanding Programming Environments . . . . . . . . . . 22 Understanding User Environments . . . . . . . . . . . . . . 24

Understanding the Evolution of Programming Models . . . . . . . 25 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . 27 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . 28 Review Questions . . . . . . . . . . . . . . . . . . . . . . 31 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 33 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . . 35 Game Zone . . . . . . . . . . . . . . . . . . . . . . . . 35 Up for Discussion . . . . . . . . . . . . . . . . . . . . . 36

vii

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

CHAPTER 2 Elements of High-Qual ity Programs . . . . . . 37

Declaring and Using Variables and Constants . . . . . . . . . . 38 Understanding Unnamed, Literal Constants and their

Data Types . . . . . . . . . . . . . . . . . . . . . . . 38 Working with Variables . . . . . . . . . . . . . . . . . . . 39 Naming Variables . . . . . . . . . . . . . . . . . . . . . 41 Assigning Values to Variables . . . . . . . . . . . . . . . . 42 Understanding the Data Types of Variables . . . . . . . . . . 43 Declaring Named Constants . . . . . . . . . . . . . . . . 44

Performing Arithmetic Operations . . . . . . . . . . . . . . . 45 Understanding the Advantages of Modularization . . . . . . . . . 48 Modularization Provides Abstraction . . . . . . . . . . . . . 49 Modularization Allows Multiple Programmers to

Work on a Problem . . . . . . . . . . . . . . . . . . . 50 Modularization Allows You to Reuse Work . . . . . . . . . . . 50

Modularizing a Program . . . . . . . . . . . . . . . . . . . 51 Declaring Variables and Constants within Modules . . . . . . . 55 Understanding the Most Common Configuration for Mainline Logic . 57

Creating Hierarchy Charts . . . . . . . . . . . . . . . . . . 61 Features of Good Program Design . . . . . . . . . . . . . . . 63 Using Program Comments . . . . . . . . . . . . . . . . . 64 Choosing Identifiers . . . . . . . . . . . . . . . . . . . . 66 Designing Clear Statements . . . . . . . . . . . . . . . . 68 Writing Clear Prompts and Echoing Input . . . . . . . . . . . 69 Maintaining Good Programming Habits . . . . . . . . . . . . 71

Chapter Summary . . . . . . . . . . . . . . . . . . . . . . 72 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . 73 Review Questions . . . . . . . . . . . . . . . . . . . . . . 76 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 79 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . . 81 Game Zone . . . . . . . . . . . . . . . . . . . . . . . . 82 Up for Discussion . . . . . . . . . . . . . . . . . . . . . 82

CHAPTER 3 Understanding Structure . . . . . . . . . . . 83

The Disadvantages of Unstructured Spaghetti Code . . . . . . . 84 Understanding the Three Basic Structures . . . . . . . . . . . 86

viii

C O N T E N T S

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Using a Priming Input to Structure a Program . . . . . . . . . . 95 Understanding the Reasons for Structure . . . . . . . . . . . 101 Recognizing Structure . . . . . . . . . . . . . . . . . . . 102 Structuring and Modularizing Unstructured Logic . . . . . . . . 105 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 110 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 111 Review Questions . . . . . . . . . . . . . . . . . . . . . 112 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 114 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 118 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 118 Up for Discussion . . . . . . . . . . . . . . . . . . . . 119

CHAPTER 4 Making Decisions . . . . . . . . . . . . . 121

Boolean Expressions and the Selection Structure . . . . . . . 122 Using Relational Comparison Operators . . . . . . . . . . . 126 Avoiding a Common Error with Relational Operators . . . . . 129

Understanding AND Logic . . . . . . . . . . . . . . . . . 129 Nesting AND Decisions for Efficiency . . . . . . . . . . . . 132 Using the AND Operator . . . . . . . . . . . . . . . . . 134 Avoiding Common Errors in an AND Selection . . . . . . . . 136

Understanding OR Logic . . . . . . . . . . . . . . . . . . 138 Writing OR Decisions for Efficiency . . . . . . . . . . . . . 140 Using the OR Operator . . . . . . . . . . . . . . . . . . 141 Avoiding Common Errors in an OR Selection . . . . . . . . 143

Making Selections within Ranges . . . . . . . . . . . . . . 148 Avoiding Common Errors When Using Range Checks . . . . . 150

Understanding Precedence When Combining AND and OR Operators . . . . . . . . . . . . . . . . . . . . 154

Chapter Summary . . . . . . . . . . . . . . . . . . . . . 157 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 158 Review Questions . . . . . . . . . . . . . . . . . . . . . 159 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 162 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 167 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 167 Up for Discussion . . . . . . . . . . . . . . . . . . . . 168

ix

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

CHAPTER 5 Looping . . . . . . . . . . . . . . . . . 169

Understanding the Advantages of Looping . . . . . . . . . . 170 Using a Loop Control Variable . . . . . . . . . . . . . . . . 171 Using a Definite Loop with a Counter . . . . . . . . . . . . 172 Using an Indefinite Loop with a Sentinel Value . . . . . . . . 173 Understanding the Loop in a Program’s Mainline Logic . . . . 175

Nested Loops . . . . . . . . . . . . . . . . . . . . . . 177 Avoiding Common Loop Mistakes . . . . . . . . . . . . . . 183 Mistake: Neglecting to Initialize the Loop Control Variable . . . 183 Mistake: Neglecting to Alter the Loop Control Variable . . . . 185 Mistake: Using the Wrong Comparison with the Loop Control

Variable . . . . . . . . . . . . . . . . . . . . . . . 186 Mistake: Including Statements Inside the Loop

that Belong Outside the Loop . . . . . . . . . . . . . . 187 Using a for Loop . . . . . . . . . . . . . . . . . . . . . 192 Common Loop Applications . . . . . . . . . . . . . . . . . 194 Using a Loop to Accumulate Totals . . . . . . . . . . . . 194 Using a Loop to Validate Data . . . . . . . . . . . . . . . 198 Limiting a Reprompting Loop . . . . . . . . . . . . . . . 200 Validating a Data Type . . . . . . . . . . . . . . . . . . 202 Validating Reasonableness and Consistency of Data . . . . . 203

Chapter Summary . . . . . . . . . . . . . . . . . . . . . 205 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 205 Review Questions . . . . . . . . . . . . . . . . . . . . . 206 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 209 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 211 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 211 Up for Discussion . . . . . . . . . . . . . . . . . . . . 212

CHAPTER 6 Arrays . . . . . . . . . . . . . . . . . . 213

Storing Data in Arrays . . . . . . . . . . . . . . . . . . . 214 How Arrays Occupy Computer Memory . . . . . . . . . . . 214

How an Array Can Replace Nested Decisions . . . . . . . . . 216 Using Constants with Arrays . . . . . . . . . . . . . . . . 224 Using a Constant as the Size of an Array . . . . . . . . . . 224 Using Constants as Array Element Values . . . . . . . . . . 225 Using a Constant as an Array Subscript . . . . . . . . . . 225

x

C ON T E N T S

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Searching an Array for an Exact Match . . . . . . . . . . . . 226 Using Parallel Arrays . . . . . . . . . . . . . . . . . . . . 230 Improving Search Efficiency . . . . . . . . . . . . . . . . 234

Searching an Array for a Range Match . . . . . . . . . . . . 237 Remaining within Array Bounds . . . . . . . . . . . . . . . 241 Using a for Loop to Process Arrays . . . . . . . . . . . . . 244 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 245 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 246 Review Questions . . . . . . . . . . . . . . . . . . . . . 246 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 249 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 253 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 253 Up for Discussion . . . . . . . . . . . . . . . . . . . . 255

CHAPTER 7 Fi le Handl ing and Applicat ions . . . . . . . 257

Understanding Computer Files . . . . . . . . . . . . . . . 258 Organizing Files . . . . . . . . . . . . . . . . . . . . . 259

Understanding the Data Hierarchy . . . . . . . . . . . . . . 260 Performing File Operations . . . . . . . . . . . . . . . . . 261 Declaring a File . . . . . . . . . . . . . . . . . . . . . 261 Opening a File . . . . . . . . . . . . . . . . . . . . . 262 Reading Data from a File . . . . . . . . . . . . . . . . . 262 Writing Data to a File . . . . . . . . . . . . . . . . . . 264 Closing a File . . . . . . . . . . . . . . . . . . . . . . 264 A Program that Performs File Operations . . . . . . . . . . 264

Understanding Sequential Files and Control Break Logic . . . . 267 Understanding Control Break Logic . . . . . . . . . . . . 268

Merging Sequential Files . . . . . . . . . . . . . . . . . . 273 Master and Transaction File Processing . . . . . . . . . . . 281 Random Access Files . . . . . . . . . . . . . . . . . . . 290 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 292 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 293 Review Questions . . . . . . . . . . . . . . . . . . . . . 295 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 299 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 302 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 302 Up for Discussion . . . . . . . . . . . . . . . . . . . . 303

xi

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

CHAPTER 8 Advanced Data Handl ing Concepts . . . . . 305

Understanding the Need for Sorting Data . . . . . . . . . . . 306 Using the Bubble Sort Algorithm . . . . . . . . . . . . . . . 307 Understanding Swapping Values . . . . . . . . . . . . . . 308 Understanding the Bubble Sort . . . . . . . . . . . . . . 309 Sorting a List of Variable Size . . . . . . . . . . . . . . . 318 Refining the Bubble Sort to Reduce Unnecessary

Comparisons . . . . . . . . . . . . . . . . . . . . . 322 Refining the Bubble Sort to Eliminate Unnecessary

Passes . . . . . . . . . . . . . . . . . . . . . . . 324 Sorting Multifield Records . . . . . . . . . . . . . . . . . 326 Sorting Data Stored in Parallel Arrays . . . . . . . . . . . 326 Sorting Records as a Whole . . . . . . . . . . . . . . . 328

Using the Insertion Sort Algorithm . . . . . . . . . . . . . . 329 Using Multidimensional Arrays . . . . . . . . . . . . . . . . 333 Using Indexed Files and Linked Lists . . . . . . . . . . . . . 340 Using Indexed Files . . . . . . . . . . . . . . . . . . . 340 Using Linked Lists . . . . . . . . . . . . . . . . . . . . 342

Chapter Summary . . . . . . . . . . . . . . . . . . . . . 345 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 346 Review Questions . . . . . . . . . . . . . . . . . . . . . 347 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 350 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 352 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 353 Up for Discussion . . . . . . . . . . . . . . . . . . . . 354

CHAPTER 9 Advanced Modularization Techniques . . . . 355

Using Methods with No Parameters . . . . . . . . . . . . . 356 Creating Methods that Require Parameters . . . . . . . . . . 358 Creating Methods that Require Multiple Parameters . . . . . 364

Creating Methods that Return a Value . . . . . . . . . . . . 366 Using an IPO Chart . . . . . . . . . . . . . . . . . . . 372

Passing an Array to a Method . . . . . . . . . . . . . . . . 373 Overloading Methods . . . . . . . . . . . . . . . . . . . 380 Avoiding Ambiguous Methods . . . . . . . . . . . . . . . 383

Using Predefined Methods . . . . . . . . . . . . . . . . . 386

xii

C O N T E N T S

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Method Design Issues: Implementation Hiding, Cohesion, and Coupling . . . . . . . . . . . . . . . . . . . . . . 388 Understanding Implementation Hiding . . . . . . . . . . . 388 Increasing Cohesion . . . . . . . . . . . . . . . . . . . 388 Reducing Coupling . . . . . . . . . . . . . . . . . . . 389

Understanding Recursion . . . . . . . . . . . . . . . . . . 390 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 395 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 396 Review Questions . . . . . . . . . . . . . . . . . . . . . 397 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 400 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 404 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 404 Up for Discussion . . . . . . . . . . . . . . . . . . . . 405

CHAPTER 10 Object-Oriented Programming . . . . . . . 407

Principles of Object-Oriented Programming . . . . . . . . . . 408 Classes and Objects . . . . . . . . . . . . . . . . . . . 408 Polymorphism . . . . . . . . . . . . . . . . . . . . . 412 Inheritance . . . . . . . . . . . . . . . . . . . . . . . 413 Encapsulation . . . . . . . . . . . . . . . . . . . . . . 414

Defining Classes and Creating Class Diagrams . . . . . . . . 415 Creating Class Diagrams . . . . . . . . . . . . . . . . . 417 The Set Methods . . . . . . . . . . . . . . . . . . . . 420 The Get Methods . . . . . . . . . . . . . . . . . . . . 421 Work Methods . . . . . . . . . . . . . . . . . . . . . 422

Understanding Public and Private Access . . . . . . . . . . . 424 Organizing Classes . . . . . . . . . . . . . . . . . . . . 428 Understanding Instance Methods . . . . . . . . . . . . . . 429 Understanding Static Methods . . . . . . . . . . . . . . . 434 Using Objects . . . . . . . . . . . . . . . . . . . . . . . 436 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 440 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 440 Review Questions . . . . . . . . . . . . . . . . . . . . . 442 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 445 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 447 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 447 Up for Discussion . . . . . . . . . . . . . . . . . . . . 447

xiii

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

CHAPTER 11 More Object-Oriented Programming Concepts 449

Understanding Constructors . . . . . . . . . . . . . . . . 450 Default Constructors . . . . . . . . . . . . . . . . . . . 450 Nondefault Constructors . . . . . . . . . . . . . . . . . 453 Overloading Methods and Constructors . . . . . . . . . . . 453

Understanding Destructors . . . . . . . . . . . . . . . . . 456 Understanding Composition . . . . . . . . . . . . . . . . . 458 Understanding Inheritance . . . . . . . . . . . . . . . . . 459 Understanding Inheritance Terminology . . . . . . . . . . . 462 Accessing Private Fields and Methods of a Parent Class . . . 465 Using Inheritance to Achieve Good Software Design . . . . . 470

An Example of Using Predefined Classes: Creating GUI Objects . 472 Understanding Exception Handling . . . . . . . . . . . . . . 473 Drawbacks to Traditional Error-Handling Techniques . . . . . 473 The Object-Oriented Exception-Handling Model . . . . . . . . 475 Using Built-in Exceptions and Creating Your Own Exceptions . . 477

Reviewing the Advantages of Object-Oriented Programming . . . 479 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 479 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 480 Review Questions . . . . . . . . . . . . . . . . . . . . . 482 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 485 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 489 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 489 Up for Discussion . . . . . . . . . . . . . . . . . . . . 490

CHAPTER 12 Event-Driven GUI Programming, Mult ithreading, and Animation . . . . . . . 491

Understanding Event-Driven Programming . . . . . . . . . . . 492 User-Initiated Actions and GUI Components . . . . . . . . . . 495 Designing Graphical User Interfaces . . . . . . . . . . . . . 498 The Interface Should Be Natural and Predictable . . . . . . . 498 The Interface Should Be Attractive, Easy to Read, and

Nondistracting . . . . . . . . . . . . . . . . . . . . 499 To Some Extent, It’s Helpful If the User Can Customize Your

Applications . . . . . . . . . . . . . . . . . . . . . 500 The Program Should Be Forgiving . . . . . . . . . . . . . 500 The GUI Is Only a Means to an End . . . . . . . . . . . . . 500

xiv

C O N T E N T S

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Developing an Event-Driven Application . . . . . . . . . . . . 501 Creating Storyboards . . . . . . . . . . . . . . . . . . 502 Defining the Storyboard Objects in an Object Dictionary . . . . 502 Defining Connections Between the User Screens . . . . . . . 503 Planning the Logic . . . . . . . . . . . . . . . . . . . . 504

Understanding Threads and Multithreading . . . . . . . . . . 509 Creating Animation . . . . . . . . . . . . . . . . . . . . 512 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 515 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 516 Review Questions . . . . . . . . . . . . . . . . . . . . . 517 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 520 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 520 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 521 Up for Discussion . . . . . . . . . . . . . . . . . . . . 522

CHAPTER 13 System Modeling with the UML . . . . . . . 523

Understanding System Modeling . . . . . . . . . . . . . . . 524 What Is the UML? . . . . . . . . . . . . . . . . . . . . . 525 Using UML Use Case Diagrams . . . . . . . . . . . . . . . 527 Using UML Class and Object Diagrams . . . . . . . . . . . . 533 Using Other UML Diagrams . . . . . . . . . . . . . . . . . 537 Sequence Diagrams . . . . . . . . . . . . . . . . . . . 537 Communication Diagrams . . . . . . . . . . . . . . . . 538 State Machine Diagrams . . . . . . . . . . . . . . . . . 539 Activity Diagrams . . . . . . . . . . . . . . . . . . . . 540 Component and Deployment Diagrams . . . . . . . . . . . 542 Profile Diagrams . . . . . . . . . . . . . . . . . . . . 544 Diagramming Exception Handling . . . . . . . . . . . . . 544

Deciding When to Use the UML and Which UML Diagrams to Use . 546 Chapter Summary . . . . . . . . . . . . . . . . . . . . . 547 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 548 Review Questions . . . . . . . . . . . . . . . . . . . . . 549 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 552 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 553 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 553 Up for Discussion . . . . . . . . . . . . . . . . . . . . 554

xv

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

CHAPTER 14 Using Relat ional Databases . . . . . . . . 555

Understanding Relational Database Fundamentals . . . . . . . 556 Creating Databases and Table Descriptions . . . . . . . . . . 558 Identifying Primary Keys . . . . . . . . . . . . . . . . . . 560 Understanding Database Structure Notation . . . . . . . . . . 563 Working with Records within Tables . . . . . . . . . . . . . 564 Creating Queries . . . . . . . . . . . . . . . . . . . . . 565 Understanding Relationships between Tables . . . . . . . . . 568 Understanding One-To-Many Relationships . . . . . . . . . . 569 Understanding Many-To-Many Relationships . . . . . . . . . 569 Understanding One-To-One Relationships . . . . . . . . . . 573

Recognizing Poor Table Design . . . . . . . . . . . . . . . 574 Understanding Anomalies, Normal Forms, and Normalization . . 576 First Normal Form . . . . . . . . . . . . . . . . . . . . 578 Second Normal Form . . . . . . . . . . . . . . . . . . 579 Third Normal Form . . . . . . . . . . . . . . . . . . . 582

Database Performance and Security Issues . . . . . . . . . . 585 Providing Data Integrity . . . . . . . . . . . . . . . . . 585 Recovering Lost Data . . . . . . . . . . . . . . . . . . 586 Avoiding Concurrent Update Problems . . . . . . . . . . . 586 Providing Authentication and Permissions . . . . . . . . . . 586 Providing Encryption . . . . . . . . . . . . . . . . . . . 587

Chapter Summary . . . . . . . . . . . . . . . . . . . . . 587 Key Terms . . . . . . . . . . . . . . . . . . . . . . . . 589 Review Questions . . . . . . . . . . . . . . . . . . . . . 591 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 594 Find the Bugs . . . . . . . . . . . . . . . . . . . . . . 598 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 598 Up for Discussion . . . . . . . . . . . . . . . . . . . . 598

APPENDIX A Understanding Numbering Systems and Computer Codes . . . . . . . . . . . . 601

APPENDIX B Flowchart Symbols . . . . . . . . . . . . . 611

APPENDIX C Structures . . . . . . . . . . . . . . . . . 612

xvi

C O N T E N T S

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

APPENDIX D Solving Diff icult Structuring Problems . . . . 614

APPENDIX E Creating Print Charts . . . . . . . . . . . . 624

APPENDIX F Two Variat ions on the Basic Structures—case and do-while . . . . . . . . . . . . . . . 626

Glossary . . . . . . . . . . . . . . . . . . 633

Index . . . . . . . . . . . . . . . . . . . . 653

xvii

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Preface

Programming Logic and Design, Comprehensive, Seventh 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.

Organization and Coverage Programming Logic and Design, Comprehensive, Seventh Edition introduces students to programming concepts and enforces good style and logical thinking. General programming concepts are introduced in Chapter 1. Chapter 2 discusses using data and introduces two important concepts: modularization and creating high-quality programs. It is important to emphasize these topics early so that students start thinking in a modular way and concentrate on making their programs efficient, robust, easy to read, and easy to maintain.

Chapter 3 covers the key concepts of structure, including what structure is, how to recognize it, and most importantly, the advantages to writing structured programs. This chapter’s content is unique among programming texts. The early overview of structure presented here gives students a solid foundation in thinking in a structured way.

Chapters 4, 5, and 6 explore the intricacies of decision making, looping, and array manipulation. Chapter 7 provides details of file handling so students can create programs that process a significant amount of data.

In Chapters 8 and 9, students learn more advanced techniques in array manipulation and modularization. Chapters 10 and 11 provide a thorough yet accessible introduction to concepts and terminology used in object-oriented programming. Students learn about classes, objects, instance and static class members, constructors, destructors, inheritance, and the advantages of object-oriented thinking.

xviii

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Chapter 12 explores additional object-oriented programming issues: event-driven GUI programming, multithreading, and animation. Chapter 13 discusses system design issues and details the features of the Unified Modeling Language. Chapter 14 is a thorough introduction to important database concepts that business programmers should understand.

The first three appendices give students summaries of numbering systems, flowchart symbols, and structures. Additional appendices allow students to gain extra experience with structuring large unstructured programs, creating print charts, and understanding posttest loops and case structures.

Programming Logic and Design combines text explanation with flowcharts and pseudocode examples to provide students with alternative means of expressing structured logic. Numerous detailed, full-program exercises at the end of each chapter illustrate the concepts explained within the chapter, and reinforce understanding and retention of the material presented.

Programming Logic and Design distinguishes itself from other programming logic books in the following ways:

l It is written and designed to be non-language specific. The logic used in this book can be applied to any programming language.

l The examples are everyday business examples; no special knowledge of mathematics, accounting, or other disciplines is assumed.

l The concept of structure is covered earlier than in many other texts. Students are exposed to structure naturally, so they will automatically create properly designed programs.

l Text explanation is interspersed with both flowcharts and pseudocode so students can become comfortable with these logic development tools and understand their interrelationship. Screen shots of running programs also are included, providing students with a clear and concrete image of the programs’ execution.

l Complex programs are built through the use of complete business examples. Students see how an application is constructed from start to finish instead of studying only segments of programs.

Organization and Coverage

xix

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Features This text focuses on helping students become better programmers and understand the big picture in program development through a variety of key features. In addition to chapter Objectives, Summaries, and Key Terms, these useful features will help students regardless of their learning style.

The use of flowcharts is excellent. This is a must-have book for learning programming logic before tackling the various languages.

—Lori Selby, University of Arkansas at Monticello

FLOWCHARTS, figures, and illustrations provide

learning experience. the reader with a visual

VIDEO LESSONS help explain important chapter concepts. Videos are part of the text’s enhanced CourseMate site.

NOTES provide additional information— for example, another location in the book that expands on a topic, or a common error to watch out for.

xx

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

TWO TRUTHS & A LIE mini quizzes appear after each chapter section, with answers provided. The quiz contains three statements based on the preceding section of text—two statements are true and one is false. Answers give immediate feedback without “giving away” answers to the multiple-choice questions and programming problems later in the chapter. Students also have the option to take these quizzes electronically through the enhanced CourseMate site.

THE DON’T DO IT ICON illustrates how NOT to do something—for example, having a dead code path in a program. This icon provides a visual jolt to the student,

are NOT to be emulated and making students more careful to recognize problems in existing code.

xxi

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Assessment

The material is very well written, clearly presented, and up to date. All explanations are very solid, and Farrell’s language is clean, cogent, and easy to follow.

—Judy Woodruff, Indiana University-Purdue University Indianapolis

EXERCISES provide opportunities to practice concepts. These exercises

to explore logical programming concepts. Each exercise can

pseudocode, or both. In addition, instructors can assign the exercises as programming problems to be coded and executed in a particular programming language.

REVIEW QUESTIONS test student comprehension of the major ideas and techniques presented. Twenty questions follow each chapter.

xxii

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

DEBUGGING EXERCISES are included with each chapter because examining programs critically and closely is a crucial programming skill. Students can download these exercises at www.cengagebrain.com and through the CourseMate available for this text. These files are also available to instructors through the Instructor Resources CD and login.cengage.com.

GAME ZONE EXERCISES are included at the end of each chapter. Students can create games as an additional entertaining way to understand key programming concepts.

ESSAY QUESTIONS present personal and ethical issues that programmers must consider. These questions can be used for written assignments or as a starting point for classroom discussion.

xxiii

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Other Features of the Text This edition of the text includes many features to help students become better programmers and understand the big picture in program development. In this edition, all explanations have been carefully reviewed to provide the clearest possible instruction. Material that previously was included in margin notes has most frequently been incorporated into the main text, giving the pages a more streamlined appearance. All the chapters in this edition contain new programming exercises. All Sixth Edition exercises that have been replaced are available on the Instructor Resources CD and through login. cengage.com so instructors can use them as additional assigned exercises or as topics for class discussions.

l Clear explanations. The language and explanations in this book have been refined over seven editions, providing the clearest possible explanations of difficult concepts.

l Emphasis on structure. More than its competitors, this book emphasizes structure. Chapter 3 provides an early picture of the major concepts of structured programming, giving students an overview of the principles before they are required to consider program details.

l Emphasis on modularity. From the second chapter, students are encouraged to write code in concise, easily manageable, and reusable modules. Instructors have found that modularization should be encouraged early to instill good habits and a clearer understanding of structure. This edition uses modularization early, using global variables instead of local passed and returned values, and saves parameter passing for later when the student has become more adept.

l Methods as black boxes. The use of methods is consistent with the languages in which the student is likely to have first programming experiences. In particular, this book emphasizes using methods as black boxes, declaring all variables and constants as local to methods, and passing arguments to and receiving returned values from methods as needed.

l Objectives. Each chapter begins with a list of objectives so the student knows the topics that will be presented in the chapter. In addition to providing a quick reference to topics covered, this feature provides a useful study aid.

l Pseudocode. This book includes numerous examples of pseudocode, which illustrate correct usage of the programming logic and design concepts being taught.

l Chapter summaries. Following each chapter is a summary that recaps the programming concepts and techniques covered in the chapter. This feature provides a concise means for students to review and check their understanding of the main points in each chapter.

l Key terms. Each chapter lists key terms and their definitions; the list appears in the order the terms are encountered in the chapter. Along with the chapter summary, the

xxiv

P R E F A C E Other Features of the Text

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

list of key terms provides a snapshot overview of a chapter’s main ideas. A glossary at the end of the book lists all the key terms in alphabetical order, along with working definitions.

CourseMate The more you study, the better the results. Make the most of your study time by accessing everything you need to succeed in one place. Read your textbook, review flashcards, watch videos, and take practice quizzes online. CourseMate goes beyond the book to deliver what you need! Learn more at www.cengage.com/coursemate.

The Programming Logic and Design CourseMate includes:

l Video Lessons. Designed and narrated by the author, videos in each chapter explain and enrich important concepts.

l Two Truths & A Lie and Debugging Exercises. Complete popular exercises from the text online!

l An interactive eBook with highlighting and note-taking, flashcards, quizzing, study games, and more!

Instructors may add CourseMate to the textbook package, or students may purchase CourseMate directly at www.cengagebrain.com.

Instructor Resources The following teaching tools are available to the instructor on a single CD-ROM. Many are also available for download through our Instructor Companion Site at login.cengage.com.

l Electronic Instructor’s Manual. The Instructor’s Manual follows the text chapter by chapter to assist in planning and organizing an effective, engaging course. The manual includes learning objectives, chapter overviews, lecture notes, ideas for classroom activities, and abundant additional resources. A sample course syllabus is also available.

l PowerPoint Presentations. This text provides PowerPoint slides to accompany each chapter. Slides are included to guide classroom presentation, to make available to students for chapter review, or to print as classroom handouts. Instructors may customize the slides, which include the complete figure files from the text, to best suit their courses.

l Solutions. Solutions to review questions and exercises are provided to assist with grading.

xxv

Instructor Resources

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

l ExamView�. This textbook is accompanied by ExamView, a powerful testing software package that allows instructors to create and administer printed, LAN-based, and Internet exams. ExamView includes hundreds of questions that correspond to the text, enabling students to generate detailed study guides that include page references for further review. The computer-based and Internet testing components allow students to take exams at their computers, and the components save the instructor time by grading each exam automatically. These test banks are also available in Blackboard and Angel compatible formats.

Additional Offerings You have the option to bundle software with your text. Please contact your Cengage Learning sales representative for more information.

l PAL Guides. Together with Programming Logic and Design, these brief books, or PAL guides, provide an excellent opportunity to learn the fundamentals of programming while gaining exposure to a programming language. Readers will discover how real code behaves within the context of the traditionally language-independent logic and design course. PAL guides are available for C++, Java, and Visual Basic; please contact your sales rep for more information on how to add the PAL guides to your course.

l Microsoft� Office Visio� Professional 2010, 60-day version. Visio 2010 is a diagramming program that allows users to create flowcharts and diagrams easily while working through the text, enabling them to visualize concepts and learn more effectively.

l Visual LogicTM software. Visual Logic is a simple but powerful tool for teaching programming logic and design without traditional high-level programming language syntax. Visual Logic uses flowcharts to explain the essential programming concepts discussed in this book, including variables, input, assignment, output, conditions, loops, procedures, graphics, arrays, and files. Visual Logic also interprets and executes flowcharts, providing students with immediate and accurate feedback. Visual Logic combines the power of a high-level language with the ease and simplicity of flowcharts.

Acknowledgments I would like to thank all of the people who helped to make this book a reality, especially Dan Seiter, Development Editor. After seven editions, Dan still finds ways to improve my explanations so that we can create a book of the highest possible quality. Thanks also to Alyssa Pratt, Senior Product Manager; Brandi Shailer, Acquisitions Editor; Catherine DiMassa, Senior Content Project Manager; and Green Pen QA, Technical Editors. I am grateful to be able to work with so many fine people who are dedicated to producing quality instructional materials.

xxvi

P R E F A C E Acknowledgments

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

I am indebted to the many reviewers who provided helpful and insightful comments during the development of this book, including Linda Cohen, Forsyth Tech; Andrew Hurd, Hudson Valley Community College; George Reynolds, Strayer University; Lori Selby, University of Arkansas at Monticello; and Judy Woodruff, Indiana University–Purdue University Indianapolis.

Thanks, too, to my husband, Geoff, and our daughters, Andrea and Audrey, for their support. This book, as were all its previous editions, is dedicated to them.

–Joyce Farrell

xxvii

Acknowledgments

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

CHAPTER 1 An Overview of Computers and Programming

In this chapter, you will learn about:

Computer systems

Simple program logic

The steps involved in the program development cycle

Pseudocode statements and flowchart symbols

Using a sentinel value to end a program

Programming and user environments

The evolution of programming models

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

Understanding Computer Systems A computer system is a combination of all the components required to process and store data using a computer. Every computer system is composed of multiple pieces of hardware and software.

l Hardware is the equipment, or the physical devices, associated with a computer. For example, keyboards, mice, speakers, and printers are all hardware. The devices are manufactured differently for large mainframe computers, laptops, and even smaller computers that are embedded into products such as cars and thermostats, but the types of operations performed by different-sized computers are very similar. When you think of a computer, you often think of its physical components first, but for a computer to be useful, it needs more than devices; a computer needs to be given instructions. Just as your stereo equipment does not do much until you provide music, computer hardware needs instructions that control how and when data items are input, how they are processed, and the form in which they are output or stored.

l Software is computer instructions that tell the hardware what to do. Software is programs, which are instruction sets written by programmers. You can buy prewritten programs that are stored on a disk or that you download from the Web. For example, businesses use word-processing and accounting programs, and casual computer users enjoy programs that play music and games. Alternatively, you can write your own programs. When you write software instructions, you are programming. This book focuses on the programming process.

Software can be classified into two broad types:

l Application software comprises all the programs you apply to a task, such as word- processing programs, spreadsheets, payroll and inventory programs, and even games.

l System software comprises the programs that you use to manage your computer, including operating systems such as Windows, Linux, or UNIX.

This book focuses on the logic used to write application software programs, although many of the concepts apply to both types of software.

Together, computer hardware and software accomplish three major operations in most programs:

l Input—Data items enter the computer system and are placed in memory, where they can be processed. Hardware devices that perform input operations include keyboards and mice. Data items include all the text, numbers, and other raw material that are entered into and processed by a computer. In business, many of the data items used are facts and figures about such entities as products, customers, and personnel. However, data can also include items such as images, sounds, and a user’s mouse movements.

l Processing—Processing data items may involve organizing or sorting them, checking them for accuracy, or performing calculations with them. The hardware component that performs these types of tasks is the central processing unit, or CPU.

2

C H A P T E R 1 An Overview of Computers and Programming

Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

l Output—After data items have been processed, the resulting information usually is sent to a printer, monitor, or some other output device so people can view, interpret, and use the results. Programming professionals often use the term data for input items, but use the term information for data that has been processed and output. Sometimes you place output on storage devices, such as disks or flash media. People cannot read data directly from these storage devices, but the devices hold information for later retrieval. When you send output to a storage device, sometimes it is used later as input for another program.

You write computer instructions in a computer programming language such as Visual Basic, C#, C++, or Java. Just as some people speak English and others speak Japanese, programmers write programs in different languages. Some programmers work exclusively in one language, whereas others know several and use the one that is best suited to the task at hand.

The instructions you write using a programming language are called program code; when you write instructions, you are coding the program.

Every programming language has rules governing its word usage and punctuation. These rules are called the language’s syntax. Mistakes in a language’s usage are syntax errors. If you ask, “How the geet too store do I?” in English, most people can figure out what you probably mean, even though you have not used proper English syntax—you have mixed up the word order, misspelled a word, and used an incorrect word. However, computers are not nearly as smart as most people; in this case, you might as well have asked the computer, “Xpu mxv ort dod nmcad bf B?” Unless the syntax is perfect, the computer cannot interpret the programming language instruction at all.

When you write a program, you usually type its instructions using a keyboard. When you type program instructions, they are stored in computer memory, which is a computer’s temporary, internal storage. Random access memory, or RAM, is a form of internal, volatile memory. Programs that are currently running and data items that are currently being used are stored in RAM for quick access. Internal storage is volatile—its contents are lost when the computer is turned off or loses power. Usually, you want to be able to retrieve and perhaps modify the stored instructions later, so you also store them on a permanent storage device, such as a disk. Permanent storage devices are nonvolatile—that is, their contents are persistent and are retained even when power is lost. If you have had a power loss while working on a computer, but were able to recover your work when power was restored, it’s not because the work was still in RAM. Your system has been configured to automatically save your work at regular intervals on a nonvolatile storage device.

About eduhawks

Check Also

for what value of r would the potential difference across each of the bulbs be 2.4 v ?

Two bulbs are connected in parallel across a source of emf EMF = 10.0V with …