Buy. Rent. Access. Access student data fi les and other study tools on cengagebrain.com. For detailed instructions visit http://solutions.cengage.com/ctdownloads/ Store your Data Files on a USB drive for maximum effi ciency in organizing and working with the fi les. Macintosh users should use a program to expand WinZip or PKZip archives. Ask your instructor or lab coordinator for assistance. Copyright 2015 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. EIGHTH EDITION PROGRAMMING LOGIC AND DESIGN COMPREHENSIVE VERSION JOYCE FARRELL Australia Brazil Japan Korea Mexico Singapore Spain UnitedKingdom UnitedStates Copyright 2015 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. Copyright 2015 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. ProgrammingLogicandDesign, ©2015CengageLearning. Comprehensiveversion, WCN: 02-200-203 EighthEdition ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyrightherein JoyceFarrell maybereproduced,transmitted,storedorusedinanyformorbyanymeans— SeniorProductManager:JimGish graphic,electronic,ormechanical,includingbutnotlimitedtophotocopying, recording,scanning,digitizing,taping,Webdistribution,informationnetworks, SeniorContentDeveloper:AlyssaPratt orinformationstorageandretrievalsystems,exceptaspermittedunderSection DevelopmentEditor:DanSeiter 107or108ofthe1976UnitedStatesCopyrightAct—withoutthepriorwritten permissionofthepublisher. ContentProjectManager: JenniferFeltri-George Forproductinformationandtechnologyassistance,contactusat ProductAssistant:GillianDaniels CengageLearningCustomer&SalesSupport,www.cengage.com/support. SeniorMarketDevelopmentManager: Forpermissiontousematerialfromthistextorproduct, EricLaScola submitallrequestsonlineatcengage.com/permissions. MarketingManager:GretchenSwann Furtherpermissionsquestionscanbee-mailedto [email protected]. ArtDirector:CherylPearl,GEX PublishingServices LibraryofCongressControlNumber:2013956197 TextDesigner:GEXPublishingServices ISBN-13:978-1-285-77671-2 CoverDesigner:GEXPublishingServices CengageLearning ImageCredit:©Kasia/Shutterstock.com 200FirstStamfordPlace,4thFloor ManufacturingPlanner:JulioEsperas Stamford,CT06902 USA Copyeditor:MichaelBeckett CengageLearningisaleadingproviderofcustomizedlearningsolutionswith Proofreader:LisaWeidenfeld officelocationsaroundtheglobe,includingSingapore,theUnitedKingdom, Indexer:AlexandraNickerson Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat: www.cengage.com/global Compositor:Integra CengageLearningproductsarerepresentedinCanadaby NelsonEducation,Ltd. Purchaseanyofourproductsatyourlocalcollegestoreoratourpreferred onlinestore:www.cengagebrain.com Someoftheproductnamesandcompanynamesusedinthisbookhavebeen usedforidentificationpurposesonlyandmaybetrademarksorregistered trademarksoftheirrespectivemanufacturersandsellers. MicrosoftproductscreenshotsusedwithpermissionfromMicrosoftCorporation. Unlessotherwisecredited,allartandtables©2015CengageLearning,produced byIntegra. CengageLearningreservestherighttorevisethispublicationandmakechanges fromtimetotimeinitscontentwithoutnotice. Printedin theUnitedStatesofAmerica 12345671716151413 Copyright 2015 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 . . . . . . . . . . . . . . . . . . . ix iii CHAPTER 1 An Overview of Computers and Programming . . 1 CHAPTER 2 Elements of High-Quality Programs . . . . . . 38 CHAPTER 3 Understanding Structure . . . . . . . . . . . 87 CHAPTER 4 Making Decisions . . . . . . . . . . . . . 125 CHAPTER 5 Looping . . . . . . . . . . . . . . . . . 177 CHAPTER 6 Arrays . . . . . . . . . . . . . . . . . . 226 CHAPTER 7 File Handling and Applications . . . . . . . 274 CHAPTER 8 Advanced Data Handling Concepts . . . . . 321 CHAPTER 9 Advanced Modularization Techniques . . . . 371 CHAPTER 10 Object-Oriented Programming . . . . . . . 427 CHAPTER 11 More Object-Oriented Programming Concepts . . . . . . . . . . . . . . . . . 471 CHAPTER 12 Event-Driven GUI Programming, Multithreading, and Animation . . . . . . . 514 CHAPTER 13 System Modeling with the UML . . . . . . . 547 CHAPTER 14 Using Relational Databases . . . . . . . . 579 APPENDIX A Understanding Numbering Systems and Computer Codes . . . . . . . . . . . . 625 APPENDIX B Solving Difficult Structuring Problems . . . . 633 APPENDIX C Creating Print Charts . . . . . . . . . . . 642 APPENDIX D Two Variations on the Basic Structures— case and do-while . . . . . . . . .644 Glossary . . . . . . . . . . . . . . . . . 651 Index . . . . . . . . . . . . . . . . . . . 667 Copyright 2015 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 iv Preface . . . . . . . . . . . . . . . . . . . ix CHAPTER 1 An Overview of Computers and Programming . . 1 Understanding ComputerSystems . . . . . . . . . . . . . . . .2 Understanding SimpleProgramLogic . . . . . . . . . . . . . .5 Understanding the ProgramDevelopmentCycle . . . . . . . . . .7 Using PseudocodeStatements andFlowchartSymbols . . . . . . 14 Using aSentinelValue to EndaProgram . . . . . . . . . . . . 20 Understanding Programming andUserEnvironments . . . . . . . 23 Understanding the EvolutionofProgramming Models . . . . . . . 26 ChapterSummary . . . . . . . . . . . . . . . . . . . . . . 28 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . 28 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 31 CHAPTER 2 Elements of High-Quality Programs . . . . . . 38 Declaring andUsing VariablesandConstants . . . . . . . . . . 39 Performing Arithmetic Operations . . . . . . . . . . . . . . . 47 Understanding the Advantages ofModularization . . . . . . . . . 51 Modularizing aProgram . . . . . . . . . . . . . . . . . . . 54 Creating Hierarchy Charts . . . . . . . . . . . . . . . . . . 64 FeaturesofGoodProgramDesign . . . . . . . . . . . . . . . 66 ChapterSummary . . . . . . . . . . . . . . . . . . . . . . 75 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . 76 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 79 CHAPTER 3 Understanding Structure . . . . . . . . . . . 87 TheDisadvantagesofUnstructuredSpaghettiCode . . . . . . . 88 Understanding the Three Basic Structures . . . . . . . . . . . 90 Using aPriming Input to Structurea Program . . . . . . . . . . 99 Understanding the ReasonsforStructure . . . . . . . . . . . .106 Recognizing Structure . . . . . . . . . . . . . . . . . . . .107 Structuring andModularizing UnstructuredLogic . . . . . . . . .110 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .116 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .116 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .117 Copyright 2015 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 4 Making Decisions . . . . . . . . . . . . . 125 BooleanExpressions andthe SelectionStructure . . . . . . . .126 Using Relational Comparison Operators . . . . . . . . . . . .131 Understanding AND Logic . . . . . . . . . . . . . . . . . .135 Understanding ORLogic . . . . . . . . . . . . . . . . . . .145 v Understanding NOTLogic . . . . . . . . . . . . . . . . . .156 Making Selectionswithin Ranges . . . . . . . . . . . . . . .157 Understanding Precedence When Combining ANDandOR Operators . . . . . . . . . . . . . . . . . . . . . . . .163 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .166 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .167 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .168 CHAPTER 5 Looping . . . . . . . . . . . . . . . . . 177 Understanding the Advantages ofLooping . . . . . . . . . . .178 Using aLoopControlVariable . . . . . . . . . . . . . . . . .180 NestedLoops . . . . . . . . . . . . . . . . . . . . . . .186 Avoiding CommonLoopMistakes . . . . . . . . . . . . . . .192 Using aforLoop . . . . . . . . . . . . . . . . . . . . . .201 CommonLoopApplications . . . . . . . . . . . . . . . . . .203 Comparing Selections andLoops . . . . . . . . . . . . . . .213 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .217 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .217 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .218 CHAPTER 6 Arrays . . . . . . . . . . . . . . . . . . 226 Storing Data in Arrays . . . . . . . . . . . . . . . . . . . .227 HowanArrayCanReplace NestedDecisions . . . . . . . . . .230 Using ConstantswithArrays . . . . . . . . . . . . . . . . .239 Searching anArrayforanExactMatch . . . . . . . . . . . . .241 Using Parallel Arrays . . . . . . . . . . . . . . . . . . . . .246 Searching anArrayfora RangeMatch . . . . . . . . . . . . .253 Remaining within ArrayBounds . . . . . . . . . . . . . . . .257 Using aforLoopto Process anArray . . . . . . . . . . . . .261 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .262 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .263 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .263 Copyright 2015 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 CHAPTER 7 File Handling and Applications . . . . . . . 274 Understanding ComputerFiles . . . . . . . . . . . . . . . .275 Understanding the Data Hierarchy . . . . . . . . . . . . . . .277 Performing FileOperations . . . . . . . . . . . . . . . . . .279 Understanding ControlBreakLogic . . . . . . . . . . . . . .286 vi Merging SequentialFiles . . . . . . . . . . . . . . . . . . .292 MasterandTransactionFile Processing . . . . . . . . . . . .301 Random Access Files . . . . . . . . . . . . . . . . . . . .310 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .311 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .312 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .314 CHAPTER 8 Advanced Data Handling Concepts . . . . . 321 Understanding the NeedforSorting Data . . . . . . . . . . . .322 Using the Bubble SortAlgorithm . . . . . . . . . . . . . . . .323 Sorting MultifieldRecords . . . . . . . . . . . . . . . . . .342 Using the Insertion Sort Algorithm . . . . . . . . . . . . . . .345 Using Multidimensional Arrays . . . . . . . . . . . . . . . . .349 Using IndexedFilesandLinkedLists . . . . . . . . . . . . . .356 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .361 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .362 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .363 CHAPTER 9 Advanced Modularization Techniques . . . . 371 ThePartsofa Method . . . . . . . . . . . . . . . . . . . .372 Using Methodswithno Parameters . . . . . . . . . . . . . .373 Creating MethodsthatRequireParameters . . . . . . . . . . .376 Creating MethodsthatReturn a Value . . . . . . . . . . . . .384 Passing anArraytoa Method . . . . . . . . . . . . . . . . .391 OverloadingMethods . . . . . . . . . . . . . . . . . . . .398 Using Predefined Methods . . . . . . . . . . . . . . . . . .405 MethodDesign Issues:ImplementationHiding, Cohesion, andCoupling . . . . . . . . . . . . . . . . . .407 Understanding Recursion . . . . . . . . . . . . . . . . . . .410 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .415 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .416 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .418 Copyright 2015 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 10 Object-Oriented Programming . . . . . . . 427 Principles ofObject-OrientedProgramming . . . . . . . . . . .428 Defining ClassesandCreatingClass Diagrams . . . . . . . . .435 Understanding Public andPrivateAccess . . . . . . . . . . . .444 Organizing Classes . . . . . . . . . . . . . . . . . . . . .448 vii Understanding Instance Methods . . . . . . . . . . . . . . .449 Understanding Static Methods . . . . . . . . . . . . . . . .454 Using Objects . . . . . . . . . . . . . . . . . . . . . . . .456 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .462 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .463 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .465 CHAPTER 11 More Object-Oriented Programming Concepts . . . . . . . . . . . . . . . . . 471 Understanding Constructors . . . . . . . . . . . . . . . . .472 Understanding Destructors . . . . . . . . . . . . . . . . . .479 Understanding Composition . . . . . . . . . . . . . . . . . .481 Understanding Inheritance . . . . . . . . . . . . . . . . . .482 AnExample ofUsing PredefinedClasses: CreatingGUIObjects . . . . . . . . . . . . . . . . . . .494 Understanding ExceptionHandling . . . . . . . . . . . . . . .495 Reviewingthe Advantages ofObject-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . .501 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .502 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .503 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .504 CHAPTER 12 Event-Driven GUI Programming, Multithreading, and Animation . . . . . . . 514 Understanding Event-Driven Programming . . . . . . . . . . . .515 User-InitiatedActionsandGUIComponents . . . . . . . . . . .518 DesigningGraphicalUserInterfaces . . . . . . . . . . . . . .521 Developing anEvent-DrivenApplication . . . . . . . . . . . . .524 Understanding ThreadsandMultithreading . . . . . . . . . . .532 Creating Animation . . . . . . . . . . . . . . . . . . . . .535 ChapterSummary . . . . . . . . . . . . . . . . . . . . . .538 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . .539 Exercises . . . . . . . . . . . . . . . . . . . . . . . . .540 Copyright 2015 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.