Java: An Introduction to Problem Solving & Programming, Eighth Edition
By Walter Savitch
Contents:
Chapter 1 Introduction to Computers and Java 37
1.1 COMPUTER BASICS 38
Hardware and Memory 39
Programs 42
Programming Languages, Compilers, and Interpreters 43
Java Bytecode 45
Class Loader 47
1.2 A SIP OF JAVA 48
History of the Java Language 48
Applications and Applets 49
A First Java Application Program 50
Writing, Compiling, and Running a Java Program 55
1.3 PROGRAMMING BASICS 57
Object-Oriented Programming 57
Algorithms 61
Testing and Debugging 63
Software Reuse 64
1.4 GRAPHICS SUPPLEMENT 66
A Sample JavaFX Application 67
Size and Position of Figures 70
Drawing Ovals and Circles 71
Drawing Arcs 73
Chapter 2 Basic Computation 85
2.1 VARIABLES AND EXPRESSIONS 86
Variables 87
Data Types 89
Java Identifiers 91
Assignment Statements 93
Simple Input 96
Simple Screen Output 98
Constants 98
Named Constants 100
Assignment Compatibilities 101
Type Casting 103
Arithmetic Operators 106
Parentheses and Precedence Rules 109
Specialized Assignment Operators 110
Case Study: Vending Machine Change 112
Increment and Decrement Operators 117
More About the Increment and Decrement Operators 118
2.2 THE CLASS String 119
String Constants and Variables 119
Concatenation of Strings 120
String Methods 121
String Processing 123
Escape Characters 126
The Unicode Character Set 127
2.3 KEYBOARD AND SCREEN I/O 129
Screen Output 129
Keyboard Input 132
Other Input Delimiters (Optional) 137
Formatted Output with printf (Optional) 139
2.4 DOCUMENTATION AND STYLE 141
Meaningful Variable Names 141
Comments 142
Indentation 145
Using Named Constants 145
2.5 GRAPHICS SUPPLEMENT 147
Style Rules Applied to a JavaFX Application 148
Introducing the Class JOptionPane 150
Reading Input as Other Numeric Types 159
Programming Example: Change-Making Program
with Windowing I/O 160
Chapter 3 Flow of Control: Branching 175
3.1 THE if-else STATEMENT 176
The Basic if-else Statement 177
Boolean Expressions 184
Comparing Strings 189
Nested if-else Statements 194
Multibranch if-else Statements 196
Programming Example: Assigning Letter Grades 198
Case Study: Body Mass Index 201
The Conditional Operator (Optional) 204
The exit Method 204
3.2 THE TYPE boolean 205
Boolean Variables 206
Precedence Rules 207
Input and Output of Boolean Values 210
3.3 THE switch STATEMENT 212
Enumerations 218
3.4 GRAPHICS SUPPLEMENT 219
Specifying a Drawing Color 220
A Dialog Box for a Yes-or-No Question 224
Chapter 4 Flow of Control: Loops 237
4.1 JAVA LOOP STATEMENTS 238
The while Statement 239
The do-while Statement 242
Programming Example: Bug Infestation 247
Programming Example: Nested Loops 253
The for Statement 255
Declaring Variables Within a for Statement 261
Using a Comma in a for Statement (Optional) 262
The for-each Statement 264
4.2 PROGRAMMING WITH LOOPS 264
The Loop Body 265
Initializing Statements 266
Controlling the Number of Loop Iterations 267
Case Study: Using a Boolean Variable to End a Loop 269
Programming Example: Spending Spree 271
The break Statement and continue Statement in Loops
(Optional) 274
Loop Bugs 277
Tracing Variables 279
Assertion Checks 281
4.3 GRAPHICS SUPPLEMENT 283
Programming Example: A Multiface JavaFX Application 283
Drawing Text 288
Chapter 5 Defining Classes and Methods 305
5.1 CLASS AND METHOD DEFINITIONS 307
Class Files and Separate Compilation 309
Programming Example: Implementing a Dog Class 309
Instance Variables 310
Methods 313
Defining void Methods 316
Defining Methods That Return a Value 317
Programming Example: First Try at Implementing a Species Class 322
The Keyword this 326
Local Variables 328
Blocks 330
Parameters of a Primitive Type 331
5.2 INFORMATION HIDING AND ENCAPSULATION 337
Information Hiding 338
Precondition and Postcondition Comments 338
The public and private Modifiers 340
Programming Example: A Demonstration of Why Instance
Variables Should Be Private 343
Programming Example: Another Implementation of a Class
of Rectangles 344
Accessor Methods and Mutator Methods 346
Programming Example: A Purchase Class 350
Methods Calling Methods 354
Encapsulation 360
Automatic Documentation with javadoc 363
UML Class Diagrams 364
5.3 OBJECTS AND REFERENCES 365
Variables of a Class Type 366
Defining an equals Method for a Class 371
Programming Example: A Species Class 375
Boolean-Valued Methods 378
Case Study: Unit Testing 380
Parameters of a Class Type 382
Programming Example: Class-Type Parameters Versus
Primitive-Type Parameters 386
5.4 GRAPHICS SUPPLEMENT 390
The GraphicsContext Class 390
Programming Example: Multiple Faces, but with a Helping
Method 394
Adding Labels to a JavaFX Application 398
Chapter 6 More About Objects and Methods 419
6.1 CONSTRUCTORS 421
Defining Constructors 421
Calling Methods from Constructors 430
Calling a Constructor from Other Constructors (Optional) 433
6.2 STATIC VARIABLES AND STATIC METHODS 435
Static Variables 435
Static Methods 436
Dividing the Task of a main Method into Subtasks 443
Adding a main Method to a Class 444
The Math Class 446
Wrapper Classes 449
6.3 WRITING METHODS 455
Case Study: Formatting Output 455
Decomposition 461
Addressing Compiler Concerns 462
Testing Methods 464
6.4 OVERLOADING 466
Overloading Basics 466
Overloading and Automatic Type Conversion 469
Overloading and the Return Type 472
Programming Example: A Class for Money 474
6.5 INFORMATION HIDING REVISITED 481
Privacy Leaks 481
6.6 ENUMERATION AS A CLASS 485
6.7 PACKAGES 487
Packages and Importing 488
Package Names and Directories 489
Name Clashes 492
6.8 GRAPHICS SUPPLEMENT 493
Adding Buttons 493
Adding Icons 493
Chapter 7 Arrays 513
7.1 ARRAY BASICS 515
Creating and Accessing Arrays 516
Array Details 519
The Instance Variable length 522
More About Array Indices 525
Initializing Arrays 528
7.2 ARRAYS IN CLASSES AND METHODS 530
Case Study: Sales Report 530
Indexed Variables as Method Arguments 538
Entire Arrays as Arguments to a Method 541
Arguments for the Method main 542
Array Assignment and Equality 543
Methods That Return Arrays 546
7.3 PROGRAMMING WITH ARRAYS AND CLASSES 550
Programming Example: A Specialized List Class 550
Partially Filled Arrays 558
7.4 SORTING AND SEARCHING ARRAYS 560
Selection Sort 560
Other Sorting Algorithms 564
Searching an Array 566
7.5 MULTIDIMENSIONAL ARRAYS 567
Multidimensional-Array Basics 568
Multidimensional-Array Parameters and Returned Values 571
Java’s Representation of Multidimensional Arrays 574
Ragged Arrays (Optional) 575
Programming Example: Employee Time Records 577
7.6 GRAPHICS SUPPLEMENT 583
Layout Panes 583
Text Areas, Text Fields and Combining Layouts 591
Drawing Polygons 596
Chapter 8 Inheritance, Polymorphism, and Interfaces 621
8.1 INHERITANCE BASICS 622
Derived Classes 624
Overriding Method Definitions 628
Overriding Versus Overloading 629
The final Modifier 629
Private Instance Variables and Private Methods of a Base Class 630
UML Inheritance Diagrams 632
8.2 PROGRAMMING WITH INHERITANCE 635
Constructors in Derived Classes 635
The this Method—Again 637
Calling an Overridden Method 637
Programming Example: A Derived Class of a Derived
Class 638
Another Way to Define the equals Method in
Undergraduate 643
Type Compatibility 643
The Class Object 648
A Better equals Method 650
8.3 POLYMORPHISM 652
Dynamic Binding and Inheritance 652
Dynamic Binding with toString 655
8.4 INTERFACES AND ABSTRACT CLASSES 657
Class Interfaces 657
Java Interfaces 658
Implementing an Interface 659
An Interface as a Type 661
Extending an Interface 664
Case Study: Character Graphics 665
Case Study: The Comparable Interface 678
Abstract Classes 682
8.5 GRAPHICS SUPPLEMENT 684
Event-Driven Programming 685
Event Handling in a Separate Class 686
Event Handling in the Main GUI Application Class 689
Event Handling in an Anonymous Inner Class 691
Programming Example: Adding Numbers 694
Chapter 9 Exception Handling 709
9.1 BASIC EXCEPTION HANDLING 710
Exceptions in Java 711
Predefined Exception Classes 721
9.2 DEFINING YOUR OWN EXCEPTION CLASSES 723
9.3 MORE ABOUT EXCEPTION CLASSES 733
Declaring Exceptions (Passing the Buck) 733
Kinds of Exceptions 736
Errors 738
Multiple Throws and Catches 739
The finally Block 745
Rethrowing an Exception (Optional) 746
Case Study: A Line-Oriented Calculator 747
9.4 GRAPHICS SUPPLEMENT 759
Additional User Interface Controls and Shapes 759
Images and Shapes 763
Handling Mouse Events 765
The Timeline Class 769
Chapter 10 Streams, File I/O, and Networking 785
10.1 AN OVERVIEW OF STREAMS AND FILE I/O 787
The Concept of a Stream 787
Why Use Files for I/O? 788
Text Files and Binary Files 788
10.2 TEXT-FILE I/O 790
Creating a Text File 790
Appending to a Text File 796
Reading from a Text File 798
10.3 TECHNIQUES FOR ANY FILE 801
The Class File 801
Programming Example: Reading a File Name from the
Keyboard 801
Using Path Names 803
Methods of the Class File 804
Defining a Method to Open a Stream 806
Case Study: Processing a Comma-Separated Values File 808
10.4 BASIC BINARY-FILE I/O 811
Creating a Binary File 811
Writing Primitive Values to a Binary File 813
Writing Strings to a Binary File 816
Some Details About writeUTF 817
Reading from a Binary File 818
The Class EOFException 824
Programming Example: Processing a File of Binary Data 826
10.5 BINARY-FILE I/O WITH OBJECTS AND ARRAYS 831
Binary-File I/O with Objects of a Class 831
Some Details of Serialization 835
Array Objects in Binary Files 836
10.6 NETWORK COMMUNICATION WITH STREAMS 839
10.7 GRAPHICS SUPPLEMENT 845
Programming Example: A JavaFX GUI for Manipulating
Files 845
Chapter 11 Recursion 867
11.1 THE BASICS OF RECURSION 868
Case Study: Digits to Words 871
How Recursion Works 876
Infinite Recursion 880
Recursive Methods Versus Iterative Methods 882
Recursive Methods That Return a Value 884
11.2 PROGRAMMING WITH RECURSION 888
Programming Example: Insisting That User Input Be Correct 888
Case Study: Binary Search 890
Programming Example: Merge Sort—A Recursive Sorting
Method 898
11.3 GRAPHICS SUPPLEMENT 902
Lambda Functions and Event Handlers 902
Chapter 12 Dynamic Data Structures and Generics 919
12.1 ARRAY-BASED DATA STRUCTURES 921
The Class ArrayList 922
Creating an Instance of ArrayList 922
Using the Methods of ArrayList 924
Programming Example: A To-Do List 928
Parameterized Classes and Generic Data Types 931
12.2 THE JAVA COLLECTIONS FRAMEWORK 931
The Collection Interface 931
The Class HashSet 932
The Map Interface 934
The Class HashMap 934
12.3 LINKED DATA STRUCTURES 937
The Class LinkedList 937
Linked Lists 938
Implementing the Operations of a Linked List 941
A Privacy Leak 948
Inner Classes 949
Node Inner Classes 950
Iterators 950
The Java Iterator Interface 962
Exception Handling with Linked Lists 962
Variations on a Linked List 964
Other Linked Data Structures 966
12.4 GENERICS 967
The Basics 967
Programming Example: A Generic Linked List 970
12.5 GRAPHICS SUPPLEMENT 975
Building JavaFX Applications with the Scene Builder 975
Where to Go from Here 981
APPENDICES
1 Getting Java 997
2 Running Applets 998
3 Protected and Package Modifiers 1000
4 The DecimalFormat Class 1001
Other Pattern Symbols 1002
5 Javadoc 1005
Commenting Classes for Use with javadoc 1005
Running javadoc 1006
6 Differences Between C++ and Java 1008
Primitive Types 1008
Strings 1008
Flow of Control 1008
Testing for Equality 1009
main Method (Function) and Other Methods 1009
Files and Including Files 1009
Class and Method (Function) Definitions 1010
No Pointer Types in Java 1010
Method (Function) Parameters 1010
Arrays 1010
Garbage Collection 1011
Other Comparisons 1011
7 Unicode Character Codes 1012
8 Introduction to Java 8 Functional Programming 1013
9 The Iterator Interface 1017
10 Cloning 1019
11 Java Reserved Keywords 1023
CREDITS 1024
INDEX 1027