Java Programming, Tenth Edition
By Joyce Farrell
Contents:
PREFACE XI
CHAPTER 1
CREATING JAVA PROGRAMS 1
1.1 Learning Programming Terminology 1
1.2 Comparing Procedural and Object-
Oriented Programming Concepts 4
Procedural Programming 4
Object-Oriented Programming 5
Understanding Classes, Objects, and Encapsulation 6
Understanding Inheritance and Polymorphism 7
1.3 Features of the Java Programming
Language 8
1.4 Analyzing a Java Application That
Produces Console Output 10
Understanding the Statement That Produces the
Output 10
Understanding the First Class 12
Understanding the main() Method 14
Indent Style 15
Saving a Java Class 16
1.5 Compiling a Java Class and
Correcting Syntax Errors 18
Compiling a Java Class 18
Correcting Syntax Errors 19
1.6 Running a Java Application and
Correcting Logic Errors 23
Running a Java Application 23
Modifying a Compiled Java Class 23
Correcting Logic Errors 24
1.7 Adding Comments to a Java Class 25
1.8 Creating a Java Application That
Produces GUI Output 27
1.9 Finding Help 29
Don’t Do It 30
Summary 31
Key Terms 32
Review Questions 33
Programming Exercises 34
Debugging Exercises 36
Game Zone 36
Case Problems 37
CHAPTER 2
USING DATA 39
2.1 Declaring and Using Constants
and Variables 39
Declaring Variables 40
Declaring Named Constants 42
The Scope of Variables and Constants 43
Concatenating Strings to Variables and
Constants 43
Pitfall: Forgetting That a Variable Holds One
Value at a Time 45
2.2 Learning About Integer Data
Types 47
2.3 Using the boolean Data Type 51
2.4 Learning About Floating-Point
Data Types 52
2.5 Using the char Data Type 53
2.6 Using the Scanner Class to
Accept Keyboard Input 57
Pitfall: Using nextLine() Following One of the
Other Scanner Input Methods 59
2.7 Using the JOptionPane Class to
Accept GUI Input 64
Using Input Dialog Boxes 64
Using Confirm Dialog Boxes 66
2.8 Performing Arithmetic Using
Variables and Constants 68
Associativity and Precedence 69
Writing Arithmetic Statements Efficiently 69
Pitfall: Not Understanding Imprecision in
Floating-Point Numbers 70
2.9 Understanding Type Conversion 72
Automatic Type Conversion 73
Explicit Type Conversion 73
Don’t Do It 76
Summary 77
Key Terms 77
Review Questions 78
Programming Exercises 80
Debugging Exercises 81
Game Zone 81
Case Problems 82
CHAPTER 3
USING METHODS 83
3.1 Understanding Method Calls and
Placement 83
3.2 Understanding Method
Construction 86
Access Specifiers 86
The static Modifier 87
Return Type 87
Method Name 87
Parentheses 88
3.3 Adding Parameters to Methods 91
Creating a Method That Receives a Single
Parameter 91
Creating a Method That Requires Multiple
Parameters 94
3.4 Creating Methods That
Return Values 95
3.5 Understanding Blocks and Scope 99
3.6 Overloading a Method 104
3.7 Learning about Ambiguity 107
Don’t Do It 108
Summary 108
Key Terms 109
Review Questions 109
Programming Exercises 111
Debugging Exercises 113
Game Zone 113
Case Problems 114
CHAPTER 4
USING CLASSES AND OBJECTS 115
4.1 Learning About Classes
and Objects 115
4.2 Creating a Class 117
4.3 Creating Instance Methods
in a Class 119
4.4 Declaring Objects and
Using Their Methods 124
Understanding Data Hiding 126
4.5 Understanding That Classes
Are Data Types 128
4.6 Creating and Using Constructors 131
Creating Constructors with Parameters 132
4.7 Learning About the this
Reference 134
Using the this Reference to Make
Overloaded Constructors More Efficient 137
4.8 Using static Fields 139
Using Constant Fields 140
4.9 Using Imported, Prewritten
Constants and Methods 143
The Math Class 144
Importing Classes That Are Not Imported
Automatically 145
Using the LocalDate Class 146
4.10 Understanding Composition
and Nested Classes 150
Composition 150
Nested Classes 151
Don’t Do It 153
Summary 153
Key Terms 154
Review Questions 154
Programming Exercises 156
Debugging Exercises 158
Game Zone 158
Case Problems 159
CHAPTER 5
MAKING DECISIONS 161
5.1 Planning Decision-Making Logic 161
5.2 The if and if…else Statements 163
The if Statement 163
Pitfall: Misplacing a Semicolon in an if Statement 164
Pitfall: Using the Assignment Operator Instead
of the Equivalency Operator 165
Pitfall: Attempting to Compare Objects Using
the Relational Operators 165
The if…else Statement 166
5.3 Using Multiple Statements in
if and if…else Clauses 168
5.4 Nesting if and if…else
Statements 172
5.5 Using Logical AND and OR
Operators 174
The AND Operator 174
The OR Operator 175
Short-Circuit Evaluation 175
5.6 Making Accurate and Efficient
Decisions 178
Making Accurate Range Checks 178
Making Efficient Range Checks 180
Using && and || Appropriately 180
5.7 Using switch 181
Using the switch Expression 183
5.8 Using the Conditional and NOT
Operators 186
Using the NOT Operator 187
5.9 Understanding Operator
Precedence 187
5.10 Making Constructors More
Efficient by Using Decisions in
Other Methods 189
Don’t Do It 193
Summary 193
Key Terms 194
Review Questions 194
Programming Exercises 197
Debugging Exercises 198
Game Zone 199
Case Problems 200
CHAPTER 6
LOOPING 201
6.1 Learning About the Loop
Structure 201
6.2 Creating while Loops 202
Writing a Definite while Loop 202
Pitfall: Failing to Alter the Loop Control Variable
Within the Loop Body 204
Pitfall: Unintentionally Creating a Loop with
an Empty Body 204
Altering a Definite Loop’s Control Variable 206
Writing an Indefinite while Loop 206
Validating Data 208
6.3 Using Shortcut Arithmetic
Operators 210
6.4 Creating a for Loop 214
Variations in for Loops 215
6.5 Learning How and When to Use
a do…while Loop 217
6.6 Learning About Nested Loops 220
6.7 Improving Loop Performance 223
Avoiding Unnecessary Operations 223
Considering the Order of Evaluation of
Short-Circuit Operators 224
Comparing to Zero 224
Employing Loop Fusion 226
A Final Note on Improving Loop Performance 226
Don’t Do It 228
Summary 228
Key Terms 229
Review Questions 229
Programming Exercises 232
Debugging Exercises 233
Game Zone 234
Case Problems 235
CHAPTER 7
CHARACTERS, STRINGS, AND
THE StringBuilder 237
7.1 Understanding String Data
Problems 237
7.2 Using Character Class Methods 238
7.3 Declaring and Comparing
String Objects 241
Comparing String Values 241
Empty and null Strings 245
7.4 Using a Variety of String
Methods 246
Converting String Objects to Numbers 249
7.5 Learning About the StringBuilder
and StringBuffer Classes 253
Don’t Do It 257
Summary 258
Key Terms 258
Review Questions 258
Programming Exercises 260
Debugging Exercises 262
Game Zone 263
Case Problems 264
CHAPTER 8
ARRAYS 267
8.1 Declaring an Array 267
8.2 Initializing an Array 271
8.3 Using Variable Subscripts with an
Array 273
Using the Enhanced for Loop 275
Using Part of an Array 275
8.4 Declaring and Using Arrays
of Objects 277
Using the Enhanced for Loop with Objects 279
Manipulating Arrays of Strings 279
8.5 Searching an Array and Using
Parallel Arrays 284
Using Parallel Arrays 284
Searching an Array for a Range Match 286
8.6 Passing Arrays to and Returning
Arrays from Methods 289
Returning an Array from a Method 291
8.7 Sorting Array Elements 292
Using the Bubble Sort Algorithm 293
Improving Bubble Sort Efficiency 295
Sorting Arrays of Objects 295
Using the Insertion Sort Algorithm 296
8.8 Using Two-Dimensional and Other
Multidimensional Arrays 300
Passing a Two-Dimensional Array to a Method 302
Using the length Field with a Two-Dimensional
Array 303
Understanding Jagged Arrays 304
Using Other Multidimensional Arrays 304
8.9 Using the Arrays Class 307
8.10 Creating Enumerations 311
Don’t Do It 316
Summary 317
Key Terms 318
Review Questions 318
Programming Exercises 320
Debugging Exercises 323
Game Zone 323
Case Problems 327
CHAPTER 9
INHERITANCE AND INTERFACES 329
9.1 Learning About the Concept of
Inheritance 329
Inheritance Terminology 331
9.2 Extending Classes 332
9.3 Overriding Superclass Methods 336
Using the @Override Annotation 337
9.4 Calling Constructors During
Inheritance 339
Using Superclass Constructors That Require
Arguments 340
9.5 Accessing Superclass Methods 344
Comparing this and super 345
9.6 Employing Information Hiding 346
9.7 Methods You Cannot Override 348
A Subclass Cannot Override static Methods
in Its Superclass 348
A Subclass Cannot Override final Methods
in Its Superclass 350
A Subclass Cannot Override Methods in a final
Superclass 351
9.8 Creating and Using Abstract
Classes 352
9.9 Using Dynamic Method Binding 359
Using a Superclass as a Method Parameter Type 360
9.10 Creating Arrays of Subclass
Objects 361
9.11 Using the Object Class and Its
Methods 364
Using the toString() Method 364
Using the equals() Method 366
Overloading equals() 367
Overriding equals() 369
9.12 Creating and Using Interfaces 371
Creating Interfaces to Store Related Constants 374
9.13 Using records, Anonymous Inner
Classes, and Lambda Expressions 377
Using records 377
Using Anonymous Inner Classes 379
Using Lambda Expressions 380
Don’t Do It 381
Summary 381
Key Terms 383
Review Questions 383
Programming Exercises 385
Debugging Exercises 389
Game Zone 390
Case Problems 391
CHAPTER 10
EXCEPTION HANDLING 393
10.1 Learning About Exceptions 393
10.2 Trying Code and Catching
Exceptions 397
Using a try Block to Make Programs “Foolproof” 400
Declaring and Initializing Variables in try…catch
Blocks 402
10.3 Throwing and Catching Multiple
Exceptions 404
10.4 Using the finally Block 408
10.5 Understanding the Advantages
of Exception Handling 410
10.6 Specifying the Exceptions That
a Method Can Throw 412
10.7 Tracing Exceptions Through the
Call Stack 415
10.8 Creating Your Own Exception
Classes 419
10.9 Using Assertions 421
10.10 Displaying the Virtual Keyboard 430
Don’t Do It 433
Summary 434
Key Terms 434
Review Questions 435
Programming Exercises 437
Debugging Exercises 439
Game Zone 439
Case Problems 440
CHAPTER 11
FILE INPUT AND OUTPUT 441
11.1 Understanding Computer Files 441
11.2 Using the Path and Files
Classes 443
Creating a Path 443
Retrieving Information About a Path 444
Converting a Relative Path to an Absolute One 445
Checking File Accessibility 446
Deleting a Path 447
Determining File Attributes 448
11.3 File Organization, Streams, and
Buffers 450
11.4 Using Java’s IO Classes 452
Writing to a File 454
Reading from a File 454
11.5 Creating and Using Sequential
Data Files 457
11.6 Learning About Random Access
Files 461
11.7 Writing Records to a Random
Access Data File 463
11.8 Reading Records from a Random
Access Data File 468
Accessing a Random Access File Sequentially 468
Accessing a Random Access File Randomly 470
Don’t Do It 479
Summary 479
Key Terms 480
Review Questions 480
Programming Exercises 482
Debugging Exercises 484
Game Zone 484
Case Problems 485
CHAPTER 12
RECURSION 487
12.1 Understanding Recursion 487
12.2 Using Recursion to Solve
Mathematical Problems 489
Computing Sums 490
Computing Factorials 491
12.3 Using Recursion to Manipulate
Strings 495
Using Recursion to Separate a Phrase into Words 495
Using Recursion to Reverse the Characters in a
String 496
12.4 Using Recursion to Create Visual
Patterns 499
12.5 Recursion’s Relationship to
Iterative Programming 500
Don’t Do It 503
Summary 503
Key Terms 504
Review Questions 504
Programming Exercises 506
Debugging Exercises 508
Game Zone 509
Case Problems 510
CHAPTER 13
COLLECTIONS AND GENERICS 511
13.1 Understanding the Collection
Interface 511
13.2 Understanding the List
Interface 513
13.3 Using the ArrayList Class 514
13.4 Using the LinkedList Class 524
13.5 Using Iterators 528
13.6 Creating Generic Classes 530
13.7 Creating Generic Methods 532
Creating a Generic Method with More than One
Type Parameter 533
Don’t Do It 537
Summary 538
Key Terms 538
Review Questions 539
Programming Exercises 541
Debugging Exercises 542
Game Zone 542
Case Problems 543
CHAPTER 14
INTRODUCTION TO Swing
COMPONENTS 545
14.1 Understanding Swing Components 545
14.2 Using the JFrame Class 547
Customizing a JFrame’s Appearance 549
14.3 Using the JLabel Class 552
Changing a JLabel’s Font 553
14.4 Using a Layout Manager 555
14.5 Extending the JFrame Class 557
14.6 Adding JTextFields and
JButtons to a JFrame 559
Adding JTextFields to a JFrame 559
Adding JButtons to a JFrame 560
14.7 Learning About Event-Driven
Programming 563
Preparing Your Class to Accept Event Messages 564
Telling Your Class to Expect Events to Happen 564
Telling Your Class How to Respond to Events 564
Writing an Event-Driven Program 565
Using Multiple Event Sources 566
Using the set Enabled () Method 567
14.8 Understanding Swing Event
Listeners 569
14.9 Using the JCheckBox,
ButtonGroup, and JComboBox
Classes 572
The JCheckBox Class 572
The ButtonGroup Class 574
The JComboBox Class 575
Don’t Do It 580
Summary 581
Key Terms 581
Review Questions 582
Programming Exercises 584
Debugging Exercises 585
Game Zone 585
Case Problems 586
APPENDIX A
WORKING WITH THE
JAVA PLATFORM 587
APPENDIX B
DATA REPRESENTATION 591
APPENDIX C
FORMATTING OUTPUT 595
APPENDIX D
GENERATING RANDOM
NUMBERS 603
APPENDIX E
JAVADOC 607
APPENDIX F
USING JAVAFX AND SCENE
BUILDER 613
GLOSSARY 625
INDEX 641