CHAPTER 8Exception Handling569
Objectives570
Prerequisites570
8.1Basic Exception Handling571
Exceptions in Java571
Predefined Exception Classes583
ArrayIndex0ut0fBoundsException (Alternative Ordering)584
8.2Defining Exception Classes585
Defining Your Own Exception Classes585
Programming Tip: When to Define an Exception Class591
8.3Using Exception Classes596
Declaring Exceptions (Passing the Buck)596
Exceptions That Do Not Need To Be Caught600
The Assertion Error Class (Optional)601
Multiple Throws and Catches601
Programming Tip: Exception Handling and Information Hiding605
Programming Tip: When to Throw an Exception607
The final1y Block (Optional)609
Rethrowing an Exception (Optional)610
Case Study: A Line-0riented Calculator611
8.4Graphics Supplement (Optional)625
Exceptions in GUls625
Programming Example: A JFrame Using Exceptions626
Chapter Summary630
Answers to Self-Test Questions630
Programming Projects635
CHAPTER 9Streams and File I/O641
Objectives642
Prerequisites642
9.1An Overview of Streams and File I/O343
The Concept of a Stream643
Why Use Files for I/0?643
Differences between Text Files and Binary Files643
9.2Text-File I/O645
Text-File Output with PrintWriter645
Text-File Input with BufferedReader656
Programming Example: Reading a File Name from the Keyboard660
The StringTokenizer Class663
The Classes Fi1eReader and File0utputStream667
9.3The Fi1e Class669
Using the File Class669
9.4Basic Binary-File I/O672
Output to Binary Files, Using ObjectOutputStream673
Some Details about write UTF (Optional)678
Reading Input from a Binary File, Using ObjectInputStream679
The E0FException Class685
The Classes Fi1elnputStream and Fi1eOutputStream688
Programming Example: Processing a File of Binary Data689
9.5Object I/O with Object Streams393
Binary I/0 of Class Objects693
The Serializable Interface698
Array Objects in Binary Files699
9.6Graphics Supplement (Optional)701
Programming Example: A JFrame GUI for Manipulating Files701
Chapter Summary707
Answers to Self-Test Questions707
Programming Projects713
CHAPTER 10 Dynamic Data Structures and Generics717
Objectives718
Prerequisites718
10.1 Vectors719
Using Vectors719
Programming Tip: Adding to a Vector726
Parameterized Classes and Generics732
10.2 Linked Data Structures733
Linked Lists733
Inner Classes744
Node Inner Classes745
Iterators746
Programming Tip: Internal and External Iterators759
Exception Handling with Linked Lists759
Variations on a Linked List763
Other Linked Data Structures765
10.3 Generics765
Generic Basics766
Programming Example: A Generic Linked List768
Chapter Summary774
Answers to Self-Test Questions774
Programming Projects783
CHAPTER 11 Recursion787
Objectives788
Prerequisites788
11.1 The Basics of Recursion788
Case Study: Digits to Words789
How Recursion Works794
Recursive versus Iterative Definitions801
Recursive Methods That Return a Value801
11.2 Programming with Recursion806
Programming Tip: Ask Until the User Gets It Right806
Case Study: Binary Search808
Programming Tip: Generalize the Problem812
Programming Example: Merge Sort——A Recursive Sorting Method816
Chapter Summary821
Answers to Self-Test Questions821
Programming Projects823
CHAPTER 12Window Interfaces Using Swing827
Objectives828
Prerequisites828
12.1Background829
Guls——Graphical User Interfaces829
Event-Driven Programming829
12.2Basic Swing Details831
Programming Example: A Simple Window832
More about Window Listeners838
Size Units for Screen Objects840
More on setVisible841
Programming Example: A Better Version of Our First Swing Program843
Programming Example: A Window with Color846
Some Methods of the Class 3 Frame850
Layout Managers853
12.3Buttons and Action Listeners860
Programming Example: Adding Buttons860
Buttons862
Action Listeners and Action Events864
Interfaces869
The Model-View-Controller Pattern871
12.4Container Classes874
The JPanel Class874
12.5The Container Class877
12.6Text I/O for GUIs882
Text Areas and Text Fields882
Programming Example: Labeling a Text Field888
Inputting and Outputting Numbers890
Programming Example: A GUI Adding Machine893
Catching a NumberFormatExcepti on897
Chapter Summary900
Answers to Self-Test Questions901
Programming Projects909
CHAPTER 13Applets and HTML913
Objectives914
Prerequisites914
13.1Applets915
Applet Basics915
Running an Applet918
Programming Example: An Adder Applet918
Adding Icons to an Applet921
13.2Introduction to HTML924
HTML Basics924
Programming Tip: A Simple HTML-Document Outline926
Inserting Hyperlinks926
Displaying a Picture931
Programming Tip: HTML Is a Low-Level Language932
13.3Applets in HTML932
Placing an Applet in an HTML Document932
The Older Applet Class (Optional)935
Applets and Security936
Chapter Summary937
Answers to Self-Test Questions937
Programming Projects938
CHAPTER 14More Swing941
Objectives942
Prerequisites942
14.1Menus942
Programming Example: A GUI with a Menu943
Menu Bars, Menus, and Menu Items943
Nested Menus948
14.2Making GUIs Pretty (and More Functional)950
Adding Icons950
The JScroll Pane Class for Scroll Bars957
Adding Borders963
14.3More Layout Managers969
The Box Layout Manager Class969
Struts and Glue973
Setting the Spacing between Components975
The Box Container Class975
The Card Layout Manager978
14.4Inner Classes984
Helping Classes984
14.5More on Events and Listeners986
The Windowkistener Interface986
Programming Example: Components with Changing Visibility997
Some More Details on Updating a GUI1000
14.6Another Look at the Swing Class Hierarchy1001
Buttons, Menus, and Abstract Buttons1001
Chapter Summary1004
Answers to Self-Test QuestionslOO4
Programming Projects1008