Computing with Java(TM): Alternate 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 1.7 Introduction to Object-Oriented Design Objects Object-Oriented Design A Definition Use Cases and Scenarios Scenarios for an ATM system Classification The Unified Modeling Language 2. Programming with Objects 2.1 Names Object Behavior Identifiers Keywords The Character Set 2.2 Objects and Classes Simulating a Vending Machine Classes Variables and State Integer Variables Constructors and Identity Methods and Behavior A First Look at Packages 2.3 Using Objects Creating Objects Reference Variables Invoking Behavior A Class for Testing Local Variables Output Using a Package Classpath A Message Dialog 2.4 Arithmetic Expressions Precedence of Arithmetic Operators Combining Assignment and Arithmetic Increment and Decrement Operators Running a Test in a Package 2.5 Parameters and Input Passing by Value An Input Dialog 2.6 Getting Started with 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 Finding the Maximum 3.5 Debugging 3.6 Getting Started with 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 String Tokenizer 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. Object-Oriented Design and Programming 5.1 Using Objects Strings Values vs. References Comparing Strings 5.2 Developing Java Classes 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.3 The QuickFood Example Class Diagrams Sequence Diagrams 5.4 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 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 7.6 GridBag Layout Default GridBag Constraints Setting Weight and Fill Anchoring and Internal Padding Insets Positioning Constraints The Form Applet 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 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 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 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. Threads and Multimedia 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 The Java Media Framework 15.6 Concurrent Programming An Example without Synchronization Synchronization Communication Deadlock 16. Networking 16.1 Using a URL to Connect The Uniform Resource Locator (URL) Connecting from an Applet Connecting from a Standalone Application 16.2 Protocols with a URLConnection The Hypertext Transfer Protocol (HTTP) Using a URL Connection 16.3 Clients and Servers Using Sockets Server Ports A Client-Server Example A Request Header Server 16.4 Browsers and Web Servers A Very Simple Browser A Very Simple Web Server A Threaded Web Server 16.5 Remote Method Invocation (RMI) Distributed Computing: The RMI Solution The Interface The Server The Client 17. Java Database Connectivity (JDBC) 17.1 Database Tables and SQL Queries Relational Database Tables Structured Query Language 17.2 Connecting to a Database Database and Application Servers A JDBC Driver Creating an ODBC Source Connecting from Java Building the Database 17.3 Retrieving Information Viewing Query Results SELECT Statement Options 17.4 Metadata and Aggregate Functions Database Metadata Result Set Metadata Aggregate Functions 17.5 Prepared Statements and Transactions Using Prepared Statements Transaction Processing 17.6 A GUI for Database Queries The List Component 18. Servlets and JavaServer Pages 18.1 A First Servlet Servlets with the JSWDK or JRun Generic Servlets Using the Web Server 18.2 HTML forms Get Requests Post Requests 18.3 Server Side Includes and Servlet Chaining Server-Side Includes Servlet Chaining 18.4 Three-tier Architectures Using a Database in a Three-tiered Architecture 18.5 Session-Tracking 18.6 JavaServer Pages Getting Started with JSP A JSP Response to a Form JSP and JavaBeans Components 19. Java Beans 19.1 Building with Beans Starting the BeanBox Building an Applet with Beans 19.2 Bound and Constrained Properties Simple Properties Bound Properties Constrained Properties 19.3 Writing and Packaging a Bean A Simple Bean JAR Files A Bean with a Bound Property Smiley Bean Anonymous Inner Classes 19.4 Using BeanInfo What is BeanInfo? Friendly Bean BeanInfo for Friendly Packaging Friendly 19.5 Programming with Beans Using Beans in an Applet Customizing Beans 20. XML (Extensible Markup Language) 20.1 XML and Information The Limitations of HTML XML Syntax A Document Tye Definition Valid Documents 20.2 SAX (Simple API for XML) Processing Using a SAX Parser Using a Validating Parser 20.3 DOM (Document Object Model) Processing Building a DOM Tree from an XML File Checking for Well-Formed and Valid Documents Building a DOM Tree from Data 20.4 XSLT (Extensible Stylesheet Language for Transformations) Stylesheets A Stylesheet for the Author Document Using a Stylesheet 20.5 SOAP (Simple Object Access Protocol) A SOAP Message Calling a Method with Arguments Using the SOAP API 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 F. Using Javadoc and Reading Java Documentation G. Listing of Packages, Classes, and Methods