TM JAVA PROGRAMMING Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. EIGHTH EDITION TM JAVA PROGRAMMING JOYCE FARRELL Australia (cid:129) Brazil (cid:129) Japan (cid:129) Korea (cid:129) Mexico (cid:129) Singapore (cid:129) Spain (cid:129) United Kingdom (cid:129) United States Copyright 2016 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. Important Notice: Media content referenced within the product description or the product text may not be available in the eBook version. Copyright 2016 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. JavaProgramming, ©2016,2014,2012CengageLearning EighthEdition WCN:02-200-203 JoyceFarrell ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyright ProductDirector: hereinmaybereproduced,transmitted,stored,orusedinanyformor KathleenMcMahon byanymeansgraphic,electronic,ormechanical,includingbutnot SeniorContentDeveloper: limitedtophotocopying,recording,scanning,digitizing,taping,Web AlyssaPratt distribution,informationnetworks,orinformationstorageand retrievalsystems,exceptaspermittedunderSection107or108ofthe DevelopmentEditor:DanSeiter 1976UnitedStatesCopyrightAct,withoutthepriorwritten MarketingManager:EricLaScola permissionofthepublisher. ManufacturingPlanner: JulioEsperas Forproductinformationandtechnologyassistance,contactusat ArtDirector:JackPendleton CengageLearningCustomer&SalesSupport,1-800-354-9706 ProductionManagement, Forpermissiontousematerialfromthistextorproduct, Copyediting,Composition, submitallrequestsonlineatwww.cengage.com/permissions. Proofreading,andIndexing: IntegraSoftwareServicesPvt.Ltd. Furtherpermissionsquestionscanbeemailedto CoverPhoto: [email protected]. ©Maram/Shutterstock.com LibraryofCongressControlNumber:2014956152 ISBN:978-1-285-85691-9 CengageLearning 20ChannelCenterStreet Boston,MA02210 USA CengageLearningisaleadingproviderofcustomizedlearning solutionswithofficelocationsaroundtheglobe,includingSingapore, theUnitedKingdom,Australia,Mexico,Brazil,andJapan.Locateyour localofficeatwww.cengage.com/global. CengageLearningproductsarerepresentedinCanadabyNelson Education,Ltd. Allimages©2016CengageLearning®.Allrightsreserved. TolearnmoreaboutCengageLearningSolutions,visit www.cengage.com. Purchaseanyofourproductsatyourlocalcollegestore oratourpreferredonlinestorewww.cengagebrain.com. Printed in theUnited StatesofAmerica Print Number:01 Print Year:2015 Copyright 2016 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 v Preface . . . . . . . . . . . . . . . . . . . . . . xxi CHAPTER 1 Creating Java Programs . . . . . . . . . . . . . . . . 1 CHAPTER 2 Using Data . . . . . . . . . . . . . . . . . . . . . 53 CHAPTER 3 Using Methods, Classes, and Objects . . . . . . . 119 CHAPTER 4 More Object Concepts . . . . . . . . . . . . . . . 183 CHAPTER 5 Making Decisions . . . . . . . . . . . . . . . . . 245 CHAPTER 6 Looping . . . . . . . . . . . . . . . . . . . . . 301 CHAPTER 7 Characters, Strings, and the StringBuilder . . . 353 CHAPTER 8 Arrays . . . . . . . . . . . . . . . . . . . . . . 393 CHAPTER 9 Advanced Array Concepts . . . . . . . . . . . . . 439 CHAPTER 10 Introduction to Inheritance . . . . . . . . . . . . . 491 CHAPTER 11 Advanced Inheritance Concepts . . . . . . . . . . 537 CHAPTER 12 Exception Handling . . . . . . . . . . . . . . . . 593 CHAPTER 13 File Input and Output . . . . . . . . . . . . . . . 665 CHAPTER 14 Introduction to Swing Components . . . . . . . . 729 CHAPTER 15 Advanced GUI Topics . . . . . . . . . . . . . . . 791 CHAPTER 16 Graphics . . . . . . . . . . . . . . . . . . . . . 861 APPENDIX A Working with the Java Platform . . . . . . . . . . . 919 APPENDIX B Data Representation . . . . . . . . . . . . . . . 925 APPENDIX C Formatting Output . . . . . . . . . . . . . . . . 931 APPENDIX D Generating Random Numbers . . . . . . . . . . . 941 APPENDIX E Javadoc . . . . . . . . . . . . . . . . . . . . . 949 Glossary . . . . . . . . . . . . . . . . . . . . 957 Index . . . . . . . . . . . . . . . . . . . . . . 979 Copyright 2016 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 vi Preface . . . . . . . . . . . . . . . . . . xxi CHAPTER 1 Creating Java Programs . . . . . . . . . . . 1 Learning Programming Terminology . . . . . . . . . . . . . . 2 Comparing Procedural andObject-Oriented ProgrammingConcepts . . . . . . . . . . . . . . . . . . 6 ProceduralProgramming . . . . . . . . . . . . . . . . . . 6 Object-OrientedProgramming . . . . . . . . . . . . . . . . 6 Understanding Classes,Objects, andEncapsulation . . . . . . 7 Understanding Inheritance andPolymorphism . . . . . . . . . 9 Featuresofthe Java Programming Language . . . . . . . . . . 11 JavaProgramTypes . . . . . . . . . . . . . . . . . . . . 12 Analyzing a Java ApplicationthatProducesConsoleOutput . . . . 13 Understanding the Statement thatProducesthe Output . . . . . 14 Understanding the First Class . . . . . . . . . . . . . . . 15 Indent Style . . . . . . . . . . . . . . . . . . . . . . . 18 Understanding the main()Method . . . . . . . . . . . . . 19 Saving a Java Class . . . . . . . . . . . . . . . . . . . . 21 Compilinga JavaClassandCorrectingSyntaxErrors . . . . . . . 23 Compiling aJava Class . . . . . . . . . . . . . . . . . . . 23 Correcting SyntaxErrors . . . . . . . . . . . . . . . . . . 24 Running aJava Application andCorrecting LogicErrors . . . . . . 29 Running aJava Application . . . . . . . . . . . . . . . . . 29 Modifying a Compiled Java Class . . . . . . . . . . . . . . 30 Correcting LogicErrors . . . . . . . . . . . . . . . . . . 31 AddingComments to a Java Class . . . . . . . . . . . . . . . 32 Creating a Java ApplicationthatProducesGUIOutput . . . . . . 35 Finding Help . . . . . . . . . . . . . . . . . . . . . . . . 38 Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . . 39 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . . 41 Copyright 2016 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. ChapterSummary . . . . . . . . . . . . . . . . . . . . . . 45 ReviewQuestions . . . . . . . . . . . . . . . . . . . . . . 46 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 48 Programming Exercises . . . . . . . . . . . . . . . . . . 48 DebuggingExercises . . . . . . . . . . . . . . . . . . . 50 vii Game Zone . . . . . . . . . . . . . . . . . . . . . . . . 50 Case Problems . . . . . . . . . . . . . . . . . . . . . . 51 CHAPTER 2 Using Data . . . . . . . . . . . . . . . . . 53 Declaring andUsing ConstantsandVariables . . . . . . . . . . 54 DeclaringVariables . . . . . . . . . . . . . . . . . . . . 55 DeclaringNamedConstants . . . . . . . . . . . . . . . . 56 TheScopeofVariablesandConstants . . . . . . . . . . . . 58 Concatenating Strings to VariablesandConstants . . . . . . . 58 Pitfall:Forgetting thata VariableHolds OneValue ataTime . . . . . . . . . . . . . . . . . . . 60 Learning About Integer Data Types . . . . . . . . . . . . . . 64 Using the booleanDataType . . . . . . . . . . . . . . . . . 70 Learning About Floating-Point Data Types . . . . . . . . . . . . 71 Using the charData Type . . . . . . . . . . . . . . . . . . 72 Using the ScannerClasstoAcceptKeyboardInput . . . . . . . 78 Pitfall:Using nextLine()Following One ofthe OtherScannerInputMethods . . . . . . . . . . . . . . 81 Using the JOptionPaneClasstoAcceptGUIInput . . . . . . . 87 Using Input Dialog Boxes . . . . . . . . . . . . . . . . . . 87 Using Confirm Dialog Boxes . . . . . . . . . . . . . . . . 91 Performing Arithmetic . . . . . . . . . . . . . . . . . . . . 93 AssociativityandPrecedence . . . . . . . . . . . . . . . . 95 Writing ArithmeticStatements Efficiently . . . . . . . . . . . 96 Pitfall:Not Understanding Imprecision in Floating-PointNumbers . . . . . . . . . . . . . . . . 96 Understanding Type Conversion . . . . . . . . . . . . . . . 101 AutomaticTypeConversion . . . . . . . . . . . . . . . . 101 ExplicitType Conversions . . . . . . . . . . . . . . . . 102 Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 106 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 107 Copyright 2016 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 ChapterSummary . . . . . . . . . . . . . . . . . . . . . 111 ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 111 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 114 Programming Exercises . . . . . . . . . . . . . . . . . 114 viii DebuggingExercises . . . . . . . . . . . . . . . . . . 116 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 117 Case Problems . . . . . . . . . . . . . . . . . . . . . 118 CHAPTER 3 Using Methods, Classes, and Objects . . . . 119 Understanding MethodCalls andPlacement . . . . . . . . . . 120 Understanding MethodConstruction . . . . . . . . . . . . . 123 AccessSpecifiers . . . . . . . . . . . . . . . . . . . . 123 Return Type . . . . . . . . . . . . . . . . . . . . . . 124 MethodName . . . . . . . . . . . . . . . . . . . . . 125 Parentheses . . . . . . . . . . . . . . . . . . . . . . 125 AddingParametersto Methods . . . . . . . . . . . . . . . 129 Creating a MethodthatReceives a Single Parameter . . . . . 130 Creating a MethodthatRequiresMultiple Parameters . . . . . 133 Creating MethodsthatReturn Values . . . . . . . . . . . . . 136 Chaining MethodCalls . . . . . . . . . . . . . . . . . . 138 Learning About ClassesandObjects . . . . . . . . . . . . . 142 Creating a Class . . . . . . . . . . . . . . . . . . . . . 145 Creating InstanceMethods in a Class . . . . . . . . . . . . 147 Organizing Classes . . . . . . . . . . . . . . . . . . . 150 Declaring Objects andUsing theirMethods . . . . . . . . . . 154 Understanding DataHiding . . . . . . . . . . . . . . . . 156 AnIntroductiontoUsing Constructors . . . . . . . . . . . . 159 Understanding thatClassesAre Data Types . . . . . . . . . . 163 Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 168 KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 168 ChapterSummary . . . . . . . . . . . . . . . . . . . . . 170 ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 171 Exercises . . . . . . . . . . . . . . . . . . . . . . . . 174 Programming Exercises . . . . . . . . . . . . . . . . . 174 DebuggingExercises . . . . . . . . . . . . . . . . . . 177 Game Zone . . . . . . . . . . . . . . . . . . . . . . . 178 Case Problems . . . . . . . . . . . . . . . . . . . . . 179 Copyright 2016 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.