Table Of ContentEIGHTH EDITION
JAVA PROGRAMMING
JOYCE FARRELL
JavaProgramming,
EighthEdition
JoyceFarrell
©2016,2014,2012CengageLearning
WCN:02-200-203
LibraryofCongressControlNumber:2014956152
ISBN:978-1-285-85691-9
CengageLearning
20ChannelCenterStreet
Boston,MA02210
USA
Printed in theUnited StatesofAmerica
Print Number:01 Print Year:2015
Brief Contents
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
Contents
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
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . 45
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . . 46
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 48
Programming Exercises . . . . . . . . . . . . . . . . . . 48
DebuggingExercises . . . . . . . . . . . . . . . . . . . 50
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
ChapterSummary . . . . . . . . . . . . . . . . . . . . . 111
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 111
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 114
Programming Exercises . . . . . . . . . . . . . . . . . 114
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
CHAPTER 4 More Object Concepts . . . . . . . . . . . 183
Understanding BlocksandScope . . . . . . . . . . . . . . 184
Overloadinga Method . . . . . . . . . . . . . . . . . . . 192
AutomaticTypePromotion in MethodCalls . . . . . . . . . 194
Learning About Ambiguity . . . . . . . . . . . . . . . . . 199
Creating andCalling Constructors withParameters . . . . . . . 200
Overloading Constructors . . . . . . . . . . . . . . . . 201
Learning About the thisReference . . . . . . . . . . . . . 205
Using the thisReferenceto MakeOverloadedConstructors
More Efficient . . . . . . . . . . . . . . . . . . . . . 209
Using static Fields . . . . . . . . . . . . . . . . . . . 213
Using ConstantFields . . . . . . . . . . . . . . . . . . 215
Using AutomaticallyImported,PrewrittenConstants
andMethods . . . . . . . . . . . . . . . . . . . . . . 220
TheMathClass . . . . . . . . . . . . . . . . . . . . 221
ImportingClassesthatAre Not ImportedAutomatically . . . . 223
Using the LocalDateClass . . . . . . . . . . . . . . . 224
Understanding Composition andNestedClasses . . . . . . . . 230
Composition . . . . . . . . . . . . . . . . . . . . . . 230
Nested Classes . . . . . . . . . . . . . . . . . . . . . 232
Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 234
KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 234
ChapterSummary . . . . . . . . . . . . . . . . . . . . . 236
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 236
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 239
Programming Exercises . . . . . . . . . . . . . . . . . 239
DebuggingExercises . . . . . . . . . . . . . . . . . . 242
Game Zone . . . . . . . . . . . . . . . . . . . . . . . 242
Case Problems . . . . . . . . . . . . . . . . . . . . . 243
CHAPTER 5 Making Decisions . . . . . . . . . . . . . 245
PlanningDecision-Making Logic . . . . . . . . . . . . . . . 246
Theifandif…elseStatements . . . . . . . . . . . . . . 248
TheifStatement . . . . . . . . . . . . . . . . . . . . 248
Pitfall:Misplacinga Semicolonin anifStatement . . . . . . 249
Pitfall:Using the Assignment OperatorInstead
ofthe EquivalencyOperator . . . . . . . . . . . . . . 250
Pitfall:Attempting toCompareObjects
Using the Relational Operators . . . . . . . . . . . . . 251
Theif…elseStatement . . . . . . . . . . . . . . . . . 251
Using Multiple Statements in ifandif…elseClauses . . . . 254
Nesting ifandif…elseStatements . . . . . . . . . . . . 260
Using LogicalAND andOROperators . . . . . . . . . . . . 263
TheAND Operator . . . . . . . . . . . . . . . . . . . . 263
TheOROperator . . . . . . . . . . . . . . . . . . . . 265
Short-Circuit Evaluation . . . . . . . . . . . . . . . . . . 266
Making Accurate andEfficient Decisions . . . . . . . . . . . 269
Making AccurateRange Checks . . . . . . . . . . . . . . 270
Making EfficientRange Checks . . . . . . . . . . . . . . 272
Using &&and||Appropriately . . . . . . . . . . . . . . 273
Using the switch Statement . . . . . . . . . . . . . . . . 274
Using the ConditionalandNOTOperators . . . . . . . . . . . 280
Using the NOTOperator . . . . . . . . . . . . . . . . . 281
Understanding OperatorPrecedence . . . . . . . . . . . . . 282
AddingDecisions andConstructors
toInstance Methods . . . . . . . . . . . . . . . . . . . 285
Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 289
KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 289
ChapterSummary . . . . . . . . . . . . . . . . . . . . . 291
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 291
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 294
Programming Exercises . . . . . . . . . . . . . . . . . 294
DebuggingExercises . . . . . . . . . . . . . . . . . . 297
Game Zone . . . . . . . . . . . . . . . . . . . . . . . 297
Case Problems . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 6 Looping . . . . . . . . . . . . . . . . . 301
Learning About the LoopStructure . . . . . . . . . . . . . . 302
Creating whileLoops . . . . . . . . . . . . . . . . . . 303
Writing a Definitewhile Loop . . . . . . . . . . . . . . 303
Pitfall:Failing toAlterthe LoopControlVariable
Within the LoopBody . . . . . . . . . . . . . . . . . 305
Pitfall:UnintentionallyCreatingaLoopwith
anEmptyBody . . . . . . . . . . . . . . . . . . . . 306
Altering aDefinite Loop’sControlVariable . . . . . . . . . . 307
Writing anIndefinitewhile Loop . . . . . . . . . . . . . 308
ValidatingData . . . . . . . . . . . . . . . . . . . . . 310
Using Shortcut Arithmetic Operators . . . . . . . . . . . . . 314
Creating a forLoop . . . . . . . . . . . . . . . . . . . 319
Unconventional forLoops . . . . . . . . . . . . . . . . 320
Learning HowandWhen toUse ado…while Loop . . . . . . 325
Learning About Nested Loops . . . . . . . . . . . . . . . . 328
ImprovingLoopPerformance . . . . . . . . . . . . . . . . 333
Avoiding UnnecessaryOperations . . . . . . . . . . . . . 333
Considering the OrderofEvaluationofShort-Circuit
Operators . . . . . . . . . . . . . . . . . . . . . . 334
Comparing to Zero . . . . . . . . . . . . . . . . . . . 334
Employing LoopFusion . . . . . . . . . . . . . . . . . . 336
Using PrefixIncrementingRatherthanPostfix
Incrementing . . . . . . . . . . . . . . . . . . . . . 337
AFinalNote on ImprovingLoopPerformance . . . . . . . . 338
Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 342
KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 342
ChapterSummary . . . . . . . . . . . . . . . . . . . . . 344
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 344
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 347
Programming Exercises . . . . . . . . . . . . . . . . . 347
DebuggingExercises . . . . . . . . . . . . . . . . . . 350
Game Zone . . . . . . . . . . . . . . . . . . . . . . . 350
Case Problems . . . . . . . . . . . . . . . . . . . . . 352
CHAPTER 7 Characters, Strings, and
the StringBuilder . . . . . . . . . . . . 353
Understanding StringData Problems . . . . . . . . . . . . . 354
Using CharacterClassMethods . . . . . . . . . . . . . . 355
Declaring andComparing StringObjects . . . . . . . . . . 359
ComparingStringValues . . . . . . . . . . . . . . . . 359
EmptyandnullStrings . . . . . . . . . . . . . . . . . 363
Using OtherStringMethods . . . . . . . . . . . . . . . 365
Converting String Objects toNumbers . . . . . . . . . . 369
Learning About the StringBuilder
andStringBuffer Classes . . . . . . . . . . . . . . 374
Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 381
KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 382
ChapterSummary . . . . . . . . . . . . . . . . . . . . . 382
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 383
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 385
Programming Exercises . . . . . . . . . . . . . . . . . 385
DebuggingExercises . . . . . . . . . . . . . . . . . . 388
Game Zone . . . . . . . . . . . . . . . . . . . . . . . 388
Case Problems . . . . . . . . . . . . . . . . . . . . . 391
CHAPTER 8 Arrays . . . . . . . . . . . . . . . . . . 393
Declaring Arrays . . . . . . . . . . . . . . . . . . . . . 394
InitializinganArray . . . . . . . . . . . . . . . . . . . . 399
Using VariableSubscriptswith anArray . . . . . . . . . . . . 402
Using the EnhancedforLoop . . . . . . . . . . . . . . 403
Using Part ofanArray . . . . . . . . . . . . . . . . . . 404
Declaring andUsing Arrays ofObjects . . . . . . . . . . . . 406
Using the EnhancedforLoopwithObjects . . . . . . . . . 408
Manipulating Arrays ofStrings . . . . . . . . . . . . . 408
Searching anArrayandUsingParallelArrays . . . . . . . . . 414
Using Parallel Arrays . . . . . . . . . . . . . . . . . . . 415
SearchinganArrayforaRangeMatch . . . . . . . . . . . 418
Passing Arrays to andReturning ArraysfromMethods . . . . . 422
ReturninganArrayfrom aMethod . . . . . . . . . . . . . 426
Don’tDoIt . . . . . . . . . . . . . . . . . . . . . . . . 428
KeyTerms . . . . . . . . . . . . . . . . . . . . . . . . 428
ChapterSummary . . . . . . . . . . . . . . . . . . . . . 429
ReviewQuestions . . . . . . . . . . . . . . . . . . . . . 430
Exercises . . . . . . . . . . . . . . . . . . . . . . . . 433
Programming Exercises . . . . . . . . . . . . . . . . . 433
DebuggingExercises . . . . . . . . . . . . . . . . . . 435
Game Zone . . . . . . . . . . . . . . . . . . . . . . . 435
Case Problems . . . . . . . . . . . . . . . . . . . . . 438