Computing with Business Applications 1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning. 2: VBA for Modelers: Developing Decision Support Systems with Microsoft Office Excel; by Albright 5th edition, South Western Cengage Learning. Chapter 1 An Introduction to Programming 1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning. pages: 1-37 Objectives 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 3© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Computer Systems • Computer system • Combination of all the components required to process and store data using a computer • Hardware • Equipment associated with a computer • Software • Computer instructions • Tells the hardware what to do
• Programs • Instructions written by programmers 4© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Computer Systems (continued) • Application software such as word processing, spreadsheets, payroll and inventory, even games • System software such as operating systems like Windows, Linux, or UNIX • Computer hardware and software accomplish three major operations • Input • Data items such as text, numbers, images, and sound • Processing • Calculations and comparisons performed by the central processing unit (CPU) 5© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or
posted to a publicly accessible website, in whole or in part. Understanding Computer Systems (continued) • Output • Resulting information that is sent to a printer, a monitor, or storage devices after processing • Programming language • Used to write computer instructions • Examples • Visual Basic, C#, C++, or Java • Syntax • Rules governing word usage and punctuation 6© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Computer Systems (continued) • Computer memory • Computer’s temporary, internal storage – random access memory (RAM)
• Volatile memory – lost when the power is off • Permanent storage devices • Nonvolatile memory • Compiler or interpreter • Translates source code into machine language (binary language) statements called object code • Checks for syntax errors 7© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Simple Program Logic • Program executes or runs • Input will be accepted, some processing will occur, and results will be output • Programs with syntax errors cannot execute • Logical errors • Errors in program logic produce incorrect output • Logic of the computer program • Sequence of specific instructions in specific order • Variable
• Named memory location whose value can vary 8© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Program Development Cycle • Program development cycle • Understand the problem • Plan the logic • Code the program • Use software (a compiler or interpreter) to translate the program into machine language • Test the program • Put the program into production • Maintain the program 9© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding the Program Development Cycle (continued) 10 Figure 1-1 The program development cycle © 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Problem • One of the most difficult aspects of programming • Users or end users • People for whom a program is written • Documentation • Supporting paperwork for a program 11© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Planning the Logic • Heart of the programming process • Most common planning tools • Flowcharts • Pseudocode • IPO charts (input, processing, and output) • TOE charts (tasks, objects, and events) • Desk-checking • Walking through a program’s logic on paper before you actually write the program 12© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Coding the Program • Hundreds of programming languages available • Choose based on features • Similar in their basic capabilities • Easier than the planning step
13© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using Software to Translate the Program into Machine Language • Translator program • Compiler or interpreter • Changes the programmer’s English-like high-level programming language into the low-level machine language • Syntax error • Misuse of a language’s grammar rules • Programmer corrects listed syntax errors • Might need to recompile the code several times 14© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using Software to Translate the Program into Machine Language (continued)
Figure 1-2 Creating an executable program 15© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Testing the Program • Logical error • Results when a syntactically correct statement, but the wrong one for the current context, is used • Test • Execute the program with some sample data to see whether the results are logically correct • Debugging is the process of finding and correcting program errors • Programs should be tested with many sets of data 16© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Putting the Program into Production • Process depends on program’s purpose • May take several months • Conversion • The entire set of actions an organization must take to switch over to using a new program or set of programs 17© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Maintaining the Program • Maintenance • Making changes after the program is put into production • Common first programming job • Maintaining previously written programs • Make changes to existing programs • Repeat the development cycle 18© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Using Pseudocode Statements and Flowchart Symbols • Pseudocode • English-like representation of the logical steps it takes to solve a problem • Flowchart • Pictorial representation of the logical steps it takes to solve a problem 19© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Writing Pseudocode • Pseudocode representation of a number-doubling problem start input myNumber set myAnswer = myNumber * 2 output myAnswer stop 20© 2013 Cengage Learning. All Rights Reserved. This edition
is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Writing Pseudocode (continued) • Programmers preface their pseudocode with a beginning statement like start and end it with a terminating statement like stop • Flexible planning tool 21© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Drawing Flowcharts • Create a flowchart • Draw geometric shapes that contain the individual statements • Connect shapes with arrows • Input symbol • Indicates input operation • Parallelogram • Processing symbol
• Contains processing statements such as arithmetic • Rectangle 22© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Drawing Flowcharts (continued) • Output symbol • Represents output statements • Parallelogram • Flowlines • Arrows that connect steps • Terminal symbols • Start/stop symbols • Shaped like a racetrack • Also called lozenges 23© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Drawing Flowcharts (continued) Figure 1-6 Flowchart and pseudocode of program that doubles a number 24© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Repeating Instructions • Program in Figure 1-6 only works for one number • Not feasible to run the program over and over 10,000 times • Not feasible to add 10,000 lines of code to a program • Create a loop (repetition of a series of steps) instead • Avoid an infinite loop (repeating flow of logic that never ends) 25© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Repeating Instructions (continued)
Figure 1-8 Flowchart of infinite number-doubling program 26© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using a Sentinel Value to End a Program • Making a decision • Testing a value • Decision symbol • Diamond shape • Dummy value • Data-entry value that the user will never need • Sentinel value • eof (“end of file”) • Marker at the end of a file that automatically acts as a sentinel 27© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using a Sentinel Value to End
a Program (continued) Figure 1-9 Flowchart of number-doubling program with sentinel value of 0 28© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using a Sentinel Value to End a Program (continued) Figure 1-10 Flowchart using eof 29© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Programming and User Environments • Many options for programming and user environments • Planning • Flowchart • Pseudocode • Coding
• Text editors • Executing • Input from keyboard, mouse, microphone • Outputting • Text, images, sound 30© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Programming Environments • Use a keyboard to type program statements into an editor • Plain text editor • Similar to a word processor but without as many features • Text editor that is part of an integrated development environment (IDE) • Software package that provides an editor, a compiler, and other programming tools 31© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Understanding Programming Environments (continued) Figure 1-12 A C# number-doubling program in Visual Studio 32© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding User Environments • Command line • Location on your computer screen where you type text entries to communicate with the computer’s operating system • Graphical user interface (GUI) • Allows users to interact with a program in a graphical environment 33© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding User Environments (continued)
Figure 1-13 Executing a number-doubling program in a command-line environment 34© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding User Environments (continued) Figure 1-14 Executing a number-doubling program in a GUI environment 35© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Evolution of Programming Models • People have been writing modern computer programs since the 1940s • Newer programming languages • Look much more like natural language
• Are easier to use • Create self-contained modules or program segments that can be pieced together in a variety of ways 36© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Evolution of Programming Models (continued) • Major models or paradigms used by programmers • Procedural programming • Focuses on the procedures that programmers create • Object-oriented programming • Focuses on objects, or “things,” and describes their features (or attributes) and their behaviors • This text • Focuses on procedural programming techniques 37© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary • Hardware and software accomplish input, processing, and output • Logic must be developed correctly • Logical errors are much more difficult to locate than syntax errors • Use flowcharts, pseudocode, IPO charts, and TOE charts to plan the logic • Avoid infinite loops by testing for a sentinel value • Use a text editor or an IDE to enter your program statements 38© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Option #1:Organizational Problem or Opportunity Think about a specific problem or opportunity in your organization, your community, your professional association, or your social organization. Write a paper analyzing the three different research methods presented in this module. Compare each and determine which would be the most appropriate method to utilize in your research study.
The methods : qualitative, quantitative, or mixed methods COMPREHENSIVE Joyce Farrell Ninth Edition Logic & Design Programming N in th E d itio n C O M P R E H
E N S IV E P ro g ra m m in g L o g ic & D e sig n Jo yce Farrell
To register or access your online learning solution or purchase materials for your course, visit www.cengagebrain.com. Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 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. Untitled-1.indd 1 10/12/16 3:56 PM Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 PROGRAMMING LOGIC
AND DESIGN COMPREHENSIVE Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 PROGRAMMING LOGIC AND DESIGN COMPREHENSIVE J O Y C E FA R R E L L N I N T H E D I T I O N Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United St ates Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Programming Logic and Design, Comprehensive
Ninth Edition Joyce Farrell Senior Product Director: Kathleen McMahon Product Team Leader: Kristin McNary Associate Product Manager: Kate Mason Senior Content Developer: Alyssa Pratt Senior Content Project Manager: Jennifer Feltri-George Manufacturing Planner: Julio Esperas Art Director: Diana Graham Production Service/Composition: SPi Global Cover Photo: Colormos/Photodisc/Getty Images Printed in the United States of America Print Number: 01 Print Year: 2018 © 2018 Cengage Learning® ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced or distributed in any form or by any means, except as permitted by U.S. copyright law, without the prior written permission of the copyright owner. For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Further permissions questions can be emailed to [email protected] Cengage Learning 20 Channel Center Street Boston, MA 02210 USA Unless otherwise noted all items © Cengage Learning. Cengage Learning is a leading provider of customized learning solutions with employees residing in nearly 40 different coun- tries and sales in more than 125 countries around the world. Find your local representative at www.cengage.com. Cengage Learning products are represented in Canada by Nelson Education, Ltd. To learn more about Cengage Learning Solution s, visit www.cengage.com Purchase any of our products at your local college store or at our preferred online store www.cengagebrain.com
Library of Congress Control Number: 2016959742 ISBN: 978-1-337-10207-0 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Brief Contents P re f a c e � � � � � � � � � � � � � � � � x v i C H A P T E R 1 A n O v e r v i e w o f C o m p u t e r s a n d P ro g r a m m i n g � 1 C H A P T E R 2 E l e m e n t s o f H i g h - Q u a l i t y P ro g r a m s � � � � � 3 8 C H A P T E R 3 U n d e r s t a n d i n g S t r u c t u re � � � � � � � � � � 8 7 C H A P T E R 4 M a k i n g D e c i s i o n s � � � � � � � � � � � � 1 2 4 C H A P T E R 5 L o o p i n g � � � � � � � �
� � � � � � � � 1 7 6 C H A P T E R 6 A r r a y s � � � � � � � � � � � � � � � � � 2 2 7 C H A P T E R 7 F i l e H a n d l i n g a n d A p p l i c a t i o n s � � � � � � 2 7 2 C H A P T E R 8 A d v a n c e d D a t a H a n d l i n g C o n c e p t s � � � � � 3 2 1 C H A P T E R 9 A d v a n c e d M o d u l a r i z a t i o n Te c h n i q u e s � � � 3 6 6 C H A P T E R 1 0 O b j e c t - O r i e n t e d P ro g r a m m i n g � � � � � � � 4 2 0 C H A P T E R 1 1 M o re O b j e c t - O r i e n t e d P ro g r a m m i n g C o n c e p t s � � � � � � � � � � � � � � � 4 6 4 C H A P T E R 1 2 E v e n t - D r i v e n G U I P ro g r a m m i n g , M u l t i t h re a d i n g , a n d A n i m a t i o n � � � � � � 5 0 7 A P P E N D I X A U n d e r s t a n d i n g N u m b e r i n g S y s t e m s a n d C o m p u t e r C o d e s � � � � � � � �
� � 5 3 9 A P P E N D I X B S o l v i n g D i f f i c u l t S t r u c t u r i n g P ro b l e m s � � � 5 4 7 G l o s s a r y � � � � � � � � � � � � � � � � 5 5 6 I n d e x � � � � � � � � � � � � � � � � � 5 7 1 v Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Contents P re f a c e � � � � � � � � � � � � �
� � � x v i C H A P T E R 1 A n O v e r v i e w o f C o m p u t e r s a n d P ro g r a m m i n g � 1 Understanding Computer Systems � � � � � � � � � � � � � 2 Understanding Simple Program Logic � � � � � � � � � � � � 5 Understanding the Program Development Cycle � � � � � � � 8 Understanding the Problem � � � � � � � � � � � � � � � � 8 Planning the Logic � � � � � � � � � � � � � � � � � � � 10 Coding the Program � � � � � � � � � � � � � � � � � � 10 Using Software to Translate the Program into Machine Language � � � � � � � � � � � � � � � � � � � � � � 11 Testing the Program � � � � � � � � � � � � � � � � � � 12 Putting the Program into Production� � � � � � � � �
� � 13 Maintaining the Program � � � � � � � � � � � � � � � � 14 Using Pseudocode Statements and Flowchart Symbols � � � 15 Writing Pseudocode � � � � � � � � � � � � � � � � � � 15 Drawing Flowcharts � � � � � � � � � � � � � � � � � � 17 Repeating Instructions � � � � � � � � � � � � � � � � � 19 Using a Sentinel Value to End a Program � � � � � � � � � 20 Understanding Programming and User Environments � � � � 23 Understanding Programming Environments � � � � � � � 23 Understanding User Environments � � � � � � � � � � � 25 Understanding the Evolution of Programming Models � � � � 27
Chapter Summary � � � � � � � � � � � � � � � � � � � � 28 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 29 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 32 C H A P T E R 2 E l e m e n t s o f H i g h - Q u a l i t y P ro g r a m s � � � � � 3 8 Declaring and Using Variables and Constants � � � � � � � 39 Understanding Data Types � � � � � � � � � � � � � � � 39 Understanding Unnamed, Literal Constants � � � � � � � 39 Working with Variables � � � � � � � � � � � � � � � � � 40 Understanding a Declaration’s Data Type � � � � � � � � 41 vii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
WCN 02-300 Understanding a Declaration’s Identifier � � � � � � � � � 42 Assigning Values to Variables � � � � � � � � � � � � � � 45 Declaring Named Constants � � � � � � � � � � � � � � 46 Performing Arithmetic Operations � � � � � � � � � � � � � 47 The Integer Data Type � � � � � � � � � � � � � � � � � 50 Understanding the Advantages of Modularization � � � � � � 51 Modularization Provides Abstraction� � � � � � � � � � � 52 Modularization Helps Multiple Programmers to Work on a Problem � � � � � � � � � � � � � � � � � � � � 53 Modularization Allows You to Reuse Work � � � � � �
� � 53 Modularizing a Program � � � � � � � � � � � � � � � � 54 Declaring Variables and Constants within Modules � � � � � 58 Understanding the Most Common Configuration for Mainline Logic � � � � � � � � � � � � � � � � � � � 60 Creating Hierarchy Charts � � � � � � � � � � � � � � � 64 Features of Good Program Design � � � � � � � � � � � � 66 Using Program Comments � � � � � � � � � � � � � � � 67 Choosing Identifiers � � � � � � � � � � � � � � � � � � 69 Designing Clear Statements � � � � � � � � � � � � � � 71 Writing Clear Prompts and Echoing Input � � � � � � � � 72 Maintaining Good Programming Habits � � � � � � �
� � � 74 Chapter Summary � � � � � � � � � � � � � � � � � � � � 75 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 76 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 79 C H A P T E R 3 U n d e r s t a n d i n g S t r u c t u re � � � � � � � � � � 8 7 The Disadvantages of Unstructured Spaghetti Code � � � � 88 Understanding the Three Basic Structures � � � � � � � � � 90 The Sequence Structure � � � � � � � � � � � � � � � � 90 The Selection Structure � � � � � � � � � � � � � � � � 91 The Loop Structure � � � � � � � � � � � � � � � � � � 92 Combining Structures � � � � � � � � � � � � � � � � � 93
Using a Priming Input to Structure a Program � � � � � � � 99 Understanding the Reasons for Structure � � � � � � � � � 106 Recognizing Structure � � � � � � � � � � � � � � � � � � 107 Structuring and Modularizing Unstructured Logic � � � � � � 110 Chapter Summary � � � � � � � � � � � � � � � � � � � � 115 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 115 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 117 C O N T E N T S viii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300
C H A P T E R 4 M a k i n g D e c i s i o n s � � � � � � � � � � � � 1 2 4 The Selection Structure � � � � � � � � � � � � � � � � � 125 Using Relational Comparison Operators � � � � � � � � � � 129 Avoiding a Common Error with Relational Operators � � � 133 Understanding AND Logic � � � � � � � � � � � � � � � � 134 Nesting AND Decisions for Efficiency � � � � � � � � � � 137 Using the AND Operator � � � � � � � � � � � � � � � � 139 Avoiding Common Errors in an AND Selection � � � � � � 141 Understanding OR Logic � � � � � � � � � � � � � � � � � 143 Writing OR Selections for Efficiency � � � � � � � � � � � 145 Using the OR Operator � � � � � � � � � � � � �
� � � � 147 Avoiding Common Errors in an OR Selection � � � � � � � 147 Understanding NOT Logic � � � � � � � � � � � � � � � � 153 Avoiding a Common Error in a NOT Expression � � � � � � 154 Making Selections within Ranges � � � � � � � � � � � � � 155 Avoiding Common Errors When Using Range Checks � � � 157 Understanding Precedence When Combining AND and OR Operators � � � � � � � � � � � � � � � � � 160 Understanding the case Structure � � � � � � � � � � � � 163 Chapter Summary � � � � � � � � � � � � � � � � � � � � 165 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 166 Exercises � � � � � � � � � � � � � � � � �
� � � � � � � 167 C H A P T E R 5 L o o p i n g � � � � � � � � � � � � � � � � 1 7 6 Appreciating the Advantages of Looping � � � � � � � � � � 177 Using a Loop Control Variable � � � � � � � � � � � � � � 179 Using a Definite Loop with a Counter � � � � � � � � � � 179 Using an Indefinite Loop with a Sentinel Value � � � � � � 181 Understanding the Loop in a Program’s Mainline Logic � � 183 Nested Loops � � � � � � � � � � � � � � � � � � � � � � 185 Avoiding Common Loop Mistakes � � � � � � � � � � � � � 190 Mistake: Failing to Initialize the Loop Control Variable � � � 190 Mistake: Neglecting to Alter the Loop Control Variable � �
191 Mistake: Using the Wrong Type of Comparison When Testing the Loop Control Variable � � � � � � � � � � � 192 Mistake: Including Statements Inside the Loop Body that Belong Outside the Loop � � � � � � � � � � � � � 194 Using a for Loop � � � � � � � � � � � � � � � � � � � � 199 Using a Posttest Loop � � � � � � � � � � � � � � � � � � 201 Recognizing the Characteristics Shared by Structured Loops � � � � � � � � � � � � � � � � � � 203 ix C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300
Common Loop Applications � � � � � � � � � � � � � � � 205 Using a Loop to Accumulate Totals � � � � � � � � � � � 205 Using a Loop to Validate Data � � � � � � � � � � � � � 209 Limiting a Reprompting Loop � � � � � � � � � � � � � � 209 Validating a Data Type � � � � � � � � � � � � � � � � � 212 Validating Reasonableness and Consistency of Data � � � 213 Comparing Selections and Loops � � � � � � � � � � � � � 214 Chapter Summary � � � � � � � � � � � � � � � � � � � � 218 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 218 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 220
C H A P T E R 6 A r r a y s � � � � � � � � � � � � � � � � � 2 2 7 Storing Data in Arrays � � � � � � � � � � � � � � � � � � 228 How Arrays Occupy Computer Memory � � � � � � � � � 228 How an Array Can Replace Nested Decisions� � � � � � � � 231 Using Constants with Arrays � � � � � � � � � � � � � � � 238 Using a Constant as the Size of an Array � � � � � � � � 238 Using Constants as Array Element Values � � � � � � � � 239 Using a Constant as an Array Subscript � � � � � � � � � 239 Searching an Array for an Exact Match � � � � � � � � � � 240 Using Parallel Arrays � � � � � � � � � � � � � � � � � � 244
Improving Search Efficiency � � � � � � � � � � � � � � 248 Searching an Array for a Range Match� � � � � � � � � � � 250 Remaining within Array Bounds � � � � � � � � � � � � � � 255 Understanding Array Size � � � � � � � � � � � � � � � 255 Understanding Subscript Bounds � � � � � � � � � � � � 255 Using a for Loop to Process an Array � � � � � � � � � � 258 Chapter Summary � � � � � � � � � � � � � � � � � � � � 260 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 261 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 261 C H A P T E R 7 F i l e H a n d l i n g a n d A p p l i c a t i o n s � � � � � � 2 7 2 Understanding Computer Files � � � � � � � � � �
� � � � 273 Organizing Files � � � � � � � � � � � � � � � � � � � � 274 Understanding the Data Hierarchy� � � � � � � � � � � � � 275 Performing File Operations � � � � � � � � � � � � � � � � 277 Declaring a File Identifier� � � � � � � � � � � � � � � � 277 Opening a File � � � � � � � � � � � � � � � � � � � � 278 Reading Data from a File and Processing It � � � � � � � 278 Writing Data to a File � � � � � � � � � � � � � � � � � 281 Closing a File � � � � � � � � � � � � � � � � � � � � � 281 x C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May
not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 A Program that Performs File Operations � � � � � � � � 282 Understanding Control Break Logic � � � � � � � � � � � � 285 Merging Sequential Files � � � � � � � � � � � � � � � � � 290 Master and Transaction File Processing � � � � � � � � � � 299 Random Access Files � � � � � � � � � � � � � � � � � � 308 Chapter Summary � � � � � � � � � � � � � � � � � � � � 309 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 310 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 312 C H A P T E R 8 A d v a n c e d D a t a H a n d l i n g C o n c e p t s � � � � � 3 2 1
Understanding the Need for Sorting Data � � � � � � � � � 322 Using the Bubble Sort Algorithm � � � � � � � � � � � � � 324 Understanding Swapping Values � � � � � � � � � � � � 324 Understanding the Bubble Sort � � � � � � � � � � � � � 325 Sorting Multifield Records � � � � � � � � � � � � � � � � 340 Sorting Data Stored in Parallel Arrays � � � � � � � � � � 340 Sorting Records as a Whole � � � � � � � � � � � � � � 341 Other Sorting Algorithms � � � � � � � � � � � � � � � � � 342 Using Multidimensional Arrays � � � � � � � � � � � � � � 345 Using Indexed Files and Linked Lists � � � � � � � � � � � 351 Using Indexed Files � � � � � � � � � � � � � �
� � � � 352 Using Linked Lists � � � � � � � � � � � � � � � � � � � 353 Chapter Summary � � � � � � � � � � � � � � � � � � � � 356 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 357 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 358 C H A P T E R 9 A d v a n c e d M o d u l a r i z a t i o n Te c h n i q u e s � � � 3 6 6 The Parts of a Method � � � � � � � � � � � � � � � � � � 367 Using Methods with no Parameters � � � � � � � � � � � � 368 Creating Methods that Require Parameters � � � � � � � � 371 Creating Methods that Require Multiple Parameters � � � � 377 Creating Methods that Return a Value � � � � � � � � � � � 379
Using an IPO Chart � � � � � � � � � � � � � � � � � � 384 Passing an Array to a Method � � � � � � � � � � � � � � 386 Overloading Methods � � � � � � � � � � � � � � � � � � 394 Avoiding Ambiguous Methods � � � � � � � � � � � � � � 397 Using Predefined Methods � � � � � � � � � � � � � � � � 400 Method Design Issues: Implementation Hiding, Cohesion, and Coupling � � � � � � � � � � � � � � � � � � � � � 402 Understanding Implementation Hiding � � � � � � � � � � 402 Increasing Cohesion � � � � � � � � � � � � � � � � � � 403 Reducing Coupling� � � � � � � � � � � � � � � � � � � 404 Understanding Recursion � � � � � � � � � � � � � � � � � 405
xi C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Chapter Summary � � � � � � � � � � � � � � � � � � � � 410 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 411 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 412 C H A P T E R 1 0 O b j e c t - O r i e n t e d P ro g r a m m i n g � � � � � � � 4 2 0 Principles of Object-Oriented Programming � � � � � � � � 421 Classes and Objects � � � � � � � � � � � � � � � � � � 421
Polymorphism � � � � � � � � � � � � � � � � � � � � � 424 Inheritance � � � � � � � � � � � � � � � � � � � � � � 426 Encapsulation � � � � � � � � � � � � � � � � � � � � � 426 Defining Classes and Creating Class Diagrams � � � � � � � 428 Creating Class Diagrams � � � � � � � � � � � � � � � � 430 The Set Methods � � � � � � � � � � � � � � � � � � � 433 The Get Methods � � � � � � � � � � � � � � � � � � � 434 Work Methods� � � � � � � � � � � � � � � � � � � � � 435 Understanding Public and Private Access � � � � � � � � � 437 Organizing Classes � � � � � � � � � � � � � � � � � � � 440 Understanding Instance Methods � � � � � � � � � � � � � 441 Understanding Static Methods � � � � � � � � � �
� � � � 447 Using Objects � � � � � � � � � � � � � � � � � � � � � � 448 Passing an Object to a Method � � � � � � � � � � � � � 449 Returning an Object from a Method � � � � � � � � � � � 450 Using Arrays of Objects � � � � � � � � � � � � � � � � 453 Chapter Summary � � � � � � � � � � � � � � � � � � � � 455 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 456 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 458 C H A P T E R 1 1 M o re O b j e c t - O r i e n t e d P ro g r a m m i n g C o n c e p t s � � � � � � � � � � � � � � � 4 6 4 Understanding Constructors � � � � � � � � � � � � � � � 465
Default Constructors � � � � � � � � � � � � � � � � � 466 Non-default Constructors � � � � � � � � � � � � � � � � 468 Overloading Instance Methods and Constructors � � � � � 469 Understanding Destructors � � � � � � � � � � � � � � � � 472 Understanding Composition � � � � � � � � � � � � � � � 474 Understanding Inheritance � � � � � � � � � � � � � � � � 475 Understanding Inheritance Terminology � � � � � � � � � 478 Accessing Private Fields and Methods of a Parent Class� � � � � � � � � � � � � � � � � � � 481 Overriding Parent Class Methods in a Child Class� � � � � 486 xii
C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Using Inheritance to Achieve Good Software Design � � � 486 An Example of Using Predefined Classes: Creating GUI Objects � � � � � � � � � � � � � � � � � � 487 Understanding Exception Handling � � � � � � � � � � � � 488 Drawbacks to Traditional Error-Handling Techniques � � � 489 The Object-Oriented Exception-Handling Model � � � � � � 491 Using Built-in Exceptions and Creating Your Own Exceptions � � � � � � � � � � � � � � � � � 493
Reviewing the Advantages of Object-Oriented Programming 494 Chapter Summary � � � � � � � � � � � � � � � � � � � � 495 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 496 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 497 C H A P T E R 1 2 E v e n t - D r i v e n G U I P ro g r a m m i n g , M u l t i t h re a d i n g , a n d A n i m a t i o n � � � � � � 5 0 7 Understanding Event-Driven Programming � � � � � � � � � 508 User-Initiated Actions and GUI Components � � � � � � � � 511 Designing Graphical User Interfaces � � � � � � � � � � � 514 The Interface Should Be Natural and Predictable � � � � � 514 The Interface Should Be Attractive, Easy to Read, and Nondistracting � � � � � � � � � � � � � �
� � � � 515 To Some Extent, It’s Helpful If the User Can Customize Your Applications� � � � � � � � � � � � � � � � � � � 516 The Program Should Be Forgiving � � � � � � � � � � � � 516 The GUI Is Only a Means to an End � � � � � � � � � � � 516 Developing an Event-Driven Application � � � � � � � � � � 517 Creating Wireframes � � � � � � � � � � � � � � � � � � 518 Creating Storyboards � � � � � � � � � � � � � � � � � 518 Defining the Storyboard Objects in an Object Dictionary� � 519 Defining Connections Between the User Screens � � � � � 520 Planning the Logic � � � � � � � � � � � � � � � � � � � 520 Understanding Threads and Multithreading � � � � � � � � � 525
Creating Animation � � � � � � � � � � � � � � � � � � � 528 Chapter Summary � � � � � � � � � � � � � � � � � � � � 531 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 532 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 533 xiii C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 A P P E N D I X A U n d e r s t a n d i n g N u m b e r i n g S y s t e m s a n d C o m p u t e r C o d e s � � � � � � � � � � 5 3 9 A P P E N D I X B S o l v i n g D i f f i c u l t S t r u c t u r i
n g P ro b l e m s � � � 5 4 7 G l o s s a r y � � � � � � � � � � � � � � � � 5 5 6 I n d e x � � � � � � � � � � � � � � � � � 5 7 1 C O N T E N T S xiv Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Preface
Programming Logic and Design, Comprehensive, Ninth Edition, provides the beginning programmer with a guide to developing structured program logic. This textbook assumes no programming language experience. The writing is nontechnical and emphasizes good programming practices. The examples are business examples; they do not assume mathematical background beyond high school business math. Additionally, the examples illustrate one or two major points; they do not contain so many features that students become lost following irrelevant and extraneous details. The examples in this book have been created to provide students with a sound background in logic, no matter what programming languages they eventually use to write programs. This book can be used in a stand-alone logic course that students take as a prerequisite to a programming course, or as a companion book to an introductory programming text using any programming language.
Organization and Coverage Programming Logic and Design, Comprehensive, Ninth 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 provides students a solid foundation for 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 that 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 con- cepts and terminology used in object-oriented programming. Students learn about classes, objects, instance and static class members, constructors, destructors, inheritance, and the xvi Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 advantages of object-oriented thinking. Chapter 12 explores some additional object-oriented programming issues: event-driven GUI
programming, multithreading, and animation. Two appendices instruct students on working with numbering systems and providing structure for large programs. 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: • It is written and designed to be non-language specific. The logic used in this book can be applied to any programming language.
• The examples are everyday business examples: no special knowledge of mathematics, accounting, or other disciplines is assumed. • The concept of structure is covered earlier than in many other texts. Students are exposed to structure naturally, so that they will automatically create properly designed programs. • Text explanation is interspersed with both flowcharts and pseudocode so that students can become comfortable with these logic development tools and understand their inter- relationship. Screen shots of running programs also are included, providing students with a clear and concrete image of the programs’ execution. • 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 a program. P R E F A C EOrganization and Coverage
xvii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Features This text focuses on helping students become better programmers, as well as helping them understand the big picture in program development through a variety of features. Each chapter begins with objectives and ends with a list of key terms and a summary; these useful features will help students organize their learning experience. Figure 3-17 Structured but incorrect solution to the number- doubling problem input originalNumber
Declarations num originalNumber num calculatedAnswer start stop not eof? Yes No output calculatedAnswer calculatedAnswer = originalNumber * 2 Don’t Do It This logic is structured, but flawed. When the user inputs the eof value, it will incorrectly be doubled and output.
tested. Instead, a result is calculated and displayed one last time before the loop-controlling test is made again. If the program was written to recognize eof when originalNumber is 0, then an extraneous answer of 0 will be displayed before the program ends. Depending on the language you are using and on the type of input being used, the results might be worse: The program might terminate by displaying an error message or the value output might be indecipherable garbage. In any case, this last output is superfluous—no value should be doubled and output after the eof condition is encountered. As a general rule, a program-ending test should always come immediately after an input statement because that’s the earliest point at which it can be evaluated. Therefore, the best solution to the number-doubling problem remains the one shown in Figure 3-16—the structured solution containing the priming input statement. Using a Priming Input to Structure a Program
105 C9275_Chapter03_hr.indd 105 11/15/16 2:07 PM and illustrations provide learning experience. the reader with a visual FLOWCHARTS, figures, 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. xviii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
WCN 02-300 F E A T U R E S • The instruction myAnswer = myNumber * 2 is an example of a processing operation. In most programming languages, an asterisk is used to indicate multiplication, so this instruction means “Change the value of the memory location myAnswer to equal the value at the memory location myNumber times two.” Mathematical operations are not the only kind of processing operations, but they are very typical. As with input operations, the type of hardware used for processing is irrelevant—after you write a program, it can be used on computers of different brand names, sizes, and speeds. • In the number-doubling program, the output myAnswer instruction is an example of an output operation. Within a particular program, this statement could cause the output
to appear on the monitor (which might be a flat-panel plasma screen or a smartphone display), or the output could go to a printer (which could be laser or ink-jet), or the output could be written to a disk or DVD. The logic of the output process is the same no matter what hardware device you use. When this instruction executes, the value stored in memory at the location named myAnswer is sent to an output device. (The output value also remains in computer memory until something else is stored at the same memory location or power is lost.) 7 Understanding Simple Program Logic Watch the video A Simple Program. Computer memory consists of millions of numbered locations where data can be stored. The memory location of myNumber has a specific numeric address, but when you write programs, you seldom need to be concerned with the value of the memory address; instead,
you use the easy-to-remember name you created. Computer programmers often refer to memory addresses using hexadecimal notation, or base 16. Using this system, they might use a value like 42FF01A to refer to a memory address. Despite the use of letters, such an address is still a number. Appendix A contains information about the hexadecimal numbering system. The false statement is #1. A program with syntax errors cannot execute; a program with no syntax errors can execute, but might produce incorrect results. TWO TRUTHS & A LIE Understanding Simple Program Logic 1. A program with syntax errors can execute but might produce incorrect results. 2. Although the syntax of programming languages differs, the same program logic can be expressed in different languages.
3. Most simple computer programs include steps that perform input, processing, and output. C9275_Chapter01_hr.indd 7 8/23/16 7:50 PMTWO 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 MindTap. VIDEO LESSONS help explain important chapter concepts. Videos are part of the text’s MindTap. NOTES provide additional information—
for example, another location in the book that expands on a topic, or a common error to avoid. xix Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Assessment Exercises Review Questions 1. Computer programs also are known as ____________. a. data b. hardware c. software
d. information 2. The major computer operations include ____________. a. input, processing, and output b. hardware and software c. sequence and looping d. spreadsheets, word processing, and data communications 3. Visual Basic, C++, and Java are all examples of computer ____________. a. operating systems b. programming languages c. hardware d. machine languages 4. A programming language’s rules are its ____________. a. syntax b. logic c. format d. options
5. The most important task of a compiler or interpreter is to ____________. a. create the rules for a programming language b. translate English statements into a language such as Java c. translate programming language statements into machine language d. execute machine language programs to perform useful tasks 6. Which of the following is temporary, internal storage? a. CPU b. hard disk c. keyboard d. memory 7. Which of the following pairs of steps in the programming process is in the correct order? a. code the program, plan the logic b. test the program, translate it into machine language c. put the program into production, understand the problem
d. code the program, translate it into machine language C H A P T E R 1 An Overview of Computers and Programming 32 C9275_Chapter01_hr.indd 32 11/25/16 6:08 PM 1. Assume that the following variables contain the values shown: numberBig = 100 numberMedium = 10 numberSmall = 1 wordBig = "Constitution" wordMedium = "Dance" wordSmall = "Toy” For each of the following Boolean expressions, decide whether the statement is true, false, or illegal. a. numberBig > numberSmall b. numberBig < numberMedium
c. numberMedium = numberSmall d. numberBig = wordBig e. numberBig = "Big" f. wordMedium > wordSmall g. wordSmall = "TOY" h. numberBig <= 5 * numberMedium 1 50 i. numberBig >= 2000 j. numberBig > numberMedium + numberSmall k. numberBig > numberMedium AND numberBig < numberSmall l. numberBig = 100 OR numberBig > numberSmall m. numberBig < 10 OR numberSmall > 10 n. numberBig = 300 AND numberMedium = 10 OR numberSmall = 1 o. wordSmall > wordBig p. wordSmall > wordMedium 2. Design a flowchart or pseudocode for a program that accepts two numbers from a user and displays one of the following messages: First is larger, Second is larger, Numbers are equal.
3. Design a flowchart or pseudocode for a program that accepts three numbers from a user and displays a message if the sum of any two numbers equals the third. 4. Cecilia’s Boutique wants several lists of salesperson data. Design a flowchart or pseudocode for the following: a. A program that accepts one salesperson’s ID number, number of items sold in the last month, and total value of the items and displays data message only if the salesperson is a high performer—defined as a person who sells more than 200 items in the month. b. A program that accepts the salesperson’s data and displays a message only if the salesperson is a high performer—defined a person who sells more than 200 items worth at least $1,000 in the month. Programming Exercises
C H A P T E R 4 Making Decisions 170 C9275_Chapter04_hr.indd 170 11/25/16 6:17 PM PROGRAMMING EXERCISES provide opportunities to practice chapter material. These exercises increase in difficulty and allow students to explore logical program- ming concepts. Most exercises can be completed using flowcharts, 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. xx
Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 A S S E S S M E N T Exercises 85 c. Modify the softball program so that it also computes a gross production average (GPA) for each player. A GPA is calculated by multiplying a player’s on-base percentage by 1.8, then adding the player’s slugging percentage, and then dividing by four. 10. Draw the hierarchy chart and design the logic for a program for Arnie’s Appliances. Design a program that prompts the user for a refrigerator model name and the interior height, width, and depth in inches.
Calculate the refrigerator capacity in cubic feet by first multiplying the height, width, and depth to get cubic inches, and then dividing by 1728 (the number of cubic inches in a cubic foot). The program accepts model names continuously until “XXX” is entered. Use named constants where appropriate. Also use modules, including one that displays End of job after the sentinel is entered for the model name. 1. A file named MAINTENANCE02-01.txt is included with your downloadable student files. Assume that this program is a working program in your organization and that it needs modifications as described in the comments (lines that begin with two slashes) at the beginning of the file. Your job is to alter the program to meet the new specifications. 1. Your downloadable files for Chapter 2 include DEBUG02- 01.txt, DEBUG02-02. txt, and DEBUG02-03.txt. Each file starts with some comments
that describe the problem. Comments are lines that begin with two slashes (//). Following the comments, each file contains pseudocode that has one or more bugs you must find and correct. 2. Your downloadable files for Chapter 2 include a file named DEBUG02-04.jpg that contains a flowchart with syntax and/or logical errors. Examine the flowchart, and then find and correct all the bugs. 1. For games to hold your interest, they almost always include some random, unpredictable behavior. For example, a game in which you shoot asteroids loses some of its fun if the asteroids follow the same, predictable path each time you play. Therefore, generating random values is a key component in creating most Performing Maintenance Find the Bugs
Game Zone C9275_Chapter02_hr.indd 85 11/25/16 6:22 PM PERFORMING MAINTENANCE exercises ask students to modify working logic based on new requested specifications. This activity mirrors real-world tasks that students are likely to encounter in their first programming jobs. 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.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
MindTap. These files are also available to instructors through sso.cengage.com. xxi Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Other Features of the Text This edition of the text includes many features to help students become better program- mers and understand the big picture in program development. • Clear explanations. The language and explanations in this book have been refined over eight editions, providing the clearest possible explanations of difficult concepts. • Emphasis on structure. More than its competitors, this book emphasizes structure. Chapter 3 provides an early picture of the major concepts of structured
programming. • Emphasis on modularity. From the second chapter onwards, 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. • Objectives. Each chapter begins with a list of objectives so that 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. • Chapter summaries. Following each chapter is a summary that recaps the program- ming concepts and techniques covered in the chapter. • Key terms. Each chapter lists key terms and their definitions; the list appears in the order that the terms are encountered in the chapter. A glossary at the end of the book lists all the key terms in alphabetical order, along with their
working definitions. MindTap MindTap is a personalized learning experience with relevant assignments that guide stu- dents in analyzing problems, applying what they have learned, and improving their think- ing. MindTap allows instructors to measure skills and outcomes with ease. For instructors: Personalized teaching becomes yours with a learning path that is built with key student objectives. You can control what students see and when they see it. You can use MindTap as-is, or match it to your syllabus by hiding, rearranging, or adding content. For students: A unique learning path of relevant readings, multimedia, and activities is cre- ated to guide you through basic knowledge and comprehension of analysis and application. For both: Better outcomes empower instructors and motivate students with analytics and reports that provide a snapshot of class progress, the time spent
in the course, engagement levels, and completion rates. The MindTap for Programming Logic and Design includes coding labs in C11, Java, and Python, study tools, videos, and interactive quizzing, all integrated into an eReader that includes the full content of the printed text. xxii P R E F A C E MindTap Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Instructor Resources The following teaching tools are available to the instructor for download through our Instructor Companion Site at sso.cengage.com. • 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. • PowerPoint Presentations. This text provides PowerPoint slides to accompany each chapter. Slides are included to guide classroom presentations, and can be made available to students for chapter review, or to print as classroom handouts. •

Computing withBusiness Applications 1 Programming Log

  • 1.
    Computing with Business Applications 1: ProgrammingLogic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning. 2: VBA for Modelers: Developing Decision Support Systems with Microsoft Office Excel; by Albright 5th edition, South Western Cengage Learning. Chapter 1 An Introduction to Programming 1: Programming Logic and Design (comprehensive); by Joyce Farrell, Ninth Edition, Cengage Learning. pages: 1-37 Objectives In this chapter, you will learn about: • Computer systems
  • 2.
    • Simple programlogic • 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 3© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Computer Systems • Computer system • Combination of all the components required to process and store data using a computer • Hardware • Equipment associated with a computer • Software • Computer instructions • Tells the hardware what to do
  • 3.
    • Programs • Instructionswritten by programmers 4© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Computer Systems (continued) • Application software such as word processing, spreadsheets, payroll and inventory, even games • System software such as operating systems like Windows, Linux, or UNIX • Computer hardware and software accomplish three major operations • Input • Data items such as text, numbers, images, and sound • Processing • Calculations and comparisons performed by the central processing unit (CPU) 5© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or
  • 4.
    posted to apublicly accessible website, in whole or in part. Understanding Computer Systems (continued) • Output • Resulting information that is sent to a printer, a monitor, or storage devices after processing • Programming language • Used to write computer instructions • Examples • Visual Basic, C#, C++, or Java • Syntax • Rules governing word usage and punctuation 6© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Computer Systems (continued) • Computer memory • Computer’s temporary, internal storage – random access memory (RAM)
  • 5.
    • Volatile memory– lost when the power is off • Permanent storage devices • Nonvolatile memory • Compiler or interpreter • Translates source code into machine language (binary language) statements called object code • Checks for syntax errors 7© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Simple Program Logic • Program executes or runs • Input will be accepted, some processing will occur, and results will be output • Programs with syntax errors cannot execute • Logical errors • Errors in program logic produce incorrect output • Logic of the computer program • Sequence of specific instructions in specific order • Variable
  • 6.
    • Named memorylocation whose value can vary 8© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Program Development Cycle • Program development cycle • Understand the problem • Plan the logic • Code the program • Use software (a compiler or interpreter) to translate the program into machine language • Test the program • Put the program into production • Maintain the program 9© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 7.
    Understanding the Program DevelopmentCycle (continued) 10 Figure 1-1 The program development cycle © 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Problem • One of the most difficult aspects of programming • Users or end users • People for whom a program is written • Documentation • Supporting paperwork for a program 11© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 8.
    Planning the Logic •Heart of the programming process • Most common planning tools • Flowcharts • Pseudocode • IPO charts (input, processing, and output) • TOE charts (tasks, objects, and events) • Desk-checking • Walking through a program’s logic on paper before you actually write the program 12© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Coding the Program • Hundreds of programming languages available • Choose based on features • Similar in their basic capabilities • Easier than the planning step
  • 9.
    13© 2013 CengageLearning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using Software to Translate the Program into Machine Language • Translator program • Compiler or interpreter • Changes the programmer’s English-like high-level programming language into the low-level machine language • Syntax error • Misuse of a language’s grammar rules • Programmer corrects listed syntax errors • Might need to recompile the code several times 14© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using Software to Translate the Program into Machine Language (continued)
  • 10.
    Figure 1-2 Creatingan executable program 15© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Testing the Program • Logical error • Results when a syntactically correct statement, but the wrong one for the current context, is used • Test • Execute the program with some sample data to see whether the results are logically correct • Debugging is the process of finding and correcting program errors • Programs should be tested with many sets of data 16© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 11.
    Putting the Programinto Production • Process depends on program’s purpose • May take several months • Conversion • The entire set of actions an organization must take to switch over to using a new program or set of programs 17© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Maintaining the Program • Maintenance • Making changes after the program is put into production • Common first programming job • Maintaining previously written programs • Make changes to existing programs • Repeat the development cycle 18© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 12.
    Using Pseudocode Statements andFlowchart Symbols • Pseudocode • English-like representation of the logical steps it takes to solve a problem • Flowchart • Pictorial representation of the logical steps it takes to solve a problem 19© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Writing Pseudocode • Pseudocode representation of a number-doubling problem start input myNumber set myAnswer = myNumber * 2 output myAnswer stop 20© 2013 Cengage Learning. All Rights Reserved. This edition
  • 13.
    is intended foruse outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Writing Pseudocode (continued) • Programmers preface their pseudocode with a beginning statement like start and end it with a terminating statement like stop • Flexible planning tool 21© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Drawing Flowcharts • Create a flowchart • Draw geometric shapes that contain the individual statements • Connect shapes with arrows • Input symbol • Indicates input operation • Parallelogram • Processing symbol
  • 14.
    • Contains processingstatements such as arithmetic • Rectangle 22© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Drawing Flowcharts (continued) • Output symbol • Represents output statements • Parallelogram • Flowlines • Arrows that connect steps • Terminal symbols • Start/stop symbols • Shaped like a racetrack • Also called lozenges 23© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 15.
    Drawing Flowcharts (continued) Figure1-6 Flowchart and pseudocode of program that doubles a number 24© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Repeating Instructions • Program in Figure 1-6 only works for one number • Not feasible to run the program over and over 10,000 times • Not feasible to add 10,000 lines of code to a program • Create a loop (repetition of a series of steps) instead • Avoid an infinite loop (repeating flow of logic that never ends) 25© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Repeating Instructions (continued)
  • 16.
    Figure 1-8 Flowchartof infinite number-doubling program 26© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using a Sentinel Value to End a Program • Making a decision • Testing a value • Decision symbol • Diamond shape • Dummy value • Data-entry value that the user will never need • Sentinel value • eof (“end of file”) • Marker at the end of a file that automatically acts as a sentinel 27© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using a Sentinel Value to End
  • 17.
    a Program (continued) Figure1-9 Flowchart of number-doubling program with sentinel value of 0 28© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Using a Sentinel Value to End a Program (continued) Figure 1-10 Flowchart using eof 29© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Programming and User Environments • Many options for programming and user environments • Planning • Flowchart • Pseudocode • Coding
  • 18.
    • Text editors •Executing • Input from keyboard, mouse, microphone • Outputting • Text, images, sound 30© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding Programming Environments • Use a keyboard to type program statements into an editor • Plain text editor • Similar to a word processor but without as many features • Text editor that is part of an integrated development environment (IDE) • Software package that provides an editor, a compiler, and other programming tools 31© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 19.
    Understanding Programming Environments (continued) Figure1-12 A C# number-doubling program in Visual Studio 32© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding User Environments • Command line • Location on your computer screen where you type text entries to communicate with the computer’s operating system • Graphical user interface (GUI) • Allows users to interact with a program in a graphical environment 33© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding User Environments (continued)
  • 20.
    Figure 1-13 Executinga number-doubling program in a command-line environment 34© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding User Environments (continued) Figure 1-14 Executing a number-doubling program in a GUI environment 35© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Evolution of Programming Models • People have been writing modern computer programs since the 1940s • Newer programming languages • Look much more like natural language
  • 21.
    • Are easierto use • Create self-contained modules or program segments that can be pieced together in a variety of ways 36© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Understanding the Evolution of Programming Models (continued) • Major models or paradigms used by programmers • Procedural programming • Focuses on the procedures that programmers create • Object-oriented programming • Focuses on objects, or “things,” and describes their features (or attributes) and their behaviors • This text • Focuses on procedural programming techniques 37© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
  • 22.
    Summary • Hardware andsoftware accomplish input, processing, and output • Logic must be developed correctly • Logical errors are much more difficult to locate than syntax errors • Use flowcharts, pseudocode, IPO charts, and TOE charts to plan the logic • Avoid infinite loops by testing for a sentinel value • Use a text editor or an IDE to enter your program statements 38© 2013 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part. Option #1:Organizational Problem or Opportunity Think about a specific problem or opportunity in your organization, your community, your professional association, or your social organization. Write a paper analyzing the three different research methods presented in this module. Compare each and determine which would be the most appropriate method to utilize in your research study.
  • 23.
    The methods :qualitative, quantitative, or mixed methods COMPREHENSIVE Joyce Farrell Ninth Edition Logic & Design Programming N in th E d itio n C O M P R E H
  • 24.
  • 25.
    To register oraccess your online learning solution or purchase materials for your course, visit www.cengagebrain.com. Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 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. Untitled-1.indd 1 10/12/16 3:56 PM Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 PROGRAMMING LOGIC
  • 26.
    AND DESIGN COMPREHENSIVE Copyright 2018Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 PROGRAMMING LOGIC AND DESIGN COMPREHENSIVE J O Y C E FA R R E L L N I N T H E D I T I O N Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United St ates Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Programming Logic and Design, Comprehensive
  • 27.
    Ninth Edition Joyce Farrell SeniorProduct Director: Kathleen McMahon Product Team Leader: Kristin McNary Associate Product Manager: Kate Mason Senior Content Developer: Alyssa Pratt Senior Content Project Manager: Jennifer Feltri-George Manufacturing Planner: Julio Esperas Art Director: Diana Graham Production Service/Composition: SPi Global Cover Photo: Colormos/Photodisc/Getty Images Printed in the United States of America Print Number: 01 Print Year: 2018 © 2018 Cengage Learning® ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced or distributed in any form or by any means, except as permitted by U.S. copyright law, without the prior written permission of the copyright owner. For product information and technology assistance, contact us at
  • 28.
    Cengage Learning Customer& Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Further permissions questions can be emailed to [email protected] Cengage Learning 20 Channel Center Street Boston, MA 02210 USA Unless otherwise noted all items © Cengage Learning. Cengage Learning is a leading provider of customized learning solutions with employees residing in nearly 40 different coun- tries and sales in more than 125 countries around the world. Find your local representative at www.cengage.com. Cengage Learning products are represented in Canada by Nelson Education, Ltd. To learn more about Cengage Learning Solution s, visit www.cengage.com Purchase any of our products at your local college store or at our preferred online store www.cengagebrain.com
  • 29.
    Library of CongressControl Number: 2016959742 ISBN: 978-1-337-10207-0 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Brief Contents P re f a c e � � � � � � � � � � � � � � � � x v i C H A P T E R 1 A n O v e r v i e w o f C o m p u t e r s a n d P ro g r a m m i n g � 1 C H A P T E R 2 E l e m e n t s o f H i g h - Q u a l i t y P ro g r a m s � � � � � 3 8 C H A P T E R 3 U n d e r s t a n d i n g S t r u c t u re � � � � � � � � � � 8 7 C H A P T E R 4 M a k i n g D e c i s i o n s � � � � � � � � � � � � 1 2 4 C H A P T E R 5 L o o p i n g � � � � � � � �
  • 30.
    � � �� � � � � 1 7 6 C H A P T E R 6 A r r a y s � � � � � � � � � � � � � � � � � 2 2 7 C H A P T E R 7 F i l e H a n d l i n g a n d A p p l i c a t i o n s � � � � � � 2 7 2 C H A P T E R 8 A d v a n c e d D a t a H a n d l i n g C o n c e p t s � � � � � 3 2 1 C H A P T E R 9 A d v a n c e d M o d u l a r i z a t i o n Te c h n i q u e s � � � 3 6 6 C H A P T E R 1 0 O b j e c t - O r i e n t e d P ro g r a m m i n g � � � � � � � 4 2 0 C H A P T E R 1 1 M o re O b j e c t - O r i e n t e d P ro g r a m m i n g C o n c e p t s � � � � � � � � � � � � � � � 4 6 4 C H A P T E R 1 2 E v e n t - D r i v e n G U I P ro g r a m m i n g , M u l t i t h re a d i n g , a n d A n i m a t i o n � � � � � � 5 0 7 A P P E N D I X A U n d e r s t a n d i n g N u m b e r i n g S y s t e m s a n d C o m p u t e r C o d e s � � � � � � � �
  • 31.
    � � 53 9 A P P E N D I X B S o l v i n g D i f f i c u l t S t r u c t u r i n g P ro b l e m s � � � 5 4 7 G l o s s a r y � � � � � � � � � � � � � � � � 5 5 6 I n d e x � � � � � � � � � � � � � � � � � 5 7 1 v Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Contents P re f a c e � � � � � � � � � � � � �
  • 32.
    � � �x v i C H A P T E R 1 A n O v e r v i e w o f C o m p u t e r s a n d P ro g r a m m i n g � 1 Understanding Computer Systems � � � � � � � � � � � � � 2 Understanding Simple Program Logic � � � � � � � � � � � � 5 Understanding the Program Development Cycle � � � � � � � 8 Understanding the Problem � � � � � � � � � � � � � � � � 8 Planning the Logic � � � � � � � � � � � � � � � � � � � 10 Coding the Program � � � � � � � � � � � � � � � � � � 10 Using Software to Translate the Program into Machine Language � � � � � � � � � � � � � � � � � � � � � � 11 Testing the Program � � � � � � � � � � � � � � � � � � 12 Putting the Program into Production� � � � � � � � �
  • 33.
    � � 13 Maintainingthe Program � � � � � � � � � � � � � � � � 14 Using Pseudocode Statements and Flowchart Symbols � � � 15 Writing Pseudocode � � � � � � � � � � � � � � � � � � 15 Drawing Flowcharts � � � � � � � � � � � � � � � � � � 17 Repeating Instructions � � � � � � � � � � � � � � � � � 19 Using a Sentinel Value to End a Program � � � � � � � � � 20 Understanding Programming and User Environments � � � � 23 Understanding Programming Environments � � � � � � � 23 Understanding User Environments � � � � � � � � � � � 25 Understanding the Evolution of Programming Models � � � � 27
  • 34.
    Chapter Summary �� � � � � � � � � � � � � � � � � � � 28 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 29 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 32 C H A P T E R 2 E l e m e n t s o f H i g h - Q u a l i t y P ro g r a m s � � � � � 3 8 Declaring and Using Variables and Constants � � � � � � � 39 Understanding Data Types � � � � � � � � � � � � � � � 39 Understanding Unnamed, Literal Constants � � � � � � � 39 Working with Variables � � � � � � � � � � � � � � � � � 40 Understanding a Declaration’s Data Type � � � � � � � � 41 vii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
  • 35.
    WCN 02-300 Understanding aDeclaration’s Identifier � � � � � � � � � 42 Assigning Values to Variables � � � � � � � � � � � � � � 45 Declaring Named Constants � � � � � � � � � � � � � � 46 Performing Arithmetic Operations � � � � � � � � � � � � � 47 The Integer Data Type � � � � � � � � � � � � � � � � � 50 Understanding the Advantages of Modularization � � � � � � 51 Modularization Provides Abstraction� � � � � � � � � � � 52 Modularization Helps Multiple Programmers to Work on a Problem � � � � � � � � � � � � � � � � � � � � 53 Modularization Allows You to Reuse Work � � � � � �
  • 36.
    � � 53 Modularizinga Program � � � � � � � � � � � � � � � � 54 Declaring Variables and Constants within Modules � � � � � 58 Understanding the Most Common Configuration for Mainline Logic � � � � � � � � � � � � � � � � � � � 60 Creating Hierarchy Charts � � � � � � � � � � � � � � � 64 Features of Good Program Design � � � � � � � � � � � � 66 Using Program Comments � � � � � � � � � � � � � � � 67 Choosing Identifiers � � � � � � � � � � � � � � � � � � 69 Designing Clear Statements � � � � � � � � � � � � � � 71 Writing Clear Prompts and Echoing Input � � � � � � � � 72 Maintaining Good Programming Habits � � � � � � �
  • 37.
    � � �74 Chapter Summary � � � � � � � � � � � � � � � � � � � � 75 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 76 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 79 C H A P T E R 3 U n d e r s t a n d i n g S t r u c t u re � � � � � � � � � � 8 7 The Disadvantages of Unstructured Spaghetti Code � � � � 88 Understanding the Three Basic Structures � � � � � � � � � 90 The Sequence Structure � � � � � � � � � � � � � � � � 90 The Selection Structure � � � � � � � � � � � � � � � � 91 The Loop Structure � � � � � � � � � � � � � � � � � � 92 Combining Structures � � � � � � � � � � � � � � � � � 93
  • 38.
    Using a PrimingInput to Structure a Program � � � � � � � 99 Understanding the Reasons for Structure � � � � � � � � � 106 Recognizing Structure � � � � � � � � � � � � � � � � � � 107 Structuring and Modularizing Unstructured Logic � � � � � � 110 Chapter Summary � � � � � � � � � � � � � � � � � � � � 115 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 115 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 117 C O N T E N T S viii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300
  • 39.
    C H AP T E R 4 M a k i n g D e c i s i o n s � � � � � � � � � � � � 1 2 4 The Selection Structure � � � � � � � � � � � � � � � � � 125 Using Relational Comparison Operators � � � � � � � � � � 129 Avoiding a Common Error with Relational Operators � � � 133 Understanding AND Logic � � � � � � � � � � � � � � � � 134 Nesting AND Decisions for Efficiency � � � � � � � � � � 137 Using the AND Operator � � � � � � � � � � � � � � � � 139 Avoiding Common Errors in an AND Selection � � � � � � 141 Understanding OR Logic � � � � � � � � � � � � � � � � � 143 Writing OR Selections for Efficiency � � � � � � � � � � � 145 Using the OR Operator � � � � � � � � � � � � �
  • 40.
    � � �� 147 Avoiding Common Errors in an OR Selection � � � � � � � 147 Understanding NOT Logic � � � � � � � � � � � � � � � � 153 Avoiding a Common Error in a NOT Expression � � � � � � 154 Making Selections within Ranges � � � � � � � � � � � � � 155 Avoiding Common Errors When Using Range Checks � � � 157 Understanding Precedence When Combining AND and OR Operators � � � � � � � � � � � � � � � � � 160 Understanding the case Structure � � � � � � � � � � � � 163 Chapter Summary � � � � � � � � � � � � � � � � � � � � 165 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 166 Exercises � � � � � � � � � � � � � � � � �
  • 41.
    � � �� � � � 167 C H A P T E R 5 L o o p i n g � � � � � � � � � � � � � � � � 1 7 6 Appreciating the Advantages of Looping � � � � � � � � � � 177 Using a Loop Control Variable � � � � � � � � � � � � � � 179 Using a Definite Loop with a Counter � � � � � � � � � � 179 Using an Indefinite Loop with a Sentinel Value � � � � � � 181 Understanding the Loop in a Program’s Mainline Logic � � 183 Nested Loops � � � � � � � � � � � � � � � � � � � � � � 185 Avoiding Common Loop Mistakes � � � � � � � � � � � � � 190 Mistake: Failing to Initialize the Loop Control Variable � � � 190 Mistake: Neglecting to Alter the Loop Control Variable � �
  • 42.
    191 Mistake: Using theWrong Type of Comparison When Testing the Loop Control Variable � � � � � � � � � � � 192 Mistake: Including Statements Inside the Loop Body that Belong Outside the Loop � � � � � � � � � � � � � 194 Using a for Loop � � � � � � � � � � � � � � � � � � � � 199 Using a Posttest Loop � � � � � � � � � � � � � � � � � � 201 Recognizing the Characteristics Shared by Structured Loops � � � � � � � � � � � � � � � � � � 203 ix C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300
  • 43.
    Common Loop Applications� � � � � � � � � � � � � � � 205 Using a Loop to Accumulate Totals � � � � � � � � � � � 205 Using a Loop to Validate Data � � � � � � � � � � � � � 209 Limiting a Reprompting Loop � � � � � � � � � � � � � � 209 Validating a Data Type � � � � � � � � � � � � � � � � � 212 Validating Reasonableness and Consistency of Data � � � 213 Comparing Selections and Loops � � � � � � � � � � � � � 214 Chapter Summary � � � � � � � � � � � � � � � � � � � � 218 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 218 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 220
  • 44.
    C H AP T E R 6 A r r a y s � � � � � � � � � � � � � � � � � 2 2 7 Storing Data in Arrays � � � � � � � � � � � � � � � � � � 228 How Arrays Occupy Computer Memory � � � � � � � � � 228 How an Array Can Replace Nested Decisions� � � � � � � � 231 Using Constants with Arrays � � � � � � � � � � � � � � � 238 Using a Constant as the Size of an Array � � � � � � � � 238 Using Constants as Array Element Values � � � � � � � � 239 Using a Constant as an Array Subscript � � � � � � � � � 239 Searching an Array for an Exact Match � � � � � � � � � � 240 Using Parallel Arrays � � � � � � � � � � � � � � � � � � 244
  • 45.
    Improving Search Efficiency� � � � � � � � � � � � � � 248 Searching an Array for a Range Match� � � � � � � � � � � 250 Remaining within Array Bounds � � � � � � � � � � � � � � 255 Understanding Array Size � � � � � � � � � � � � � � � 255 Understanding Subscript Bounds � � � � � � � � � � � � 255 Using a for Loop to Process an Array � � � � � � � � � � 258 Chapter Summary � � � � � � � � � � � � � � � � � � � � 260 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 261 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 261 C H A P T E R 7 F i l e H a n d l i n g a n d A p p l i c a t i o n s � � � � � � 2 7 2 Understanding Computer Files � � � � � � � � � �
  • 46.
    � � �� 273 Organizing Files � � � � � � � � � � � � � � � � � � � � 274 Understanding the Data Hierarchy� � � � � � � � � � � � � 275 Performing File Operations � � � � � � � � � � � � � � � � 277 Declaring a File Identifier� � � � � � � � � � � � � � � � 277 Opening a File � � � � � � � � � � � � � � � � � � � � 278 Reading Data from a File and Processing It � � � � � � � 278 Writing Data to a File � � � � � � � � � � � � � � � � � 281 Closing a File � � � � � � � � � � � � � � � � � � � � � 281 x C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May
  • 47.
    not be copied,scanned, or duplicated, in whole or in part. WCN 02-300 A Program that Performs File Operations � � � � � � � � 282 Understanding Control Break Logic � � � � � � � � � � � � 285 Merging Sequential Files � � � � � � � � � � � � � � � � � 290 Master and Transaction File Processing � � � � � � � � � � 299 Random Access Files � � � � � � � � � � � � � � � � � � 308 Chapter Summary � � � � � � � � � � � � � � � � � � � � 309 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 310 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 312 C H A P T E R 8 A d v a n c e d D a t a H a n d l i n g C o n c e p t s � � � � � 3 2 1
  • 48.
    Understanding the Needfor Sorting Data � � � � � � � � � 322 Using the Bubble Sort Algorithm � � � � � � � � � � � � � 324 Understanding Swapping Values � � � � � � � � � � � � 324 Understanding the Bubble Sort � � � � � � � � � � � � � 325 Sorting Multifield Records � � � � � � � � � � � � � � � � 340 Sorting Data Stored in Parallel Arrays � � � � � � � � � � 340 Sorting Records as a Whole � � � � � � � � � � � � � � 341 Other Sorting Algorithms � � � � � � � � � � � � � � � � � 342 Using Multidimensional Arrays � � � � � � � � � � � � � � 345 Using Indexed Files and Linked Lists � � � � � � � � � � � 351 Using Indexed Files � � � � � � � � � � � � � �
  • 49.
    � � �� 352 Using Linked Lists � � � � � � � � � � � � � � � � � � � 353 Chapter Summary � � � � � � � � � � � � � � � � � � � � 356 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 357 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 358 C H A P T E R 9 A d v a n c e d M o d u l a r i z a t i o n Te c h n i q u e s � � � 3 6 6 The Parts of a Method � � � � � � � � � � � � � � � � � � 367 Using Methods with no Parameters � � � � � � � � � � � � 368 Creating Methods that Require Parameters � � � � � � � � 371 Creating Methods that Require Multiple Parameters � � � � 377 Creating Methods that Return a Value � � � � � � � � � � � 379
  • 50.
    Using an IPOChart � � � � � � � � � � � � � � � � � � 384 Passing an Array to a Method � � � � � � � � � � � � � � 386 Overloading Methods � � � � � � � � � � � � � � � � � � 394 Avoiding Ambiguous Methods � � � � � � � � � � � � � � 397 Using Predefined Methods � � � � � � � � � � � � � � � � 400 Method Design Issues: Implementation Hiding, Cohesion, and Coupling � � � � � � � � � � � � � � � � � � � � � 402 Understanding Implementation Hiding � � � � � � � � � � 402 Increasing Cohesion � � � � � � � � � � � � � � � � � � 403 Reducing Coupling� � � � � � � � � � � � � � � � � � � 404 Understanding Recursion � � � � � � � � � � � � � � � � � 405
  • 51.
    xi C O NT E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Chapter Summary � � � � � � � � � � � � � � � � � � � � 410 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 411 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 412 C H A P T E R 1 0 O b j e c t - O r i e n t e d P ro g r a m m i n g � � � � � � � 4 2 0 Principles of Object-Oriented Programming � � � � � � � � 421 Classes and Objects � � � � � � � � � � � � � � � � � � 421
  • 52.
    Polymorphism � �� � � � � � � � � � � � � � � � � � � 424 Inheritance � � � � � � � � � � � � � � � � � � � � � � 426 Encapsulation � � � � � � � � � � � � � � � � � � � � � 426 Defining Classes and Creating Class Diagrams � � � � � � � 428 Creating Class Diagrams � � � � � � � � � � � � � � � � 430 The Set Methods � � � � � � � � � � � � � � � � � � � 433 The Get Methods � � � � � � � � � � � � � � � � � � � 434 Work Methods� � � � � � � � � � � � � � � � � � � � � 435 Understanding Public and Private Access � � � � � � � � � 437 Organizing Classes � � � � � � � � � � � � � � � � � � � 440 Understanding Instance Methods � � � � � � � � � � � � � 441 Understanding Static Methods � � � � � � � � � �
  • 53.
    � � �� 447 Using Objects � � � � � � � � � � � � � � � � � � � � � � 448 Passing an Object to a Method � � � � � � � � � � � � � 449 Returning an Object from a Method � � � � � � � � � � � 450 Using Arrays of Objects � � � � � � � � � � � � � � � � 453 Chapter Summary � � � � � � � � � � � � � � � � � � � � 455 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 456 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 458 C H A P T E R 1 1 M o re O b j e c t - O r i e n t e d P ro g r a m m i n g C o n c e p t s � � � � � � � � � � � � � � � 4 6 4 Understanding Constructors � � � � � � � � � � � � � � � 465
  • 54.
    Default Constructors �� � � � � � � � � � � � � � � � 466 Non-default Constructors � � � � � � � � � � � � � � � � 468 Overloading Instance Methods and Constructors � � � � � 469 Understanding Destructors � � � � � � � � � � � � � � � � 472 Understanding Composition � � � � � � � � � � � � � � � 474 Understanding Inheritance � � � � � � � � � � � � � � � � 475 Understanding Inheritance Terminology � � � � � � � � � 478 Accessing Private Fields and Methods of a Parent Class� � � � � � � � � � � � � � � � � � � 481 Overriding Parent Class Methods in a Child Class� � � � � 486 xii
  • 55.
    C O NT E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Using Inheritance to Achieve Good Software Design � � � 486 An Example of Using Predefined Classes: Creating GUI Objects � � � � � � � � � � � � � � � � � � 487 Understanding Exception Handling � � � � � � � � � � � � 488 Drawbacks to Traditional Error-Handling Techniques � � � 489 The Object-Oriented Exception-Handling Model � � � � � � 491 Using Built-in Exceptions and Creating Your Own Exceptions � � � � � � � � � � � � � � � � � 493
  • 56.
    Reviewing the Advantagesof Object-Oriented Programming 494 Chapter Summary � � � � � � � � � � � � � � � � � � � � 495 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 496 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 497 C H A P T E R 1 2 E v e n t - D r i v e n G U I P ro g r a m m i n g , M u l t i t h re a d i n g , a n d A n i m a t i o n � � � � � � 5 0 7 Understanding Event-Driven Programming � � � � � � � � � 508 User-Initiated Actions and GUI Components � � � � � � � � 511 Designing Graphical User Interfaces � � � � � � � � � � � 514 The Interface Should Be Natural and Predictable � � � � � 514 The Interface Should Be Attractive, Easy to Read, and Nondistracting � � � � � � � � � � � � � �
  • 57.
    � � �� 515 To Some Extent, It’s Helpful If the User Can Customize Your Applications� � � � � � � � � � � � � � � � � � � 516 The Program Should Be Forgiving � � � � � � � � � � � � 516 The GUI Is Only a Means to an End � � � � � � � � � � � 516 Developing an Event-Driven Application � � � � � � � � � � 517 Creating Wireframes � � � � � � � � � � � � � � � � � � 518 Creating Storyboards � � � � � � � � � � � � � � � � � 518 Defining the Storyboard Objects in an Object Dictionary� � 519 Defining Connections Between the User Screens � � � � � 520 Planning the Logic � � � � � � � � � � � � � � � � � � � 520 Understanding Threads and Multithreading � � � � � � � � � 525
  • 58.
    Creating Animation �� � � � � � � � � � � � � � � � � � 528 Chapter Summary � � � � � � � � � � � � � � � � � � � � 531 Key Terms � � � � � � � � � � � � � � � � � � � � � � � 532 Exercises � � � � � � � � � � � � � � � � � � � � � � � � 533 xiii C O N T E N T S Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 A P P E N D I X A U n d e r s t a n d i n g N u m b e r i n g S y s t e m s a n d C o m p u t e r C o d e s � � � � � � � � � � 5 3 9 A P P E N D I X B S o l v i n g D i f f i c u l t S t r u c t u r i
  • 59.
    n g Pro b l e m s � � � 5 4 7 G l o s s a r y � � � � � � � � � � � � � � � � 5 5 6 I n d e x � � � � � � � � � � � � � � � � � 5 7 1 C O N T E N T S xiv Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Preface
  • 60.
    Programming Logic andDesign, Comprehensive, Ninth Edition, provides the beginning programmer with a guide to developing structured program logic. This textbook assumes no programming language experience. The writing is nontechnical and emphasizes good programming practices. The examples are business examples; they do not assume mathematical background beyond high school business math. Additionally, the examples illustrate one or two major points; they do not contain so many features that students become lost following irrelevant and extraneous details. The examples in this book have been created to provide students with a sound background in logic, no matter what programming languages they eventually use to write programs. This book can be used in a stand-alone logic course that students take as a prerequisite to a programming course, or as a companion book to an introductory programming text using any programming language.
  • 61.
    Organization and Coverage ProgrammingLogic and Design, Comprehensive, Ninth 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 provides students a solid foundation for thinking in a structured way. Chapters 4, 5, and 6 explore the intricacies of decision making,
  • 62.
    looping, and array manipulation.Chapter 7 provides details of file handling so that 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 con- cepts and terminology used in object-oriented programming. Students learn about classes, objects, instance and static class members, constructors, destructors, inheritance, and the xvi Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 advantages of object-oriented thinking. Chapter 12 explores some additional object-oriented programming issues: event-driven GUI
  • 63.
    programming, multithreading, and animation. Twoappendices instruct students on working with numbering systems and providing structure for large programs. 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: • It is written and designed to be non-language specific. The logic used in this book can be applied to any programming language.
  • 64.
    • The examplesare everyday business examples: no special knowledge of mathematics, accounting, or other disciplines is assumed. • The concept of structure is covered earlier than in many other texts. Students are exposed to structure naturally, so that they will automatically create properly designed programs. • Text explanation is interspersed with both flowcharts and pseudocode so that students can become comfortable with these logic development tools and understand their inter- relationship. Screen shots of running programs also are included, providing students with a clear and concrete image of the programs’ execution. • 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 a program. P R E F A C EOrganization and Coverage
  • 65.
    xvii Copyright 2018 CengageLearning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Features This text focuses on helping students become better programmers, as well as helping them understand the big picture in program development through a variety of features. Each chapter begins with objectives and ends with a list of key terms and a summary; these useful features will help students organize their learning experience. Figure 3-17 Structured but incorrect solution to the number- doubling problem input originalNumber
  • 66.
    Declarations num originalNumber num calculatedAnswer start stop noteof? Yes No output calculatedAnswer calculatedAnswer = originalNumber * 2 Don’t Do It This logic is structured, but flawed. When the user inputs the eof value, it will incorrectly be doubled and output.
  • 67.
    tested. Instead, aresult is calculated and displayed one last time before the loop-controlling test is made again. If the program was written to recognize eof when originalNumber is 0, then an extraneous answer of 0 will be displayed before the program ends. Depending on the language you are using and on the type of input being used, the results might be worse: The program might terminate by displaying an error message or the value output might be indecipherable garbage. In any case, this last output is superfluous—no value should be doubled and output after the eof condition is encountered. As a general rule, a program-ending test should always come immediately after an input statement because that’s the earliest point at which it can be evaluated. Therefore, the best solution to the number-doubling problem remains the one shown in Figure 3-16—the structured solution containing the priming input statement. Using a Priming Input to Structure a Program
  • 68.
    105 C9275_Chapter03_hr.indd 105 11/15/162:07 PM and illustrations provide learning experience. the reader with a visual FLOWCHARTS, figures, 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. xviii Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
  • 69.
    WCN 02-300 F EA T U R E S • The instruction myAnswer = myNumber * 2 is an example of a processing operation. In most programming languages, an asterisk is used to indicate multiplication, so this instruction means “Change the value of the memory location myAnswer to equal the value at the memory location myNumber times two.” Mathematical operations are not the only kind of processing operations, but they are very typical. As with input operations, the type of hardware used for processing is irrelevant—after you write a program, it can be used on computers of different brand names, sizes, and speeds. • In the number-doubling program, the output myAnswer instruction is an example of an output operation. Within a particular program, this statement could cause the output
  • 70.
    to appear onthe monitor (which might be a flat-panel plasma screen or a smartphone display), or the output could go to a printer (which could be laser or ink-jet), or the output could be written to a disk or DVD. The logic of the output process is the same no matter what hardware device you use. When this instruction executes, the value stored in memory at the location named myAnswer is sent to an output device. (The output value also remains in computer memory until something else is stored at the same memory location or power is lost.) 7 Understanding Simple Program Logic Watch the video A Simple Program. Computer memory consists of millions of numbered locations where data can be stored. The memory location of myNumber has a specific numeric address, but when you write programs, you seldom need to be concerned with the value of the memory address; instead,
  • 71.
    you use theeasy-to-remember name you created. Computer programmers often refer to memory addresses using hexadecimal notation, or base 16. Using this system, they might use a value like 42FF01A to refer to a memory address. Despite the use of letters, such an address is still a number. Appendix A contains information about the hexadecimal numbering system. The false statement is #1. A program with syntax errors cannot execute; a program with no syntax errors can execute, but might produce incorrect results. TWO TRUTHS & A LIE Understanding Simple Program Logic 1. A program with syntax errors can execute but might produce incorrect results. 2. Although the syntax of programming languages differs, the same program logic can be expressed in different languages.
  • 72.
    3. Most simplecomputer programs include steps that perform input, processing, and output. C9275_Chapter01_hr.indd 7 8/23/16 7:50 PMTWO 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 MindTap. VIDEO LESSONS help explain important chapter concepts. Videos are part of the text’s MindTap. NOTES provide additional information—
  • 73.
    for example, another locationin the book that expands on a topic, or a common error to avoid. xix Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Assessment Exercises Review Questions 1. Computer programs also are known as ____________. a. data b. hardware c. software
  • 74.
    d. information 2. Themajor computer operations include ____________. a. input, processing, and output b. hardware and software c. sequence and looping d. spreadsheets, word processing, and data communications 3. Visual Basic, C++, and Java are all examples of computer ____________. a. operating systems b. programming languages c. hardware d. machine languages 4. A programming language’s rules are its ____________. a. syntax b. logic c. format d. options
  • 75.
    5. The mostimportant task of a compiler or interpreter is to ____________. a. create the rules for a programming language b. translate English statements into a language such as Java c. translate programming language statements into machine language d. execute machine language programs to perform useful tasks 6. Which of the following is temporary, internal storage? a. CPU b. hard disk c. keyboard d. memory 7. Which of the following pairs of steps in the programming process is in the correct order? a. code the program, plan the logic b. test the program, translate it into machine language c. put the program into production, understand the problem
  • 76.
    d. code theprogram, translate it into machine language C H A P T E R 1 An Overview of Computers and Programming 32 C9275_Chapter01_hr.indd 32 11/25/16 6:08 PM 1. Assume that the following variables contain the values shown: numberBig = 100 numberMedium = 10 numberSmall = 1 wordBig = "Constitution" wordMedium = "Dance" wordSmall = "Toy” For each of the following Boolean expressions, decide whether the statement is true, false, or illegal. a. numberBig > numberSmall b. numberBig < numberMedium
  • 77.
    c. numberMedium =numberSmall d. numberBig = wordBig e. numberBig = "Big" f. wordMedium > wordSmall g. wordSmall = "TOY" h. numberBig <= 5 * numberMedium 1 50 i. numberBig >= 2000 j. numberBig > numberMedium + numberSmall k. numberBig > numberMedium AND numberBig < numberSmall l. numberBig = 100 OR numberBig > numberSmall m. numberBig < 10 OR numberSmall > 10 n. numberBig = 300 AND numberMedium = 10 OR numberSmall = 1 o. wordSmall > wordBig p. wordSmall > wordMedium 2. Design a flowchart or pseudocode for a program that accepts two numbers from a user and displays one of the following messages: First is larger, Second is larger, Numbers are equal.
  • 78.
    3. Design aflowchart or pseudocode for a program that accepts three numbers from a user and displays a message if the sum of any two numbers equals the third. 4. Cecilia’s Boutique wants several lists of salesperson data. Design a flowchart or pseudocode for the following: a. A program that accepts one salesperson’s ID number, number of items sold in the last month, and total value of the items and displays data message only if the salesperson is a high performer—defined as a person who sells more than 200 items in the month. b. A program that accepts the salesperson’s data and displays a message only if the salesperson is a high performer—defined a person who sells more than 200 items worth at least $1,000 in the month. Programming Exercises
  • 79.
    C H AP T E R 4 Making Decisions 170 C9275_Chapter04_hr.indd 170 11/25/16 6:17 PM PROGRAMMING EXERCISES provide opportunities to practice chapter material. These exercises increase in difficulty and allow students to explore logical program- ming concepts. Most exercises can be completed using flowcharts, 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. xx
  • 80.
    Copyright 2018 CengageLearning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 A S S E S S M E N T Exercises 85 c. Modify the softball program so that it also computes a gross production average (GPA) for each player. A GPA is calculated by multiplying a player’s on-base percentage by 1.8, then adding the player’s slugging percentage, and then dividing by four. 10. Draw the hierarchy chart and design the logic for a program for Arnie’s Appliances. Design a program that prompts the user for a refrigerator model name and the interior height, width, and depth in inches.
  • 81.
    Calculate the refrigerator capacityin cubic feet by first multiplying the height, width, and depth to get cubic inches, and then dividing by 1728 (the number of cubic inches in a cubic foot). The program accepts model names continuously until “XXX” is entered. Use named constants where appropriate. Also use modules, including one that displays End of job after the sentinel is entered for the model name. 1. A file named MAINTENANCE02-01.txt is included with your downloadable student files. Assume that this program is a working program in your organization and that it needs modifications as described in the comments (lines that begin with two slashes) at the beginning of the file. Your job is to alter the program to meet the new specifications. 1. Your downloadable files for Chapter 2 include DEBUG02- 01.txt, DEBUG02-02. txt, and DEBUG02-03.txt. Each file starts with some comments
  • 82.
    that describe the problem.Comments are lines that begin with two slashes (//). Following the comments, each file contains pseudocode that has one or more bugs you must find and correct. 2. Your downloadable files for Chapter 2 include a file named DEBUG02-04.jpg that contains a flowchart with syntax and/or logical errors. Examine the flowchart, and then find and correct all the bugs. 1. For games to hold your interest, they almost always include some random, unpredictable behavior. For example, a game in which you shoot asteroids loses some of its fun if the asteroids follow the same, predictable path each time you play. Therefore, generating random values is a key component in creating most Performing Maintenance Find the Bugs
  • 83.
    Game Zone C9275_Chapter02_hr.indd 8511/25/16 6:22 PM PERFORMING MAINTENANCE exercises ask students to modify working logic based on new requested specifications. This activity mirrors real-world tasks that students are likely to encounter in their first programming jobs. 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.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
  • 84.
    MindTap. These filesare also available to instructors through sso.cengage.com. xxi Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Other Features of the Text This edition of the text includes many features to help students become better program- mers and understand the big picture in program development. • Clear explanations. The language and explanations in this book have been refined over eight editions, providing the clearest possible explanations of difficult concepts. • Emphasis on structure. More than its competitors, this book emphasizes structure. Chapter 3 provides an early picture of the major concepts of structured
  • 85.
    programming. • Emphasis onmodularity. From the second chapter onwards, 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. • Objectives. Each chapter begins with a list of objectives so that 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. • Chapter summaries. Following each chapter is a summary that recaps the program- ming concepts and techniques covered in the chapter. • Key terms. Each chapter lists key terms and their definitions; the list appears in the order that the terms are encountered in the chapter. A glossary at the end of the book lists all the key terms in alphabetical order, along with their
  • 86.
    working definitions. MindTap MindTap isa personalized learning experience with relevant assignments that guide stu- dents in analyzing problems, applying what they have learned, and improving their think- ing. MindTap allows instructors to measure skills and outcomes with ease. For instructors: Personalized teaching becomes yours with a learning path that is built with key student objectives. You can control what students see and when they see it. You can use MindTap as-is, or match it to your syllabus by hiding, rearranging, or adding content. For students: A unique learning path of relevant readings, multimedia, and activities is cre- ated to guide you through basic knowledge and comprehension of analysis and application. For both: Better outcomes empower instructors and motivate students with analytics and reports that provide a snapshot of class progress, the time spent
  • 87.
    in the course,engagement levels, and completion rates. The MindTap for Programming Logic and Design includes coding labs in C11, Java, and Python, study tools, videos, and interactive quizzing, all integrated into an eReader that includes the full content of the printed text. xxii P R E F A C E MindTap Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Instructor Resources The following teaching tools are available to the instructor for download through our Instructor Companion Site at sso.cengage.com. • Instructor’s Manual. The Instructor’s Manual follows the text
  • 88.
    chapter by chapterto 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. • PowerPoint Presentations. This text provides PowerPoint slides to accompany each chapter. Slides are included to guide classroom presentations, and can be made available to students for chapter review, or to print as classroom handouts. •