You are previewing Java Gently, Third Edition.
O'Reilly logo
Java Gently, Third Edition

Book Description

The third edition of Java Gently by Judith Bishop continues the successful approach that made earlier versions popular and has added improvements which will maintain its place as a worldwide bestseller.  Java Gently teaches the reader how to program and how to do it in the best possible style in Java. In the process, it details the fundamental structures of the Java 2 language and most of its core libraries and utilities. The book covers object-orientation, software design, structured programming, graphical user interfacing, event-driven programming, networking, and an introduction to data structures.  Java Gently gets students started on meaningful input/output in an object-oriented way without hiding basic concepts. Applets, multimedia, graphics, and networking are introduced as students encounter and can handle classes, objects, instantiation, and inheirtance.  The textbook's excellent pedagogy reinforces understanding and demonstrates good programming practice. The three kinds of diagrams include class, form, and algorithm diagrams. The fully worked examples have been carefully chosen to illustrate recently introduced concepts and solve real-world problems in a user-friendly manner. End of chapter multiple choice quizzes and problems allow students to test their comprehension of the material.

Table of Contents

  1. Copyright
    1. Dedication
  2. List of Examples and Case Studies
    1. Trademark Notice
  3. Preface to the Third Edition
    1. A philosophy of teaching Java
    2. Java Gently’s approach
    3. Examples
    4. Teaching programming vs teaching Java
    5. Order of topics
    6. Teaching aids and the web site
    7. Who the book is for
    8. Acknowledgements
  4. The Development of Java Gently
  5. 1. Introduction
    1. 1.1. Welcome to Java
      1. The Nature Conservation project
    2. 1.2. Software development
      1. The programming process
      2. Operating systems
      3. Editors and compilers
      4. Development environments
      5. Maintenance
    3. 1.3. The approach of this book
      1. Algorithms
      2. Class design
      3. Program
      4. Summaries
    4. 1.4. Getting started with Java
      1. Downloading the JDK
      2. Testing the system
      3. The javagently package
      4. The documentation and tutorial
      5. Java integrated development environments
      6. Web sites for Java
    5. 1.5. The Java Gently web site
    6. Summary
    7. Quiz: To Get You Started
  6. 2. Simple programs
    1. 2.1. Three starter programs
      1. EXAMPLE 2.1 Welcome
      2. EXAMPLE 2.2 Drawing a flag
      3. EXAMPLE 2.3 Curio Store
    2. 2.2. The structure of a program
      1. Basic concepts
      2. Model diagrams
      3. Syntax forms for Java
      4. Java semantics
      5. Identifiers and keywords
      6. Java symbols
      7. The creation and running of a program
    3. 2.3. Basics of object orientation
      1. Classes
      2. Objects
      3. Variables
      4. Depicting instantiation
      5. Declaring object variables and creating objects
      6. Accessing members of an object
      7. Manipulating object variables
      8. Constructors and their parameters
    4. 2.4. Beginning with output
      1. Strings for printing
        1. EXAMPLE 2.4 Displaying a warning
      2. Concatenation
        1. EXAMPLE 2.5 Curio shop table
      3. Layout
    5. 2.5. Simple calculations
      1. Types
      2. Declaring variables
      3. Expressions
      4. Assignment
      5. Precedence
      6. Printing expressions
      7. The toString method
        1. EXAMPLE 2.6 Fleet timetables
      8. More about testing
    6. Summary
    7. Quiz
    8. Problems
  7. 3. Types and methods
    1. 3.1. Talking about types
      1. Printing integers and reals
      2. The Math class
      3. EXAMPLE 3.1 Math class investigation
      4. Class methods
      5. Typed methods
      6. Conversions between types
    2. 3.2. Repetition with for-loops
      1. Simple conditions
      2. The form of a for-loop
      3. EXAMPLE 3.2 Multiple labels
      4. Using the loop variable
      5. EXAMPLE 3.3 Conversion table
      6. Other looping options
    3. 3.3. Making methods
      1. Declaring a method
      2. Calling a method
      3. Void methods for structuring
    4. 3.4. Passing parameters and returning results
      1. EXAMPLE 3.4 A rainfall histogram
      2. Model diagrams for methods
      3. Objects as parameters
      4. Results of a method
    5. 3.5. Typed methods
      1. EXAMPLE 3.5 Selling from the curio store
      2. EXAMPLE 3.6 Large conversion table
    6. 3.6. The type boolean
      1. Conditions
      2. Boolean expressions
      3. Comparison of objects
    7. 3.7. Case Study 1: Pizza Delivery 2U
      1. Problem
      2. Solution
      3. Initial design
      4. Model diagram
      5. Program 1
      6. Testing
      7. The problem revisited
      8. Java’s Date and Calendar classes
      9. Class design for Time
      10. Testing the class
      11. For-loops with objects
      12. Objects as parameters
      13. Program 2
    8. Summary
    9. Quiz
    10. Problems
  8. 4. Input and output
    1. 4.1. Inputting interactively
      1. Input streams
      2. Reading strings
      3. EXAMPLE 4.1 Greetings
      4. Java’s approach to reading numbers
    2. 4.2. User-friendly input–output
      1. Java Gently’s GUI Display Class
      2. The output section
      3. The input section
      4. The Display class’s methods
      5. Starting up the Display class
      6. Inputting via the display
      7. EXAMPLE 4.2 Testing the Time class
      8. Java Gently’s Stream class
      9. Constants
      10. EXAMPLE 4.3 Large temperature table formatted
      11. EXAMPLE 4.4 Alternative greetings programs
      12. Inputting sequences
    3. 4.3. Creating and accessing packages
      1. Model diagrams for packages
    4. 4.4. Selection with if-else
      1. Form of the if-statement
      2. EXAMPLE 4.5 The highest number
      3. EXAMPLE 4.6 Averaging readings
      4. Successive else-ifs
    5. 4.5. File input and output
      1. Outputting to a file
      2. EXAMPLE 4.7 Part codes
    6. 4.6. Handling exceptions
      1. Catching an exception
      2. Throwing exceptions
      3. EXAMPLE 4.8 Part codes with exceptions
      4. Recovering from an exception
      5. EXAMPLE 4.9 Secure file opener
      6. Number exceptions
      7. EXAMPLE 4.10 Olympic medals
    7. Summary
    8. Quiz
    9. Problems
  9. 5. Controlling the flow
    1. 5.1. Properties of a good program
      1. Correctness
      2. Readability
      3. Reusability
      4. Efficiency
      5. Method design
      6. Top-down programming
      7. Protection and accessibility
      8. Guidelines for class design
    2. 5.2. Conditional loops with while- and do-statements
      1. The form of conditional loops
      2. EXAMPLE 5.1 Highest common factor
      3. EXAMPLE 5.2 Curio store open all day
      4. The do-statement
      5. Developing a conditional loop
    3. 5.3. User-defined exceptions
      1. EXAMPLE 5.3 Checking on stock availability
    4. 5.4. The type char
      1. Casting characters
      2. EXAMPLE 5.4 Printing out the character set
    5. 5.5. The switch-statement
      1. EXAMPLE 5.5 Exchange rates
      2. When not to use switches
    6. 5.6. Case Study 2: Rock–scissors–paper game
      1. Solution
      2. Step 1: instructions
      3. Step 2: initialize
      4. Step 3: set up a loop
      5. Step 4: generate the computer’s choice
      6. Step 5: getting the user’s choice
      7. Step 6: decide who has won
      8. Step 7: sign off
        1. Program
      9. Multiplayer game
    7. Summary
    8. Quiz
    9. Problems
  10. 6. Arrays and tables
    1. 6.1. Simple arrays
      1. Form of an array
      2. The array model
      3. Accessing array elements
      4. EXAMPLE 6.1 Frequency count
      5. Properties of arrays
        1. Element type
        2. Size
        3. Delayed sizing
        4. Array operator
        5. Element access
        6. Length
        7. Parameters
      6. Array and class interaction
      7. Arrays as an abstraction
      8. EXAMPLE 6.2 Diving competitions
      9. EXAMPLE 6.3 Statistical analysis
      10. EXAMPLE 6.4 The lifetime of light bulbs
    2. 6.2. Tables
      1. EXAMPLE 6.5 Gold exploration
    3. 6.3. The Graph class
      1. Graph class methods
      2. How Graph works
      3. Advanced features
      4. EXAMPLE 6.6 Rainfall statistics
    4. 6.4. Sorting and searching
      1. Selection sort
      2. Searching
      3. EXAMPLE 6.7 Sorting words in a file
    5. 6.5. Class-independent tables
      1. The Vector class
      2. Hash tables
      3. Declaring and accessing a hash table
      4. Enumerations
      5. Accessing values in a hash table
      6. EXAMPLE 6.8 Currency converter
    6. Summary
    7. Quiz
    8. Problems
  11. 7. Formatting
    1. 7.1. Strings and string handling
      1. String operations
      2. EXAMPLE 7.1 Removing double spaces
      3. The StringBuffer class
      4. Strings and character arrays
      5. EXAMPLE 7.2 Counterfeit cheques
    2. 7.2. Tokenizers and envelopes
      1. Tokenizers
      2. Changing the delimiter set
      3. Envelopes
    3. 7.3. Inside the Stream class
      1. Algorithms
      2. The Stream class
    4. 7.4. Formatting numbers
      1. The concept of a formatter
      2. The computer’s locale
      3. EXAMPLE 7.3 Where am I?
      4. The NumberFormat class
      5. Formatting real numbers
      6. Currencies
      7. EXAMPLE 7.4 Currency conversion table
      8. Inputting formatted data
    5. 7.5. Dates, calendars and time
      1. EXAMPLE 7.5 Shopping days
      2. Date formatting
      3. Date parsing
      4. EXAMPLE 7.6 Formatting and parsing dates
      5. Time zones
      6. EXAMPLE 7.7 Testing time zones
      7. Localized date formatting
      8. EXAMPLE 7.8 Multilingual dates
    6. Summary
    7. Quiz
    8. Problems
      1. On string handling
      2. On tokenizers
      3. On formatting
      4. On dates, calendars and time
  12. 8. Objects at work
    1. 8.1. Designing an object-oriented program
      1. EXAMPLE 8.1 Nelson’s Coffee Shop
    2. 8.2. Properties of objects
      1. Using references
      2. EXAMPLE 8.2 Illustrating a list
      3. Using null
      4. The this identifier
      5. The Object superclass
      6. Object equality
      7. Object assignment
      8. Class conversions
      9. Modifiers
      10. Static members
      11. Constants
      12. Inner classes
    3. 8.3. Lists and iterators
      1. Java’s List classes
      2. List iterators
    4. 8.4. Case Study 3: Nelson’s Coffee Shop
      1. Opportunity
      2. Algorithms
      3. Converting the program
      4. Program
      5. Testing
    5. Summary
    6. Quiz
    7. Problems
  13. 9. Abstraction and inheritance
    1. 9.1. Class power
      1. Abstraction
      2. Inheritance
      3. Interfaces
    2. 9.2. Inheritance
      1. EXAMPLE 9.1 The Nature hierarchy
      2. Protected accessibility
      3. Superconstructing
      4. Working within a hierarchy
      5. Shadowing variables
      6. Overloading methods
      7. Overriding methods
      8. Dynamic binding
    3. 9.3. Abstraction through interfaces
      1. EXAMPLE 9.2 Sorting
    4. 9.4. Abstract methods and classes
      1. Abstract methods
      2. Abstract classes
    5. 9.5. Case Study 4: Veterinary Tags
      1. Problem
      2. Solution 1 and class design
      3. Solution 2
      4. Program
      5. Sorting the register
    6. 9.5. Serialization
      1. EXAMPLE 9.3 Illustrating Serializable
    7. Summary
    8. Quiz
    9. Problems
  14. 10. Graphics and user interfaces
    1. 10.1. Introduction to the awt and Swing
      1. Overall structure of awt
      2. Swing
    2. 10.2. Putting graphics in a window
      1. Frame – the basic window
      2. Adding graphics to the window
      3. Writing in an awt window with drawString
      4. EXAMPLE 10.1 Virus warning
      5. Closing a window
      6. EXAMPLE 10.2 A weather chart
      7. Using a canvas
      8. EXAMPLE 10.3 Many flags
      9. Awt class and method summary
    3. 10.3. Laying out a GUI
      1. Layout managers
      2. Reacting to buttons
      3. Extended indentation guidelines
      4. EXAMPLE 10.4 Warning with two responses
      5. Other layout managers
      6. Other component options
      7. Panels for grouping
      8. EXAMPLE 10.5 Traffic lights
    4. Summary
    5. Quiz
    6. Problems
  15. 11. Event-driven programming
    1. 11.1. Interaction with text fields
      1. Inputting numbers
      2. Avoiding input
      3. EXAMPLE 11.1 Traffic lights with buttons
    2. 11.2. Events, listeners and handlers
    3. 11.3. Case Study 5: The Flag Designer
      1. Problem
      2. Solution
      3. Constructing the GUI
      4. Reacting to events
      5. Drawing flags
      6. Program
      7. Testing
    4. 11.4. Managing sequences of events
      1. EXAMPLE 11.2 Traffic light calibration
      2. EXAMPLE 11.3 Currency converter with a GUI
    5. 11.5. Case Study 6: Supermarket Till
      1. Problem
      2. Solution
      3. The design of the screen
      4. Event-handling
      5. Data entry
      6. Program
      7. Testing
    6. 11.6. Case Study 7: The Converter with Swing
      1. Swing components
      2. Updating the Rates class
      3. The data structure
      4. Look and feel
      5. Icons
      6. The program
      7. Testing
    7. Summary
    8. Quiz
    9. Problems
  16. 12. Applets in action
    1. 12.1. From applications to applets
      1. Converting an application to an applet
      2. Simple HTML
      3. EXAMPLE 12.1 Virus warning applet
      4. How applets work
    2. 12.2. Applets in browsers
      1. Where are the applets?
      2. EXAMPLE 12.2 Supermarket till applet
      3. Applet security
      4. The PARAM facility
      5. Signed applets
      6. JAR files
      7. EXAMPLE 12.3 The newspaper competition
    3. 12.3. Sound and images
      1. Sound
      2. Images
      3. Media tracking
      4. Moving images
      5. Reacting to mouse events
      6. EXAMPLE 12.4 Catching the duke
    4. 12.4. Case Study 8: The Nature Conservation Project
      1. Web pages
      2. Including an applet
      3. Getting data into an applet
      4. Tree codes
      5. Inside the applet
    5. Summary
    6. Quiz
    7. Problems
  17. 13. Multithreading
    1. 13.1. Introduction to multithreading
      1. Why threads?
    2. 13.2. Setting up threads
      1. EXAMPLE 13.1 Drawing spots
      2. The Thread class
      3. Converting to threads
      4. EXAMPLE 13.2 Traffic lights flashing
      5. Communication among threads
      6. EXAMPLE 13.3 Traffic lights for walking
    3. 13.3. Synchronization among threads
      1. Protecting shared resources
      2. EXAMPLE 13.4 Car parks for a viewpoint
      3. Signalling between threads
    4. 13.4. Inside the Display class
      1. The structure of the Display class
      2. The table of input values
      3. The ready watcher
    5. 13.5. Case Study 9: Walkman Hire
      1. Discussing the solution
      2. Class design
      3. Synchronized objects
      4. Testing
    6. Summary
    7. Quiz
    8. Problems
  18. 14. Networking
    1. 14.1. Connecting via the Internet
      1. URL connections
      2. EXAMPLE 14.1 Converter applet
      3. Fetching images
      4. EXAMPLE 14.2 Fetching an image
    2. 14.2. Ports and sockets
      1. EXAMPLE 14.3 Opening a port
      2. EXAMPLE 14.4 Creating an ATM server
    3. 14.3. Case Study 10: The Chatter system
      1. Keeping track of clients
      2. Broadcasting to all clients
      3. Synchronizing activities
      4. A model diagram
      5. The server
      6. A sample chat session
    4. 14.4. Database connectivity
      1. EXAMPLE 14.5 The converter database
    5. 14.5. Accessing remote objects
      1. EXAMPLE 14.6 Magic number server
    6. 14.6. Case Study 11: The Airport Announcer system
      1. The airlines’ applets
      2. Airline-side HTML
      3. The applets
      4. Connecting via RMI
      5. Running the system
    7. Summary
    8. Quiz
    9. Problems
      1. Using connections and sockets
      2. For database connectivity
      3. On RMI
  19. 15. Data structures and algorithms
    1. 15.1. About data structures
      1. Properties
      2. Representation
      3. Algorithms
      4. What Java provides
    2. 15.2. Linear and binary searching
      1. Binary searching
      2. Conditions for binary search
      3. EXAMPLE 15.1 Animated binary search
    3. 15.3. Quicksort and performance
      1. A look at performance
      2. Comparison of sorts
      3. Comparison of searches
      4. Where is this leading?
    4. 15.4. Stacks and queues
      1. Stack properties
      2. The Stack class
      3. EXAMPLE 15.2 Reversing a sentence
      4. Uses of stacks
      5. Queues
      6. Queue properties
      7. The queue abstract data type
      8. EXAMPLE 15.3 Doctor’s waiting room
      9. Queues using arrays
      10. Queues using lists
    5. 15.5. Linked lists again
      1. Linked list properties
      2. EXAMPLE 15.4 Photograph line-up
    6. 15.6. Bit sets
      1. EXAMPLE 15.5 Training schedules
    7. Summary
    8. Quiz
    9. Problems
  20. A. Modelling notation for Java Gently
    1. Part I. Building blocks
    2. Part II. Associations
  21. B. Collected syntax and semantic forms
  22. C. List of programs, applets and classes
  23. D. Answers to quizzes