You are previewing Java Illuminated, 4th Edition.
O'Reilly logo
Java Illuminated, 4th Edition

Book Description

Fully revised and updated to reflect the most recent features in Java 7 and Java 8, Java Illuminated, Fourth Edition is one of the most interactive and user-friendly texts available. Comprehensive but accessible, the text takes a progressive approach to object-oriented programming, allowing students to build on established skills to develop new and increasingly complex classes. Java Illuminated follows an activity-based active learning approach that ensures student engagement and interest. This Fourth Edition now includes techniques for producing graphical output and animations using both applets and graphical applications. Packed with real-world applications and student activities, Java Illuminated, Fourth Edition, will draw students in to the world of programming.

Table of Contents

  1. Cover
  2. Title Page
  3. Copyright
  4. Dedication
  5. Contents
  6. Preface
  7. Turing’s Craft CodeLab Student Registration Instructions
  8. Acknowledgments
  9. Chapter 1 Introduction to Programming and the Java Language
    1. 1.1 Basic Computer Concepts
      1. 1.1.1 Hardware
      2. 1.1.2 Operating Systems
      3. 1.1.3 Application Software
      4. 1.1.4 Computer Networks and the Internet
    2. 1.2 Practice Activity: Displaying System Configuration
      1. 1.2.1 Displaying Windows Configuration Information
      2. 1.2.2 Displaying Mac OS Configuration Information
    3. 1.3 Data Representation
      1. 1.3.1 Binary Numbers
      2. 1.3.2 Using Hexadecimal Numbers to Represent Binary Numbers
      3. 1.3.3 Representing Characters with the Unicode Character Set
    4. 1.4 Programming Languages
      1. 1.4.1 High- and Low-Level Languages
      2. 1.4.2 An Introduction to Object-Oriented Programming
      3. 1.4.3 The Java Language
    5. 1.5 An Introduction to Programming
      1. 1.5.1 Programming Basics
      2. 1.5.2 Program Design with Pseudocode
      3. 1.5.3 Developing a Java Application
      4. 1.5.4 Programming Activity 1: Writing a First Java Application
        1. Debugging Techniques
        2. Testing Techniques
    6. 1.6 Chapter Summary
    7. 1.7 Exercises, Problems, and Projects
  10. Chapter 2 Programming Building Blocks—Java Basics
    1. 2.1 Java Application Structure
    2. 2.2 Data Types, Variables, and Constants
      1. 2.2.1 Declaring Variables
      2. 2.2.2 Integer Data Types
      3. 2.2.3 Floating-Point Data Types
      4. 2.2.4 Character Data Type
      5. 2.2.5 Boolean Data Type
      6. 2.2.6 The Assignment Operator, Initial Values, and Literals
      7. 2.2.7 String Literals and Escape Sequences
      8. 2.2.8 Constants
    3. 2.3 Expressions and Arithmetic Operators
      1. 2.3.1 The Assignment Operator and Expressions
      2. 2.3.2 Arithmetic Operators
      3. 2.3.3 Operator Precedence
      4. 2.3.4 Programming Activity 1: Converting Inches to Centimeters
      5. 2.3.5 Integer Division and Modulus
      6. 2.3.6 Division by Zero
      7. 2.3.7 Mixed-Type Arithmetic and Type Casting
      8. 2.3.8 Shortcut Operators
    4. 2.4 Programming Activity 2: Temperature Conversion
    5. 2.5 Chapter Summary
    6. 2.6 Exercises, Problems, and Projects
  11. Chapter 3 Object-Oriented Programming, Part 1: Using Classes
    1. 3.1 Class Basics and Benefits
    2. 3.2 Creating Objects Using Constructors
    3. 3.3 Calling Methods
    4. 3.4 Using Object References
    5. 3.5 Programming Activity 1: Calling Methods
    6. 3.6 The Java Class Library
    7. 3.7 The String Class
      1. The length Method
      2. The toUpperCase and toLowerCase Methods
      3. The indexOf Methods
      4. The charAt and substring Methods
    8. 3.8 Formatting Output with the DecimalFormat Class
    9. 3.9 Generating Random Numbers with the Random Class
    10. 3.10 Input from the Console Using the Scanner Class
    11. 3.11 Calling Static Methods and Using Static Class Variables
    12. 3.12 Using System.in and System.out
    13. 3.13 The Math Class
      1. The pow Method
      2. The round Method
      3. The min and max Methods
    14. 3.14 Formatting Output with the NumberFormat Class
    15. 3.15 The Integer, Double, and Other Wrapper Classes
    16. 3.16 Input and Output Using JOptionPane Dialog Boxes
    17. 3.17 Programming Activity 2: Using Predefined Classes
    18. 3.18 Chapter Summary
    19. 3.19 Exercises, Problems, and Projects
  12. Chapter 4 Introduction to Applets and Graphical Applications
    1. 4.1 Applet Structure
    2. 4.2 Executing an Applet
    3. 4.3 Drawing Shapes with Graphics Methods
    4. 4.4 Using Color
    5. 4.5 Programming Activity 1: Writing an Applet with Graphics
    6. 4.6 Graphical Applications
    7. 4.7 Chapter Summary
    8. 4.8 Exercises, Problems, and Projects
  13. Chapter 5 Flow of Control, Part 1: Selection
    1. 5.1 Forming Conditions
      1. 5.1.1 Equality Operators
      2. 5.1.2 Relational Operators
      3. 5.1.3 Logical Operators
        1. DeMorgan’s Laws
    2. 5.2 Simple Selection with if
    3. 5.3 Selection Using if/else
      1. Block Scope
    4. 5.4 Selection Using if/else if
    5. 5.5 Sequential and Nested if/else Statements
      1. 5.5.1 Sequential if/else Statements
        1. Finding the Minimum or Maximum Values
      2. 5.5.2 Nested if/else Statements
        1. Dangling else
    6. 5.6 Testing Techniques for if/else Statements
    7. 5.7 Programming Activity 1: Working with if/else
    8. 5.8 Comparing Floating-Point Numbers
    9. 5.9 Comparing Objects
      1. 5.9.1 The equals Method
      2. 5.9.2 String Comparison Methods
    10. 5.10 The Conditional Operator (?:)
    11. 5.11 The switch Statement
    12. 5.12 Programming Activity 2: Using the switch Statement
    13. 5.13 Chapter Summary
    14. 5.14 Exercises, Problems, and Projects
  14. Chapter 6 Flow of Control, Part 2: Looping
    1. 6.1 Event-Controlled Loops Using while
    2. 6.2 General Form for while Loops
    3. 6.3 Event-Controlled Looping
      1. 6.3.1 Reading Data from the User
      2. 6.3.2 Reading Data from a Text File
    4. 6.4 Looping Techniques
      1. 6.4.1 Accumulation
      2. 6.4.2 Counting Items
      3. 6.4.3 Calculating an Average
      4. 6.4.4 Finding Maximum or Minimum Values
      5. 6.4.5 Animation
    5. 6.5 Type-Safe Input Using Scanner
    6. 6.6 Constructing Loop Conditions
    7. 6.7 Testing Techniques for while Loops
    8. 6.8 Event-Controlled Loops Using do/while
    9. 6.9 Programming Activity 1: Using while Loops
      1. Task Instructions
      2. Troubleshooting
    10. 6.10 Count-Controlled Loops Using for
      1. 6.10.1 Basic Structure of for Loops
      2. 6.10.2 Constructing for Loops
      3. 6.10.3 Testing Techniques for for Loops
    11. 6.11 Nested Loops
    12. 6.12 Programming Activity 2: Using for Loops
      1. Instructions
      2. Troubleshooting
    13. 6.13 Chapter Summary
    14. 6.14 Exercises, Problems, and Projects
  15. Chapter 7 Object-Oriented Programming, Part 2: User-Defined Classes
    1. 7.1 Defining a Class
    2. 7.2 Defining Instance Variables
    3. 7.3 Writing Class Methods
    4. 7.4 Writing Constructors
    5. 7.5 Writing Accessor Methods
    6. 7.6 Writing Mutator Methods
    7. 7.7 Writing Data Manipulation Methods
    8. 7.8 Programming Activity 1: Writing a Class Definition, Part 1
    9. 7.9 The Object Reference this
    10. 7.10 The toString and equals Methods
    11. 7.11 Static Class Members
    12. 7.12 Graphical Objects
    13. 7.13 Enumeration Types
    14. 7.14 Programming Activity 2: Writing a Class Definition, Part 2
    15. 7.15 Creating Packages
    16. 7.16 Generating Web-Style Documentation with Javadoc
    17. 7.17 Chapter Summary
    18. 7.18 Exercises, Problems, and Projects
  16. Chapter 8 Single-Dimensional Arrays
    1. 8.1 Declaring and Instantiating Arrays
      1. 8.1.1 Declaring Arrays
      2. 8.1.2 Instantiating Arrays
      3. 8.1.3 Combining the Declaration and Instantiation of Arrays
      4. 8.1.4 Assigning Initial Values to Arrays
    2. 8.2 Accessing Array Elements
    3. 8.3 Aggregate Array Operations
      1. 8.3.1 Printing Array Elements
      2. 8.3.2 Reading Data into an Array
      3. 8.3.3 Summing the Elements of an Array
      4. 8.3.4 Finding Maximum or Minimum Values
      5. 8.3.5 Copying Arrays
      6. 8.3.6 Changing the Size of an Array
      7. 8.3.7 Comparing Arrays for Equality
      8. 8.3.8 Displaying Array Data as a Bar Chart
    4. 8.4 Programming Activity 1: Working with Arrays
      1. Instructions
      2. Troubleshooting
    5. 8.5 Using Arrays in Classes
      1. 8.5.1 Using Arrays in User-Defined Classes
      2. 8.5.2 Retrieving Command Line Arguments
    6. 8.6 Searching and Sorting Arrays
      1. 8.6.1 Sequential Search of an Unsorted Array
      2. 8.6.2 Selection Sort
      3. 8.6.3 Insertion Sort
      4. 8.6.4 Sorting Arrays of Objects
      5. 8.6.5 Sequential Search of a Sorted Array
      6. 8.6.6 Binary Search of a Sorted Array
    7. 8.7 Programming Activity 2: Searching and Sorting Arrays
      1. Instructions
      2. Troubleshooting
    8. 8.8 Using Arrays as Counters
    9. 8.9 Accepting a Variable Number of Arguments
    10. 8.10 Chapter Summary
    11. 8.11 Exercises, Problems, and Projects
  17. Chapter 9 Multidimensional Arrays and the ArrayList Class
    1. 9.1 Declaring and Instantiating Multidimensional Arrays
      1. 9.1.1 Declaring Multidimensional Arrays
      2. 9.1.2 Instantiating Multidimensional Arrays
      3. 9.1.3 Combining the Declaration and Instantiation of Multidimensional Arrays
      4. 9.1.4 Assigning Initial Values to Multidimensional Arrays
    2. 9.2 Accessing Multidimensional Array Elements
    3. 9.3 Aggregate Two-Dimensional Array Operations
      1. 9.3.1 Processing All the Elements of a Two-Dimensional Array
      2. 9.3.2 Processing a Given Row of a Two-Dimensional Array
      3. 9.3.3 Processing a Given Column of a Two-Dimensional Array
      4. 9.3.4 Processing a Two-Dimensional Array One Row at a Time
      5. 9.3.5 Processing a Two-Dimensional Array One Column at a Time
      6. 9.3.6 Displaying Two-Dimensional Array Data as a Bar Chart
    4. 9.4 Two-Dimensional Arrays Passed to and Returned from Methods
    5. 9.5 Programming Activity 1: Working with Two-Dimensional Arrays
      1. Instructions
      2. Troubleshooting
    6. 9.6 Other Multidimensional Arrays
    7. 9.7 The ArrayList Class
      1. 9.7.1 Declaring and Instantiating ArrayList Objects
      2. 9.7.2 Methods of the ArrayList Class
      3. 9.7.3 Looping Through an ArrayList Using an Enhanced for Loop
      4. 9.7.4 Using the ArrayList Class in a Program
    8. 9.8 Programming Activity 2: Working with the ArrayList Class
      1. Instructions
      2. Troubleshooting
    9. 9.9 Chapter Summary
    10. 9.10 Exercises, Problems, and Projects
  18. Chapter 10 Object-Oriented Programming, Part 3: Inheritance, Polymorphism, and Interfaces
    1. 10.1 Inheritance
    2. 10.2 Inheritance Design
      1. 10.2.1 Inherited Members of a Class
      2. 10.2.2 Subclass Constructors
      3. 10.2.3 Adding Specialization to the Subclass
      4. 10.2.4 Overriding Inherited Methods
    3. 10.3 The protected Access Modifier
    4. 10.4 Programming Activity 1: Using Inheritance
      1. Instructions
    5. 10.5 Abstract Classes and Methods
    6. 10.6 Polymorphism
    7. 10.7 Programming Activity 2: Using Polymorphism
      1. Instructions
    8. 10.8 Interfaces
    9. 10.9 Chapter Summary
    10. 10.10 Exercises, Problems, and Projects
  19. Chapter 11 Exceptions and Input/Output Operations
    1. 11.1 Simple Exception Handling
      1. 11.1.1 Using try and catch Blocks
      2. 11.1.2 Catching Multiple Exceptions
      3. 11.1.3 User-Defined Exceptions
    2. 11.2 The java.io Package
    3. 11.3 Reading Text Files Using Scanner
    4. 11.4 Writing and Appending to Text Files
      1. 11.4.1 Writing to Text Files
      2. 11.4.2 Appending to Text Files
    5. 11.5 Reading Structured Text Files
      1. 11.5.1 Parsing a String Using Scanner
      2. 11.5.2 Reading Structured Data Using Scanner
    6. 11.6 Programming Activity 1: Reading from a Structured Text File
      1. Instructions
      2. If you have time …
      3. Troubleshooting
    7. 11.7 Reading and Writing Objects to a File
      1. 11.7.1 Writing Objects to Files
      2. 11.7.2 Reading Objects from Files
    8. 11.8 Programming Activity 2: Reading Objects from a File
      1. Task Instructions: Reading from the transactions.obj File
      2. If you have time …
      3. Troubleshooting
    9. 11.9 Chapter Summary
    10. 11.10 Exercises, Problems, and Projects
  20. Chapter 12 Graphical User Interfaces
    1. 12.1 GUI Applications Using JFrame
    2. 12.2 GUI Components
    3. 12.3 A Simple Component: JLabel
    4. 12.4 Event Handling
    5. 12.5 Text Fields
    6. 12.6 Command Buttons
    7. 12.7 Radio Buttons and Checkboxes
    8. 12.8 Programming Activity 1: Working with Buttons
      1. Instructions
      2. Troubleshooting
    9. 12.9 Combo Boxes
    10. 12.10 Adapter Classes
    11. 12.11 Mouse Movements
    12. 12.12 Layout Managers: GridLayout
    13. 12.13 Layout Managers: BorderLayout
    14. 12.14 Using Panels to Nest Components
    15. 12.15 Programming Activity 2: Working with Layout Managers
      1. Instructions
    16. 12.16 Chapter Summary
    17. 12.17 Exercises, Problems, and Projects
  21. Chapter 13 Recursion
    1. 13.1 Simple Recursion: Identifying the General and Base Cases
    2. 13.2 Recursion with a Return Value
      1. 13.2.1 Computing the Factorial of a Number
      2. 13.2.2 Computing the Greatest Common Divisor
    3. 13.3 Recursion with Two Base Cases
    4. 13.4 Programming Activity 1: Checking for a Palindrome
      1. Instructions
      2. Task Instructions
      3. Troubleshooting
    5. 13.5 Binary Search: A Recursive Solution
    6. 13.6 Programming Activity 2: The Towers of Hanoi
      1. Instructions
      2. Task Instructions
      3. Troubleshooting
    7. 13.7 Recursion Versus Iteration
    8. 13.8 Chapter Summary
    9. 13.9 Exercises, Problems, and Projects
  22. Chapter 14 An Introduction to Data Structures
    1. 14.1 Linked Lists
      1. 14.1.1 Linked-List Concepts and Structure
      2. 14.1.2 Linked-List Basics
      3. 14.1.3 Methods of a Linked List
      4. 14.1.4 Testing a Linked-List Class
    2. 14.2 Linked Lists of Objects
      1. 14.2.1 A Linked-List Shell
      2. 14.2.2 Generating an Exception
      3. 14.2.3 Other Methods of a Linked List
      4. 14.2.4 Testing a Linked-List Class
    3. 14.3 Implementing a Stack Using a Linked List
    4. 14.4 Implementing a Queue Using a Linked List
    5. 14.5 Array Representation of Stacks
    6. 14.6 Programming Activity 1: Writing Methods for a Stack Class
      1. Instructions
      2. Troubleshooting
    7. 14.7 Array Representation of Queues
    8. 14.8 Sorted Linked Lists
    9. 14.9 Programming Activity 2: Writing Insert and Delete Methods for a Sorted Linked List
      1. Instructions
      2. Troubleshooting
    10. 14.10 Doubly Linked Lists
    11. 14.11 Linked Lists Using Generic Types
    12. 14.12 Recursively Defined Linked Lists
    13. 14.13 Chapter Summary
    14. 14.14 Exercises, Problems, and Projects
  23. Chapter 15 Running Time Analysis
    1. 15.1 Orders of Magnitude and Big-Oh Notation
    2. 15.2 Running Time Analysis of Algorithms: Counting Statements
    3. 15.3 Running Time Analysis of Algorithms and Impact of Coding: Evaluating Recursive Methods
      1. Handwaving Method
      2. Iterative Method
      3. Proof by Induction Method
      4. Other Methods
    4. 15.4 Programming Activity: Tracking How Many Statements Are Executed by a Method
      1. Instructions
      2. Troubleshooting
    5. 15.5 Running Time Analysis of Searching and Sorting Algorithms
    6. 15.6 Chapter Summary
    7. 15.7 Exercises, Problems, and Projects
  24. Appendix A: Java Reserved Words and Keywords
  25. Appendix B: Operator Precedence
  26. Appendix C: The Unicode Character Set
  27. Appendix D: Representing Negative Integers
  28. Appendix E: Representing Floating-Point Numbers
  29. Appendix F: Java Classes APIs
    1. ActionEvent
    2. ActionListener Interface
    3. ArrayList
    4. BigDecimal
    5. BorderLayout
    6. ButtonGroup
    7. Character
    8. Color
    9. Container
    10. DecimalFormat
    11. Double
    12. Enum
    13. Exception
    14. File
    15. FileOutputStream
    16. FlowLayout
    17. Graphics
    18. GridLayout
    19. Insets
    20. Integer
    21. ItemEvent
    22. ItemListener Interface
    23. JButton
    24. JCheckBox
    25. JComboBox
    26. JComponent
    27. JFrame
    28. JLabel
    29. JOptionPane
    30. JPasswordField
    31. JRadioButton
    32. JTextArea
    33. JTextField
    34. Math
    35. MouseEvent
    36. MouseListener Interface
    37. MouseMotionListener Interface
    38. NumberFormat
    39. ObjectInputStream
    40. ObjectOutputStream
    41. Polygon
    42. PrintWriter
    43. Random
    44. Scanner
    45. String
    46. System
  30. Appendix G: Solutions to Selected Exercises
  31. Index