SEVENTH EDITION J A V A ™ P R O G R A M S T O A C C O M P A N Y PROGRAMMING LOGIC AND DESIGN BY JO ANN SMITH Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Java Programs to Accompany Programming © 2013 Course Technology, Cengage Learning Logic and Design, Seventh Edition ALL RIGHTS RESERVED. No part of this work covered by the copyright Jo Ann Smith herein may be reproduced, transmitted, stored, or used in any form or by any means graphic, electronic, or mechanical, including but not limited to Executive Editor: Marie Lee photocopying, recording, scanning, digitizing, taping, Web distribution, Acquisitions Editor: Brandi Shailer information networks, or information storage and retrieval systems, except Senior Product Manager: Alyssa Pratt as permitted under Section 107 or 108 of the 1976 United States Copyright Senior Content Project Manager: Act, without the prior written permission of the publisher. Cathie DiMassa For product information and technology assistance, contact us at Art Director: Faith Brosnan Cengage Learning Customer & Sales Support, cengage.com/support Compositor: Integra Software Services For permission to use material from this text or product, Cover Designer: Lisa Kuhn/Curio Press, LLC submit all requests online at www.cengage.com/permissions HYPERLINK “http://www.curiopress.com/” Further permissions questions can be e-mailed to www.curiopress.com [email protected] Image credit: © Leigh Prather/Veer Library of Congress Control Number: 2012931579 ISBN-13: 978-1-133-52606-3 ISBN-10: 1-133-52606-3 Course Technology 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with offi ce locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local offi ce at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit course.cengage.com Visit our corporate Web site at cengage.com. Some of the product names and company names used in this book have been used for identifi cation purposes only and may be trademarks or registered trademarks of their respective manufacturers and sellers. Any fi ctional data related to persons or companies or URLs used throughout this book is intended for instructional purposes only. At the time this book was printed, any such data was fi ctional and not belonging to any real persons or companies. Course Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice. The programs in this book are for instructional purposes only. They have been tested with care, but are not guaranteed for any particular intent beyond educational purposes. The author and the publisher do not off er any warranties or representations, nor do they accept any liabilities with respect to the programs. Printed in the United States of America 1 2 3 4 5 6 7 16 15 14 13 12 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Brief Contents iii Preface . . . . . . . . . . . . . . . . . ix CHAPTER 1 An Introduction to Java and the Java Programming Environment . . . . . . . 1 CHAPTER 2 Variables, Constants, Operators, and Writing Programs Using Sequential Statements . . . . . . . . . . . . . . . . 13 CHAPTER 3 Writing Structured Java Programs . . . . . . 33 CHAPTER 4 Writing Programs that Make Decisions . . . . 48 CHAPTER 5 Writing Programs Using Loops . . . . . . . 75 CHAPTER 6 Using Arrays in Java Programs . . . . . . 101 CHAPTER 7 File Handling and Applications . . . . . . 118 CHAPTER 8 Advanced Array Techniques . . . . . . . . 135 CHAPTER 9 Advanced Modularization Techniques . . . . 152 CHAPTER 10 Additional Topics . . . . . . . . . . . . 180 Index . . . . . . . . . . . . . . . . . 197 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Contents iv Preface . . . . . . . . . . . . . . . . . ix CHAPTER 1 An Introduction to Java and the Java Programming Environment . . . . . . . 1 The Java Programming Language . . . . . . . . . . . . . . 2 Three Types of Java Programs . . . . . . . . . . . . . . . 2 An Introduction to Object-Oriented Terminology. . . . . . . . 3 Downloading the Java Standard Edition Development Kit (JDK) . . . . . . . . . . . . . . . . . . 5 The Structure of a Java Program . . . . . . . . . . . . . . 5 The Java Development Cycle . . . . . . . . . . . . . . . . 7 Writing Java Source Code. . . . . . . . . . . . . . . . . 8 Compiling a Java Program . . . . . . . . . . . . . . . . 8 Executing a Java Program . . . . . . . . . . . . . . . .10 Exercise 1-1: Understanding Java Utilities . . . . . . . . .11 Lab 1.1: Understanding Java Utilities . . . . . . . . . . .12 CHAPTER 2 Variables, Constants, Operators, and Writing Programs Using Sequential Statements. . . . 13 Variables . . . . . . . . . . . . . . . . . . . . . . . . .14 Variable Names . . . . . . . . . . . . . . . . . . . . .14 Java Data Types . . . . . . . . . . . . . . . . . . . . .15 Exercise 2-1: Using Java Variables, Data Types, and Keywords . . . . . . . . . . . . . . . . . . . . .16 Declaring and Initializing Variables. . . . . . . . . . . . . .16 Exercise 2-2: Declaring and Initializing Java Variables . . . .17 Lab 2.1: Declaring and Initializing Java Variables . . . . . .18 Constants. . . . . . . . . . . . . . . . . . . . . . . . .19 Unnamed Constants . . . . . . . . . . . . . . . . . . .19 Named Constants . . . . . . . . . . . . . . . . . . . .19 Exercise 2-3: Declaring and Initializing Java Constants . . .19 Lab 2.2: Declaring and Initializing Java Constants. . . . . .20 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CONTENTS Arithmetic and Assignment Operators . . . . . . . . . . . .20 Arithmetic Operators. . . . . . . . . . . . . . . . . . .21 Assignment Operators and the Assignment Statement. . . .22 Precedence and Associativity . . . . . . . . . . . . . . .23 Exercise 2-4: Understanding Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . .24 v Lab 2.3: Arithmetic and Assignment Operators . . . . . . .25 Sequential Statements, Comments, and Interactive Input Statements. . . . . . . . . . . . . . . . . . . . .26 Exercise 2-5: Understanding Sequential Statements. . . . .30 Lab 2.4: Understanding Sequential Statements . . . . . . .31 CHAPTER 3 Writing Structured Java Programs . . . . . . 33 Using Flowcharts and Pseudocode to Write a Java Program. . .34 Lab 3.1: Using Flowcharts and Pseudocode to Write a Java Program. . . . . . . . . . . . . . . . . .38 Writing a Modular Program in Java . . . . . . . . . . . . .40 Lab 3.2: Writing a Modular Program in Java . . . . . . . .47 CHAPTER 4 Writing Programs that Make Decisions . . . . 48 Boolean Operators. . . . . . . . . . . . . . . . . . . . .49 Relational Operators . . . . . . . . . . . . . . . . . . .49 Logical Operators . . . . . . . . . . . . . . . . . . . .50 Relational and Logical Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . .51 Comparing Strings. . . . . . . . . . . . . . . . . . . . .53 Decision Statements. . . . . . . . . . . . . . . . . . . .55 The Statement . . . . . . . . . . . . . . . . . . . .55 if Exercise 4-1: Understanding Statements . . . . . . . .57 if Lab 4.1: Understanding Statements . . . . . . . . . .58 if The Statement . . . . . . . . . . . . . . . . .59 if-else Exercise 4-2: Understanding Statements . . . . .61 if-else Lab 4.2: Understanding Statements . . . . . . .62 if-else Nested Statements . . . . . . . . . . . . . . . . . .63 if Exercise 4-3: Understanding Nested Statements. . . . .65 if Lab 4.3: Understanding Nested Statements. . . . . . .66 if The Statement . . . . . . . . . . . . . . . . .67 switch Exercise 4-4: Using a Statement . . . . . . . . .68 switch Lab 4.4: Using a Statement . . . . . . . . . . .69 switch Using Decision Statements to Make Multiple Comparisons. . .70 Using AND Logic. . . . . . . . . . . . . . . . . . . . .70 Using OR Logic . . . . . . . . . . . . . . . . . . . . .71 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CONTENTS Exercise 4-5: Making Multiple Comparisons in Decision Statements . . . . . . . . . . . . . . . . . . . . . .72 Lab 4.5: Making Multiple Comparisons in Decision Statements . . . . . . . . . . . . . . . . . .73 CHAPTER 5 Writing Programs Using Loops . . . . . . . 75 vi The Increment (++) and Decrement (− −) Operators . . . . .76 Exercise 5-1: Using the Increment (++) and Decrement (−−) Operators . . . . . . . . . . . . .77 Writing a Loop in Java. . . . . . . . . . . . . . . .78 while Exercise 5-2: Using a Loop . . . . . . . . . . . .80 while Using a Counter to Control a Loop . . . . . . . . . . . . .80 Exercise 5-3: Using a Counter-Controlled Loop . . .81 while Lab 5.1: Using a Counter-Controlled Loop. . . . . .82 while Using a Sentinel Value to Control a Loop. . . . . . . . . . .83 Exercise 5-4: Using a Sentinel Value to Control a Loop . . . . . . . . . . . . . . . . . . . . .85 while Lab 5.2: Using a Sentinel Value to Control a Loop . .86 while Writing a Loop in Java . . . . . . . . . . . . . . . . .86 for Exercise 5-5: Using a Loop. . . . . . . . . . . . . .88 for Lab 5.3: Using a Loop . . . . . . . . . . . . . . . .89 for Writing a do Loop in Java . . . . . . . . . . . . . .89 while Exercise 5-6: Using a do Loop . . . . . . . . . . .90 while Lab 5.4: Using a do Loop . . . . . . . . . . . . .91 while Nesting Loops. . . . . . . . . . . . . . . . . . . . . . .91 Exercise 5-7: Nesting Loops . . . . . . . . . . . . . . .92 Lab 5.5: Nesting Loops . . . . . . . . . . . . . . . . .93 Accumulating Totals in a Loop . . . . . . . . . . . . . . .94 Exercise 5-8: Accumulating Totals in a Loop . . . . . . . .96 Lab 5.6: Accumulating Totals in a Loop . . . . . . . . . .97 Using a Loop to Validate Input . . . . . . . . . . . . . . .98 Exercise 5-9: Validating User Input . . . . . . . . . . . .99 Lab 5.7: Validating User Input. . . . . . . . . . . . . . 100 CHAPTER 6 Using Arrays in Java Programs . . . . . . 101 Array Basics . . . . . . . . . . . . . . . . . . . . . . 102 Declaring Arrays. . . . . . . . . . . . . . . . . . . . 102 Initializing Arrays . . . . . . . . . . . . . . . . . . . 104 Accessing Array Elements . . . . . . . . . . . . . . . 104 Staying Within the Bounds of an Array . . . . . . . . . . 105 Using Constants with Arrays . . . . . . . . . . . . . . 106 Exercise 6-1: Array Basics . . . . . . . . . . . . . . . 107 Lab 6.1: Array Basics . . . . . . . . . . . . . . . . . 107 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CONTENTS Searching an Array for an Exact Match . . . . . . . . . . 108 Exercise 6-2: Searching an Array for an Exact Match . . . 110 Lab 6.2: Searching an Array for an Exact Match . . . . . 111 Parallel Arrays. . . . . . . . . . . . . . . . . . . . . . 112 Exercise 6-3: Parallel Arrays . . . . . . . . . . . . . . 115 Lab 6.3: Parallel Arrays . . . . . . . . . . . . . . . . 116 vii CHAPTER 7 File Handling and Applications . . . . . . 118 File Handling . . . . . . . . . . . . . . . . . . . . . . 119 Importing Packages and Classes . . . . . . . . . . . . 119 Opening a File for Reading . . . . . . . . . . . . . . . 120 Reading Data from an Input File . . . . . . . . . . . . . 121 Reading Data Using a Loop and EOF. . . . . . . . . . . 122 Opening a File for Writing. . . . . . . . . . . . . . . . 122 Writing Data to an Output File . . . . . . . . . . . . . . 123 Exercise 7-1: Opening Files and Performing File Input . . . 125 Lab 7.1: Opening Files and Performing File Input . . . . . 126 Understanding Sequential Files and Control Break Logic. . . 126 Exercise 7-2: Accumulating Totals in Single-Level Control Break Programs . . . . . . . . . . . . . . . 132 Lab 7.2: Accumulating Totals in Single-Level Control Break Programs . . . . . . . . . . . . . . . 132 CHAPTER 8 Advanced Array Techniques . . . . . . . . 135 Sorting Data . . . . . . . . . . . . . . . . . . . . . . 136 Swapping Data Values . . . . . . . . . . . . . . . . . . 137 Exercise 8-1: Swapping Values . . . . . . . . . . . . . 137 Lab 8.1: Swapping Values . . . . . . . . . . . . . . . 138 Using a Bubble Sort . . . . . . . . . . . . . . . . . . . 138 The Method . . . . . . . . . . . . . . . . . 142 main () The Method. . . . . . . . . . . . . . . 143 fillArray() The Method. . . . . . . . . . . . . . . 144 sortArray() The Method. . . . . . . . . . . . . 145 displayArray() Exercise 8-2: Using a Bubble Sort. . . . . . . . . . . . 145 Lab 8.2: Using a Bubble Sort . . . . . . . . . . . . . . 146 Using Multidimensional Arrays . . . . . . . . . . . . . . 147 Exercise 8-3: Using Multidimensional Arrays . . . . . . . 150 Lab 8.3: Using Multidimensional Arrays . . . . . . . . . 150 CHAPTER 9 Advanced Modularization Techniques . . . . 152 Writing Methods with No Parameters . . . . . . . . . . . 153 Exercise 9-1: Writing Methods with No Parameters . . . . 155 Lab 9.1: Writing Methods with No Parameters . . . . . . 156 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. CONTENTS Writing Methods that Require a Single Parameter . . . . . . 156 Exercise 9-2: Writing Methods that Require a Single Parameter. . . . . . . . . . . . . . . . . . 159 Lab 9.2: Writing Methods that Require a Single Parameter . . . . . . . . . . . . . . . . . . . . . . 160 Writing Methods that Require Multiple Parameters . . . . . 160 viii Exercise 9-3: Writing Methods that Require Multiple Parameters . . . . . . . . . . . . . . . . . . 162 Lab 9.3: Writing Methods that Require Multiple Parameters . . . . . . . . . . . . . . . . . . . . . 163 Writing Methods that Return a Value . . . . . . . . . . . . 164 Exercise 9-4: Writing Methods that Return a Value . . . . 166 Lab 9.4: Writing Methods that Return a Value. . . . . . . 167 Passing an Array and an Array Element to a Method . . . . 168 Exercise 9-5: Passing Arrays to Methods. . . . . . . . . 171 Lab 9.5: Passing Arrays to Methods. . . . . . . . . . . 172 Overloading Methods. . . . . . . . . . . . . . . . . . . 172 Exercise 9-6: Overloading Methods . . . . . . . . . . . . 175 Lab 9.6: Overloading Methods . . . . . . . . . . . . . 176 Using Java’s Built-In Methods . . . . . . . . . . . . . . . 177 Exercise 9-7: Using Java’s Built-In Methods. . . . . . . . 177 Lab 9.7: Using Java’s Built-In Methods . . . . . . . . . . 178 CHAPTER 10 Additional Topics . . . . . . . . . . . . 180 A Programmer-Defined Class . . . . . . . . . . . . . . . 181 Creating a Programmer-Defined Class . . . . . . . . . . 182 Adding Attributes to a Class . . . . . . . . . . . . . . 184 Adding Methods to a Class . . . . . . . . . . . . . . . 184 Exercise 10-1: Creating a Programmer-Defined Class in Java. . . . . . . . . . . . . . . . . . . . . 187 Lab 10.1: Creating a Programmer-Defined Class in Java. . 189 Creating a Graphical User Interface (GUI) . . . . . . . . . 190 Writing a Constructor . . . . . . . . . . . . . . . . . 192 Writing the Method . . . . . . . . . . . . . . 194 main() Exercise 10-2: Creating a Graphical User Interface in Java . . . . . . . . . . . . . . . . . . . . . . . 195 Lab 10.2: Creating a Graphical User Interface in Java. . . 196 Index . . . . . . . . . . . . . . . . . 197 Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.