The Practice and Philosophy of Object-Oriented Programming in Java

Author(s): Drue Coles

Edition: 3

Copyright: 2024

Pages: 488

Choose Your Format

Choose Your Platform | Help Me Choose

Ebook Package

$89.25

ISBN 9798385120932

Details eBook and KHPContent 365 days

Java is a programming language and software platform used by millions of developers worldwide to create applications for everything from mobile devices and desktop computers to large-scale distributed systems and supercomputers. The design and implementation of a software system can be a massively complex process, like building a skyscraper, with teams of developers working for years to design, code, test, debug, configure, and deploy the various components of the system. This complexity can be managed through the use of object orientation, a philosophy of programming based on the creation of abstract bundles of data and behaviors that model meaningful entities in the problem domain.

The Practice and Philosophy of Object-Oriented Programming in Java introduces readers with no prior programming experience to the practice and philosophy of object-oriented programming in Java. After an introduction, the book is partitioned into two parts. Part 1 develops general programming concepts with an object-based perspective through examples involving the use of objects. Objects are defined by classes, and in the first part of the book we work with preexisting classes available to all Java programmers through the standard Java library. In Part 2, we learn how to create our own classes, which amounts to extending the Java language with useful new abstractions. This activity brings us into contact with the cornerstones of object-oriented programming. Practical features of the Java language and the standard library that rest clearly and firmly upon these cornerstones are explored in the rest of the book.

The treatment of important concepts is structured around complete working examples of properly documented code following industry standards for coding style. The examples have evolved over many years of classroom experience, benefiting especially from collaborative experimentation with curious and imaginative students. Complementing the examples are the practice problems at the end of each chapter. Consistent practice is the single most important ingredient of success in almost every difficult endeavor, from programming to swimming to playing a musical instrument, so no effort has been spared to provide a large and diverse collection of practice problems, easy ones, and challenging ones, often with connections to mathematics and the natural sciences.

Students of Java will eventually need to learn about concurrency, networking, streams, database programming, and much more. Although this book does not head in those directions, it provides a foundation for the study of advanced concepts and techniques in Java and flattens the learning curve for the study of other object-oriented languages.

The Practice and Philosophy of Object-Oriented Programming in Java teaches the aspiring Java developer how to:

  • Write properly documented Java code.
  • Create and use objects defined by classes in the standard Java library.
  • Solve programming problems of moderate complexity using loops and arrays.
  • Apply general principles of good programming to write clear and modular code.
  • Implement custom classes with cohesive public interfaces.
  • Use core collection interfaces.
  • Implement interfaces for event handling.
  • Use nested classes and lambda expressions.
  • Create useful new types through inheritance.
  • Design and implement graphical user interfaces.
  • Handle exceptions and perform basic I/O with text files.

Introduction

Chapter 1: The Programming Environment
1.1 What is Programming
1.1.1 Machine View
1.1.2 Low-Level Programming
1.1.3 High-Level Programming
1.1.4 A Note on Terminology
1.1.5 Review Questions
1.2 The Story of Java
1.2.1 Origins
1.2.2 Design Goals of Java
1.2.3 A First Look at Objects and Classes
1.2.4 What is a Philosophy of Programming?
1.2.5 Review Questions
1.3 Installing Java
1.3.1 The Java Virtual Machine
1.3.2 The Java Development Kit
1.3.3 Integrated Development Environments
1.3.4 Getting Started
1.3.5 Review Questions
1.4 The Standard Java Library
1.4.1 Packages and Modules
1.4.2 Application Programming Interfaces
1.4.3 Discovering Methods
1.4.4 Review Questions

Chapter 2: Anatomy of a Simple Program
2.1 An Empty Program
2.1.1 The Main Method
2.1.2 Review Questions
2.2 Console Output
2.2.1 Output Statements
2.2.1 Syntax Errors
2.2.3 Logic Errors
2.2.4 Review Questions
2.3 Variables and Arithmetic
2.3.1 Primitive Types
2.3.2 Variables
2.3.3 Static Typing
2.3.4 Arithmetic Operators
2.3.5 Choosing Variable Names
2.3.6 Review Questions
2.4 Style and Documentation
2.4.1 Internal Documentation
2.4.2 Coding Style
2.4.3 Comments in Java
2.4.4 Javadoc
2.4.5 Review Questions

Part 1: Programming with an Object-Based Perspective
Chapter 3: Using Objects
3.1 Strings
3.1.1 Construction and Concatenation
3.1.2 String Methods
3.1.3 Immutability
3.1.4 Anonymous Objects
3.1.6 Escape Sequences
3.1.7 Unicode
3.1.8 Review Questions
3.2 Constructors
3.2.1 Creating Objects
3.2.2 Import Statements
3.2.3 StringBuilders
3.2.4 Review Questions
3.3 Random Numbers
3.3.1 Pseudorandom Numbers
3.3.2 Factory Methods
3.3.3 Review Questions
3.4 Keyboard Input
3.4.1 I/O Streams
3.4.2 Using Scanners
3.4.3 Static Methods
3.4.4 Type Conversions
3.4.5 Overflow Errors
3.4.6 Review Questions
3.5 Large Numbers
3.5.1 Constructing BigIntegers
3.5.2 Calculating with BigIntegers
3.5.3 Class Constants
3.5.4 Review Questions
3.6 Formatting Numbers
3.6.1 Three Approaches
3.6.2 Review Questions
3.7 Graphics
3.7.1 Widget Toolkits
3.7.2 JavaFX
3.7.3 An Empty Scene
3.7.4 Rectangles
3.7.5 Polygons
3.7.6 Circles and Ellipses
3.7.7 Review Questions
3.8 Practice Problems
3.8.1 String Manipulation
3.8.2 Basic Arithmetic
3.8.3 Formatted Output
3.8.4 Monetary Calculations
3.8.5 BigIntegers
3.8.6 Dates and Times
3.8.7 Graphics

Chapter 4: Helper Methods
4.1 Modularity
4.1.1 Separation of Concerns
4.1.2 Implementing Helper Methods
4.1.3 Eliminating Redundancy
4.1.4 Review Questions
4.2 Practice Problems
4.2.1 Console Applications
4.2.2 Graphics Applications

Chapter 5: Decisions
5.1 Boolean Expressions
5.1.1 Expressions and Operators
5.1.2 Deep Equality
5.1.3 Review Questions
5.2 if Statements
5.2.1 Syntax and Semantics
5.2.2 Review Questions
5.3 if-else Statements
5.3.1 Syntax and Semantics
5.3.2 Ternary Operator
5.3.2 if-else Chains
5.3.3 Review Questions
5.4 switch Statements
5.4.1 Syntax and Semantics
5.4.2 Review Questions
5.5 Conditional and Other Operators
5.5.1 Conditional-OR
5.5.2 Conditional-AND
5.5.3 Negation
5.5.4 Short-Circuit Evaluation
5.5.5 Compound Assignment Operators
5.5.6 Increment and Decrement Operators
5.5.7 Review Questions
5.6 Practice Problems
5.6.1 Console Applications

Chapter 6: Loops
6.1 while and do-while Loops
6.1.1 while loops
6.1.2 do-while Loops
6.1.3 Review Questions
6.2 for Loops
6.2.1 Syntax and Semantics
6.2.2 Case Study: Game of Craps
6.2.3 Case Study: Fractal Generator
6.2.4 Review Questions
6.3 Monte Carlo Simulations
6.3.1 Basic Concepts
6.3.2 Case Study: Random Walks
6.3.3 Case Study: Approximating π
6.3.4 Case Study: Craps Probability
6.3.5 Case Study: Business Decisions
6.3.6 Review Questions
6.4 Nested Loops
6.4.1 Basic Concepts
6.4.2 Case Study: Winning Combinations
6.4.3 Case Study: Drawing a Checkerboard
6.4.4 Review Questions
6.5 Practice Problems
6.5.1 Basic Console Applications
6.5.2 Basic Graphics Applications
6.5.3 Monte Carlo Simulations
6.5.4 Console Applications with Nested Loops
6.5.5 Graphics Applications with Nested Loops

Chapter 7: Arrays
7.1 Linear Arrays
7.1.1 Basic Concepts
7.1.2 Initializer Lists
7.1.3 for-each loops
7.1.4 Command-Line Arguments
7.1.5 Review Questions
7.2 Deep Copy
7.2.1 Comparing Arrays
7.2.2 Copying Arrays
7.2.3 Review Questions
7.3 Balls into Bins
7.3.1 Definition and Applications
7.3.2 Case Study: The Birthday Paradox
7.3.3 Case Study: Randomized Playlist
7.3.4 Review Questions
7.4 ArrayLists
7.4.1 Generic Types
7.4.2 Wrapper Classes
7.4.3 Review Questions
7.5 Two-Dimensional Arrays
7.5.1 Motivation
7.5.2 Arrays of Arrays
7.5.3 Ragged Arrays
7.5.4 Multidimensional Arrays
7.5.5 Review Questions
7.6 Practice Problems
7.6.1 Linear Arrays
7.6.2 Monte Carlo Simulations
7.6.3 Array Lists
7.6.4 Two-Dimensional Arrays

Part 2: Cornerstones of Object-Oriented Programming
Chapter 8: Custom Classes
8.1 Abstraction and Encapsulation
8.1.1 Primitive vs. Complex Types
8.1.2 Models and Abstraction
8.1.3 Encapsulation and Information Hiding
8.1.4 Review Questions
8.2 Instance Variables
8.2.1 Instance vs. Class Variables
8.2.2 Declaring Instance Variables
8.2.3 The Scope of a Variable
8.2.4 Review Questions
8.3 Constructors
8.3.1 Constructors vs. Methods
8.3.2 Shadowing
8.3.3 Overloading Constructors
8.3.4 Review Questions
8.4 Methods
8.4.1 Static Methods vs. Instance Methods
8.4.2 The Public Interface and Cohesion
8.4.3 The Implicit Parameter
8.4.4 Review Questions
8.5 Case Study: Cup of Dice
8.5.1 Client Programs
8.5.2 Review Questions
8.6 Case Study: Game of Fifteen
8.6.1 The Back End
8.6.2 The Object Class
8.6.3 The Front End
8.6.4 Review Questions
8.7 Case Study: Rational Numbers
8.7.1 Beyond Integers
8.7.2 Overriding equals
8.7.3 Overriding hashCode
8.7.4 Review Questions
8.8 Enumerated Types
8.8.1 High-Level View
8.8.2 Low-Level Details
8.8.3 Review Questions
8.9 Practice Problems
8.9.1 Simple Custom Classes
8.9.2 Back Ends for Linear Games
8.9.3 Back Ends for Two-Dimensional Games

Chapter 9: Interfaces and Event Handling
9.1 Motivating the Concept of an Interface
9.1.1 Basic Concepts
9.1.2 Review Questions
9.2 Defining and Using an Interface
9.2.1 What is an Interface?
9.2.2 Implementing an Interface
9.2.3 Using an Interface
9.2.4 Late Binding and Polymorphism
9.2.5 Review Questions
9.3 The Comparable Interface
9.3.1 Basic Concepts
9.3.2 Raw Types
9.3.3 Sorting Arrays
9.3.4 The Natural Ordering
9.3.5 Return to Rational Numbers
9.3.6 Review Questions
9.4 The Comparator Interface
9.4.1 Basic Concepts
9.4.2 Review Questions
9.5 Collection Interfaces
9.5.1 Lists
9.5.2 Sets
9.5.3 Maps
9.5.4 The Iterable<E> Interface
9.5.5 Review Questions
9.6 Action Events
9.6.1 Events
9.6.2 Nested Classes
9.6.3 Action Events
9.6.4 Styling Nodes
9.6.5 More Action Events
9.6.6 Action Event Targets
9.6.7 Review Questions
9.7 Input Events
9.7.1 Basic Concepts
9.7.2 Review Questions
9.8 Lambda Expressions
9.8.1 Functional Programming in Java
9.8.2 Anonymous Classes
9.8.3 Anonymous Methods
9.8.4 Review Questions
9.9 Practice Problems
9.9.1 Implementing the Comparable<T> Interface
9.9.2 Implementing the Comparator<T> Interface
9.9.3 Implementing the EventHandler<T> Interface
9.9.4 Games Played on 2D Arrays
9.9.5 Property Binding

Chapter 10: Inheritance and Polymorphism
10.1 Type Inheritance
10.1.1 Basic Concepts
10.1.2 Review Questions
10.2 Code Inheritance
10.2.1 Subclasses and Superclasses
10.2.2 Code Reuse with Composition
10.2.3 Multiple Inheritance
10.2.4 Review Questions
10.3 Abstract Classes
10.3.1 Basic Concepts
10.3.2 The Number Class
10.3.3 A Small Inheritance Hierarchy
10.3.4 Protected Access
10.3.5 Review Questions
10.4 Subclass Construction
10.4.1 Top-Down Constructor Chaining
10.4.2 Constructor Chaining within the Same Class
10.4.3 Invoking Overridden Methods
10.4.4 Completing the Pet Hierarchy
10.4.5 Review Questions
10.5 Polymorphism Revisited
10.5.1 Basic Concepts
10.5.2 Upcasting
10.5.3 Downcasting
10.5.4 The instanceOf Operator
10.5.5 Review Questions
10.6 Case Study: Particle Simulator
10.6.1 Extending Library Classes
10.6.2 Review Questions
10.7 Case Study: An Ecological Simulation
10.7.1 A Simple Ecosystem
10.7.2 Creature Classes
10.7.3 Simulation Code
10.7.4 Review Questions
10.8 Practice Problems
10.8.1 Extending Library Classes
10.8.2 Custom Inheritance Hierarchies

Chapter 11: Exception Handling
11.1 Exceptional Events
11.1.1 Basic Concepts
11.1.2 Benefits of Formal Exception Handling
11.1.3 The Call Stack and Stack Traces
11.1.4 Review Questions
11.2 Throwing and Catching Exceptions
11.2.1 Errors vs. Exceptions
11.2.2 throw Statements
11.2.3 try-catch Blocks
11.2.4 Review Questions
11.3 Checked and Unchecked Exceptions
11.3.1 Informal Characterization
11.3.2 A Closer Look
11.3.3 Formal Definition
11.3.4 Review Questions
11.4 The Catch or Specify Requirement
11.4.1 A Special Rule for Checked Exceptions
11.4.2 Controversy
11.4.3 Review Questions
11.5 Case Study: File I/O
11.5.1 Binary Files vs. Text Files
11.5.2 Byte Streams
11.5.3 Character Streams
11.5.4 Line-Oriented Character Streams
11.5.5 Formatted I/O
11.5.6 Review Questions
11.6 Practice Problems
11.6.1 File I/O

Drue Coles

Drue Coles is a Professor of Computer Science at Commonwealth University of Pennsylvania, where he teaches Java, C++, mobile device application development, analysis of algorithms, and other courses. He holds a PhD in Computer Science and an MS in Computer Information Systems, both from Boston University.

Java is a programming language and software platform used by millions of developers worldwide to create applications for everything from mobile devices and desktop computers to large-scale distributed systems and supercomputers. The design and implementation of a software system can be a massively complex process, like building a skyscraper, with teams of developers working for years to design, code, test, debug, configure, and deploy the various components of the system. This complexity can be managed through the use of object orientation, a philosophy of programming based on the creation of abstract bundles of data and behaviors that model meaningful entities in the problem domain.

The Practice and Philosophy of Object-Oriented Programming in Java introduces readers with no prior programming experience to the practice and philosophy of object-oriented programming in Java. After an introduction, the book is partitioned into two parts. Part 1 develops general programming concepts with an object-based perspective through examples involving the use of objects. Objects are defined by classes, and in the first part of the book we work with preexisting classes available to all Java programmers through the standard Java library. In Part 2, we learn how to create our own classes, which amounts to extending the Java language with useful new abstractions. This activity brings us into contact with the cornerstones of object-oriented programming. Practical features of the Java language and the standard library that rest clearly and firmly upon these cornerstones are explored in the rest of the book.

The treatment of important concepts is structured around complete working examples of properly documented code following industry standards for coding style. The examples have evolved over many years of classroom experience, benefiting especially from collaborative experimentation with curious and imaginative students. Complementing the examples are the practice problems at the end of each chapter. Consistent practice is the single most important ingredient of success in almost every difficult endeavor, from programming to swimming to playing a musical instrument, so no effort has been spared to provide a large and diverse collection of practice problems, easy ones, and challenging ones, often with connections to mathematics and the natural sciences.

Students of Java will eventually need to learn about concurrency, networking, streams, database programming, and much more. Although this book does not head in those directions, it provides a foundation for the study of advanced concepts and techniques in Java and flattens the learning curve for the study of other object-oriented languages.

The Practice and Philosophy of Object-Oriented Programming in Java teaches the aspiring Java developer how to:

  • Write properly documented Java code.
  • Create and use objects defined by classes in the standard Java library.
  • Solve programming problems of moderate complexity using loops and arrays.
  • Apply general principles of good programming to write clear and modular code.
  • Implement custom classes with cohesive public interfaces.
  • Use core collection interfaces.
  • Implement interfaces for event handling.
  • Use nested classes and lambda expressions.
  • Create useful new types through inheritance.
  • Design and implement graphical user interfaces.
  • Handle exceptions and perform basic I/O with text files.

Introduction

Chapter 1: The Programming Environment
1.1 What is Programming
1.1.1 Machine View
1.1.2 Low-Level Programming
1.1.3 High-Level Programming
1.1.4 A Note on Terminology
1.1.5 Review Questions
1.2 The Story of Java
1.2.1 Origins
1.2.2 Design Goals of Java
1.2.3 A First Look at Objects and Classes
1.2.4 What is a Philosophy of Programming?
1.2.5 Review Questions
1.3 Installing Java
1.3.1 The Java Virtual Machine
1.3.2 The Java Development Kit
1.3.3 Integrated Development Environments
1.3.4 Getting Started
1.3.5 Review Questions
1.4 The Standard Java Library
1.4.1 Packages and Modules
1.4.2 Application Programming Interfaces
1.4.3 Discovering Methods
1.4.4 Review Questions

Chapter 2: Anatomy of a Simple Program
2.1 An Empty Program
2.1.1 The Main Method
2.1.2 Review Questions
2.2 Console Output
2.2.1 Output Statements
2.2.1 Syntax Errors
2.2.3 Logic Errors
2.2.4 Review Questions
2.3 Variables and Arithmetic
2.3.1 Primitive Types
2.3.2 Variables
2.3.3 Static Typing
2.3.4 Arithmetic Operators
2.3.5 Choosing Variable Names
2.3.6 Review Questions
2.4 Style and Documentation
2.4.1 Internal Documentation
2.4.2 Coding Style
2.4.3 Comments in Java
2.4.4 Javadoc
2.4.5 Review Questions

Part 1: Programming with an Object-Based Perspective
Chapter 3: Using Objects
3.1 Strings
3.1.1 Construction and Concatenation
3.1.2 String Methods
3.1.3 Immutability
3.1.4 Anonymous Objects
3.1.6 Escape Sequences
3.1.7 Unicode
3.1.8 Review Questions
3.2 Constructors
3.2.1 Creating Objects
3.2.2 Import Statements
3.2.3 StringBuilders
3.2.4 Review Questions
3.3 Random Numbers
3.3.1 Pseudorandom Numbers
3.3.2 Factory Methods
3.3.3 Review Questions
3.4 Keyboard Input
3.4.1 I/O Streams
3.4.2 Using Scanners
3.4.3 Static Methods
3.4.4 Type Conversions
3.4.5 Overflow Errors
3.4.6 Review Questions
3.5 Large Numbers
3.5.1 Constructing BigIntegers
3.5.2 Calculating with BigIntegers
3.5.3 Class Constants
3.5.4 Review Questions
3.6 Formatting Numbers
3.6.1 Three Approaches
3.6.2 Review Questions
3.7 Graphics
3.7.1 Widget Toolkits
3.7.2 JavaFX
3.7.3 An Empty Scene
3.7.4 Rectangles
3.7.5 Polygons
3.7.6 Circles and Ellipses
3.7.7 Review Questions
3.8 Practice Problems
3.8.1 String Manipulation
3.8.2 Basic Arithmetic
3.8.3 Formatted Output
3.8.4 Monetary Calculations
3.8.5 BigIntegers
3.8.6 Dates and Times
3.8.7 Graphics

Chapter 4: Helper Methods
4.1 Modularity
4.1.1 Separation of Concerns
4.1.2 Implementing Helper Methods
4.1.3 Eliminating Redundancy
4.1.4 Review Questions
4.2 Practice Problems
4.2.1 Console Applications
4.2.2 Graphics Applications

Chapter 5: Decisions
5.1 Boolean Expressions
5.1.1 Expressions and Operators
5.1.2 Deep Equality
5.1.3 Review Questions
5.2 if Statements
5.2.1 Syntax and Semantics
5.2.2 Review Questions
5.3 if-else Statements
5.3.1 Syntax and Semantics
5.3.2 Ternary Operator
5.3.2 if-else Chains
5.3.3 Review Questions
5.4 switch Statements
5.4.1 Syntax and Semantics
5.4.2 Review Questions
5.5 Conditional and Other Operators
5.5.1 Conditional-OR
5.5.2 Conditional-AND
5.5.3 Negation
5.5.4 Short-Circuit Evaluation
5.5.5 Compound Assignment Operators
5.5.6 Increment and Decrement Operators
5.5.7 Review Questions
5.6 Practice Problems
5.6.1 Console Applications

Chapter 6: Loops
6.1 while and do-while Loops
6.1.1 while loops
6.1.2 do-while Loops
6.1.3 Review Questions
6.2 for Loops
6.2.1 Syntax and Semantics
6.2.2 Case Study: Game of Craps
6.2.3 Case Study: Fractal Generator
6.2.4 Review Questions
6.3 Monte Carlo Simulations
6.3.1 Basic Concepts
6.3.2 Case Study: Random Walks
6.3.3 Case Study: Approximating π
6.3.4 Case Study: Craps Probability
6.3.5 Case Study: Business Decisions
6.3.6 Review Questions
6.4 Nested Loops
6.4.1 Basic Concepts
6.4.2 Case Study: Winning Combinations
6.4.3 Case Study: Drawing a Checkerboard
6.4.4 Review Questions
6.5 Practice Problems
6.5.1 Basic Console Applications
6.5.2 Basic Graphics Applications
6.5.3 Monte Carlo Simulations
6.5.4 Console Applications with Nested Loops
6.5.5 Graphics Applications with Nested Loops

Chapter 7: Arrays
7.1 Linear Arrays
7.1.1 Basic Concepts
7.1.2 Initializer Lists
7.1.3 for-each loops
7.1.4 Command-Line Arguments
7.1.5 Review Questions
7.2 Deep Copy
7.2.1 Comparing Arrays
7.2.2 Copying Arrays
7.2.3 Review Questions
7.3 Balls into Bins
7.3.1 Definition and Applications
7.3.2 Case Study: The Birthday Paradox
7.3.3 Case Study: Randomized Playlist
7.3.4 Review Questions
7.4 ArrayLists
7.4.1 Generic Types
7.4.2 Wrapper Classes
7.4.3 Review Questions
7.5 Two-Dimensional Arrays
7.5.1 Motivation
7.5.2 Arrays of Arrays
7.5.3 Ragged Arrays
7.5.4 Multidimensional Arrays
7.5.5 Review Questions
7.6 Practice Problems
7.6.1 Linear Arrays
7.6.2 Monte Carlo Simulations
7.6.3 Array Lists
7.6.4 Two-Dimensional Arrays

Part 2: Cornerstones of Object-Oriented Programming
Chapter 8: Custom Classes
8.1 Abstraction and Encapsulation
8.1.1 Primitive vs. Complex Types
8.1.2 Models and Abstraction
8.1.3 Encapsulation and Information Hiding
8.1.4 Review Questions
8.2 Instance Variables
8.2.1 Instance vs. Class Variables
8.2.2 Declaring Instance Variables
8.2.3 The Scope of a Variable
8.2.4 Review Questions
8.3 Constructors
8.3.1 Constructors vs. Methods
8.3.2 Shadowing
8.3.3 Overloading Constructors
8.3.4 Review Questions
8.4 Methods
8.4.1 Static Methods vs. Instance Methods
8.4.2 The Public Interface and Cohesion
8.4.3 The Implicit Parameter
8.4.4 Review Questions
8.5 Case Study: Cup of Dice
8.5.1 Client Programs
8.5.2 Review Questions
8.6 Case Study: Game of Fifteen
8.6.1 The Back End
8.6.2 The Object Class
8.6.3 The Front End
8.6.4 Review Questions
8.7 Case Study: Rational Numbers
8.7.1 Beyond Integers
8.7.2 Overriding equals
8.7.3 Overriding hashCode
8.7.4 Review Questions
8.8 Enumerated Types
8.8.1 High-Level View
8.8.2 Low-Level Details
8.8.3 Review Questions
8.9 Practice Problems
8.9.1 Simple Custom Classes
8.9.2 Back Ends for Linear Games
8.9.3 Back Ends for Two-Dimensional Games

Chapter 9: Interfaces and Event Handling
9.1 Motivating the Concept of an Interface
9.1.1 Basic Concepts
9.1.2 Review Questions
9.2 Defining and Using an Interface
9.2.1 What is an Interface?
9.2.2 Implementing an Interface
9.2.3 Using an Interface
9.2.4 Late Binding and Polymorphism
9.2.5 Review Questions
9.3 The Comparable Interface
9.3.1 Basic Concepts
9.3.2 Raw Types
9.3.3 Sorting Arrays
9.3.4 The Natural Ordering
9.3.5 Return to Rational Numbers
9.3.6 Review Questions
9.4 The Comparator Interface
9.4.1 Basic Concepts
9.4.2 Review Questions
9.5 Collection Interfaces
9.5.1 Lists
9.5.2 Sets
9.5.3 Maps
9.5.4 The Iterable<E> Interface
9.5.5 Review Questions
9.6 Action Events
9.6.1 Events
9.6.2 Nested Classes
9.6.3 Action Events
9.6.4 Styling Nodes
9.6.5 More Action Events
9.6.6 Action Event Targets
9.6.7 Review Questions
9.7 Input Events
9.7.1 Basic Concepts
9.7.2 Review Questions
9.8 Lambda Expressions
9.8.1 Functional Programming in Java
9.8.2 Anonymous Classes
9.8.3 Anonymous Methods
9.8.4 Review Questions
9.9 Practice Problems
9.9.1 Implementing the Comparable<T> Interface
9.9.2 Implementing the Comparator<T> Interface
9.9.3 Implementing the EventHandler<T> Interface
9.9.4 Games Played on 2D Arrays
9.9.5 Property Binding

Chapter 10: Inheritance and Polymorphism
10.1 Type Inheritance
10.1.1 Basic Concepts
10.1.2 Review Questions
10.2 Code Inheritance
10.2.1 Subclasses and Superclasses
10.2.2 Code Reuse with Composition
10.2.3 Multiple Inheritance
10.2.4 Review Questions
10.3 Abstract Classes
10.3.1 Basic Concepts
10.3.2 The Number Class
10.3.3 A Small Inheritance Hierarchy
10.3.4 Protected Access
10.3.5 Review Questions
10.4 Subclass Construction
10.4.1 Top-Down Constructor Chaining
10.4.2 Constructor Chaining within the Same Class
10.4.3 Invoking Overridden Methods
10.4.4 Completing the Pet Hierarchy
10.4.5 Review Questions
10.5 Polymorphism Revisited
10.5.1 Basic Concepts
10.5.2 Upcasting
10.5.3 Downcasting
10.5.4 The instanceOf Operator
10.5.5 Review Questions
10.6 Case Study: Particle Simulator
10.6.1 Extending Library Classes
10.6.2 Review Questions
10.7 Case Study: An Ecological Simulation
10.7.1 A Simple Ecosystem
10.7.2 Creature Classes
10.7.3 Simulation Code
10.7.4 Review Questions
10.8 Practice Problems
10.8.1 Extending Library Classes
10.8.2 Custom Inheritance Hierarchies

Chapter 11: Exception Handling
11.1 Exceptional Events
11.1.1 Basic Concepts
11.1.2 Benefits of Formal Exception Handling
11.1.3 The Call Stack and Stack Traces
11.1.4 Review Questions
11.2 Throwing and Catching Exceptions
11.2.1 Errors vs. Exceptions
11.2.2 throw Statements
11.2.3 try-catch Blocks
11.2.4 Review Questions
11.3 Checked and Unchecked Exceptions
11.3.1 Informal Characterization
11.3.2 A Closer Look
11.3.3 Formal Definition
11.3.4 Review Questions
11.4 The Catch or Specify Requirement
11.4.1 A Special Rule for Checked Exceptions
11.4.2 Controversy
11.4.3 Review Questions
11.5 Case Study: File I/O
11.5.1 Binary Files vs. Text Files
11.5.2 Byte Streams
11.5.3 Character Streams
11.5.4 Line-Oriented Character Streams
11.5.5 Formatted I/O
11.5.6 Review Questions
11.6 Practice Problems
11.6.1 File I/O

Drue Coles

Drue Coles is a Professor of Computer Science at Commonwealth University of Pennsylvania, where he teaches Java, C++, mobile device application development, analysis of algorithms, and other courses. He holds a PhD in Computer Science and an MS in Computer Information Systems, both from Boston University.