CIS 110 LAB ASSIGNMENT DETAILS AT THE ATTACHED...

profilethythythythy
cis_110.pdf

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 [email protected]

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 . . . . . . . . . . . . . …