Computing with Java: Programs, Objects, Graphics Second Edition Copyright 1998, 2001 by Art Gittleman Table of Contents 1. Computing with Java 1.1 Introduction to Computing Hardware Software Imaginary Processor ABC Imaginary Processor XYZ High-Level Languages Compilers Interpreters Newtorks 1.2 Overview of Java History Java's Features How Java Works 1.3 Running Java Programs and Applets Editing, Compiling, and Running a Java Application Editing, Compiling, and Running a Java Applet A Graphical Applet 1.4 Program-driven vs. Event-driven Applications The Character Mode The Graphical User Interface (GUI) 1.5 Procedural vs. Object-Oriented Programming Procedural Programming Object-Oriented Programming 1.6 A First Look at Everything: A User Interface Example 2. Java Progamming Basics 2.1 Identifiers and Keywords The Character Set 2.2 Variables, Assignments, and Output 2.3 Arithmetic Expressions Precedence of Arithmetic Operators Combining Assignment and Arithmetic Increment and Decrement Operators 2.4 Methods and Parameters Methods Passing by Value 2.5 Input and a First Look at Packages An Input Dialog Using a Package 2.6 Getting Started with Objects, Applets, and Events Objects Graphics Mode Applets Events 3. Software Engineering with Control Structures 3.1 Relational Operators and Expressions 3.2 If and If-Else Statements The If Statement Control Flow The If-Else Statement Blocks 3.3 The Type Double Scientific Notation Double Values Output Input and Formatted Output Mixed-type Expressions 3.4 Program Design with the While Loop Repetition Planning Programs using Pseudocode Loop Termination 3.5 Problem-Solving Methods: Stepwise Refinement Stepwise Refinement The Steps of Stepwise Refinement Finding the Refinements From a Problem Statement to Java Code 3.6 Debugging 3.7 Getting Started with Objects and Applets: Drawing Shapes Graphics Drawing Drawing Relative to the Screen 4. More Control Structures and Types 4.1 The AND, OR, and NOT Operators Conditional AND Conditional OR Operator Precedence Combining AND with OR 4.2 Nested-Ifs and the Switch Statement Nested If Statements Pairing Else with If The Switch Statement 4.3 The For and Do-While Loops The For Statement The Do-While Statement 4.4 Additional Primitive Types The Char Type The Long Type The Byte, Short, and Float Types 4.5 Using the Math Library Powers and Roots Maximum, Minimum, and Absolute Value Floor and Ceiling Pi and e Random Numbers 4.6 Solving Problems with Java: An Iterative Development Cycle Defining the Problem Toward a Solution: The Iterative Process -- Develop pseudocode, a Java program, and Test Toward a Solution: First Iteration -- Pseudocode, Program, Test Toward a Solution: Second Iteration -- Pseudocode, Program, Test Completing the Java Code: Third Iteration -- Pseudocode, Program, Test Testing the Java Code 5. Getting Started with Object-Oriented Programming 5.1 Using Objects Defining an Object Strings Values vs. References Comparing Strings 5.2 Introduction to Object-Oriented Design A Definition Use Cases and Scenarios Scenarios for an ATM system Classification 5.3 Classes and Objects in Java The Structure of the BankAccount Class Instance Variables Instance Methods Constructors Using BankAccount Objects Creating Objects Making an Object Behave Method Overloading Class Variables and Methods 5.4 The QuickFood Example Class Diagrams Sequence Diagrams 5.5 Object Composition 6. Event-Driven Programming 6.1 The World Wide Web and HTML Protocols Clients and Servers The URL HTML 6.2 Paint Events Responding to Paint Events 6.3 Drawing Text Fonts Initializing an Applet Font Metrics 6.4 Using Color Creating Colors 3D Rectangles 6.5 Interfaces Declaring an Interface Implementing an Interface 6.6 Event Listeners 6.7 A First Look at Inheritance and the AWT The Component Class Introducing Inheritance The Container Class 7. User Interfaces 7.1 Using a Layout The Component Hierarchy Adding Buttons to a Container Flow Layout Drawing on a Canvas 7.2 Button and TextField The Java Event Model and Action Events Making Buttons Work Using Labels Number Formatting Wrapper Classes Entering Data in Text Fields 7.3 Checkbox and Choice Inner Classes Checkbox and Choice with a Button Checkbox and Choice Item Events 7.4 Border and Grid Layouts The Border Layout Panels The Grid Layout 7.5 List and TextArea List TextArea StringBuffer 8. Arrays 8.1 Using Arrays Changing an Array 8.2 Reference versus Value Again Creating an Array 8.3 Passing Array Arguments and Returning Arrays Passing an Array Argument: The display Method Returning an Array: The readIntArray Method Changing an Array: The reverse Method An Example: Using Methods to Name Operations 8.4 An Example: Simulating Rolls of the Dice 8.5 Arrays of Arrays 8.6 Solving Problems with Java: Insertion Sort Defining the Problem Toward a Solution: Developing the Pseudocode Toward a Solution: Alternatives Completing the Java Code Testing the Code 8.7 A GUI for Insertion Sorting Defining the Problem Designing a Solution: The Exploratory Process Designing a Solution: Making a Chart Designing a Solution: Sorting Designing a Solution: The User Interface Completing the Java Code: Making the User Interface Work Testing the Code 9. Inheritance 9.1 Subclasses Classification Revisited Class Hierarchies Constructing a Subclass Adding State and Behavior to a Subclass Overriding Behavior The Object Classs 9.2 Polymorhphism Polymorphic Operations Compile-time and Run-time Types The BankAccount Hierarchy 9.3 Abstract Classes Declaring an Abstract Class Polymorphism and Abstract Classes A Shape Class Line and Circle 9.4 Modifiers and Access A Second Look at Packages Classpath Class (and Interface) Visibility Data Field and Method Visibility Access to Data Fields Access to Methods 9.5 Object-Oriented Design with Use Cases and Scenarios Defining the Problem Object-Oriented Design--Developing Scenarios Object-Oriented Design--Assigning Responsibilities Object-Oriented Design--Defining the Classes Completing the Java Code Testing the Code 10. Window, Mouse, and Key Events 10.1 Closing Windows Frames Window Events and WindowListener Adapter Classes WindowAdapter 10.2 Applets <--> Standalone Applications Applets to Applications Console Application to GUI Application Standalone Application to Applet 10.3 Using the Mouse Mouse Events MouseListener and MouseMotionListener Moving the Mouse MouseAdapter 10.4 Using the Keyboard Focus Key Events KeyListener 10.5 Solving Problems with Java: Drawing the Tangram Puzzle Defining the Problem Designing a Solution: Starting with One Polygon Designing a Solution: Solving a Simpler Problem Completing the Java Code Testing the Code 10.6 Solving Problems with Java: Tangrams with the Mouse and Keys Defining the Problem Designing a Solution: Solving a Subproblem -- Rotating a Polygon Completing the Java Code 11. Exception Handling and Input/Output 11.1 Program Arguments and Applet Parameters Program Arguments Applet Parameters 11.2 Exception Handling Exception Classes The Array Index Out of Bounds Exception The Number Format Exception 11.3 Text Input and Output Reading from a File Reading Lines and Fields Writing to a File Reading an Integer 11.4 Binary and Object Input and Output The File Class Reading and Writing Bytes Reading and Writing Primitive Types Random Access Files Reading and Writing Objects 12. Swing Components 12.1 Starting to Swing The Swing Classes Converting an AWT Applet to Swing Text Components and Labels A Swing Application with Look and Feel Demo 12.2 Images and Buttons Images Buttons Automatic Double Buffering 12.3 Lists and Toggle Buttons Lists Scroll Panes Checkboxes Radio Buttons Combo Boxes 12.4 Menus and Dialogs Menus Dialogs 12.5 Tabs and Tables Tabbed Panes Tables 13. Data Structures 13.1 Recursion: Searching and Sorting Binary Search Merge Sort 13.2 Linked Lists 13.3 Stacks and Queues Stacks Queues 13.4 Vectors and Enumerations 13.5 Hash Tables 14. Collections 14.1 The Collection Interfaces and Classes The Collection Interface Sets Iterators Implementations 14.2 Lists Implementations 14.3 Maps Implementations 14.4 Comparisons and Orderings Inheriting From Object Overriding Object Methods The Comparable Interface Sorted Sets and Maps 14.5 Algorithms 15. Multimedia and Networking 15.1 Introduction to Threads The Thread Class The Runnable Interface 15.2 Animation 15.3 Images Double Buffering Using Swing 15.4 Sound 15.5 Introduction to Networking Writing a Server and a Client Appendices A. Binary and Hexadecimal Numbers B. Bitwise and Shift Operators C. Operator Precedence Rules D. The ASCII Character Set E. Using Java with Various Environments