| |
| |
| |
Programming with Objects | |
| |
| |
| |
Modeling with Objects | |
| |
| |
| |
Using Models | |
| |
| |
| |
Using Software Objects to Create Models | |
| |
| |
| |
Modeling Robots | |
| |
| |
| |
Understanding Karel's World | |
| |
| |
| |
Avenues, Streets, and Intersections | |
| |
| |
| |
Walls and (other) Things | |
| |
| |
| |
Robots | |
| |
| |
| |
Modeling Robots with Software Objects | |
| |
| |
| |
Attributes | |
| |
| |
| |
Constructors | |
| |
| |
| |
Services | |
| |
| |
| |
Two Example Programs | |
| |
| |
| |
Situations | |
| |
| |
| |
Program Listing | |
| |
| |
| |
Setting up the Initial Situation | |
| |
| |
| |
Sending Messages | |
| |
| |
| |
Tracing a Program | |
| |
| |
| |
Another Example Program | |
| |
| |
| |
The Form of a Java Program | |
| |
| |
| |
Reading Documentation to Learn More | |
| |
| |
| |
Compiling and Executing Programs | |
| |
| |
| |
Compile-Time Errors | |
| |
| |
| |
Run-Time Errors | |
| |
| |
| |
Intent Errors | |
| |
| |
| |
A Brief History of Bugs and Debugging | |
| |
| |
| |
GUI: Creating a Window | |
| |
| |
| |
Displaying a Frame | |
| |
| |
| |
Adding User Interface Components | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Java Program Pattern | |
| |
| |
| |
The Object Instantiation Pattern | |
| |
| |
| |
The Command Invocation Pattern | |
| |
| |
| |
The Sequential Execution Pattern | |
| |
| |
| |
The Display a Frame Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Concept Maps | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Extending Classes with Services | |
| |
| |
| |
Understanding Programs: An Experiment | |
| |
| |
| |
Extending the Robot Class | |
| |
| |
| |
The Vocabulary of Extending Classes | |
| |
| |
| |
The Form of an Extended Class | |
| |
| |
| |
Implementing the Constructor | |
| |
| |
| |
Adding a Service | |
| |
| |
| |
Implementing move3 | |
| |
| |
| |
Implementing turnRight | |
| |
| |
| |
RobotSE | |
| |
| |
| |
Extension vs. Modification | |
| |
| |
| |
Extending the Thing Class | |
| |
| |
| |
Exploring the Thing Class | |
| |
| |
| |
Implementing a Simple Lamp Object | |
| |
| |
| |
Completely Initializing Lamps | |
| |
| |
| |
Fine-Tuning the Lamp Class (optional) | |
| |
| |
| |
Other Subclasses of Thing | |
| |
| |
| |
Fun with Lights (optional) | |
| |
| |
| |
Style | |
| |
| |
| |
White Space and Indentation | |
| |
| |
| |
Identifiers | |
| |
| |
| |
Comments | |
| |
| |
| |
External Documentation (advanced) | |
| |
| |
| |
Meaning and Correctness | |
| |
| |
| |
Modifying Inherited Methods | |
| |
| |
| |
Overriding a Method Definition | |
| |
| |
| |
Method Resolution | |
| |
| |
| |
Side Effects | |
| |
| |
| |
GUI: Extending GUI Components | |
| |
| |
| |
Extending JComponent | |
| |
| |
| |
Overriding paintComponent | |
| |
| |
| |
How paintComponent Is Invoked | |
| |
| |
| |
Extending Icon | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Extended Class Pattern | |
| |
| |
| |
The Constructor Pattern | |
| |
| |
| |
The Parameterless Command Pattern | |
| |
| |
| |
The Draw a Picture Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Developing Methods | |
| |
| |
| |
Solving Problems | |
| |
| |
| |
Stepwise Refinement | |
| |
| |
| |
Identifying the Required Services | |
| |
| |
| |
Refining harvestField | |
| |
| |
| |
Refining harvestTwoRows | |
| |
| |
| |
Refining harvestOneRow | |
| |
| |
| |
Refining goToNextRow | |
| |
| |
| |
Refining positionForNextHarvest | |
| |
| |
| |
The Complete Program | |
| |
| |
| |
Summary of Stepwise Refinement | |
| |
| |
| |
Advantages of Stepwise Refinement | |
| |
| |
| |
Understandable Programs | |
| |
| |
| |
Avoiding Errors | |
| |
| |
| |
Testing and Debugging | |
| |
| |
| |
Future Modifications | |
| |
| |
| |
Pseudocode | |
| |
| |
| |
Variations on the Theme | |
| |
| |
| |
Using Multiple Robots | |
| |
| |
| |
Multiple Robots with Threads (advanced) | |
| |
| |
| |
Factoring Out Differences | |
| |
| |
| |
Private and Protected Methods | |
| |
| |
| |
GUI: Using Helper Methods | |
| |
| |
| |
Declaring Parameters | |
| |
| |
| |
Using Parameters | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Helper Method Pattern | |
| |
| |
| |
The Multiple Threads Pattern | |
| |
| |
| |
The Template Method Pattern | |
| |
| |
| |
The Parameterized Method Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Making Decisions | |
| |
| |
| |
Understanding Two Kinds of Decisions | |
| |
| |
| |
Flowcharts for if and while Statements | |
| |
| |
| |
Examining an if Statement | |
| |
| |
| |
Examining a while Statement | |
| |
| |
| |
The General Forms of the if and while Statements | |
| |
| |
| |
Questions Robots Can Ask | |
| |
| |
| |
Built-In Queries | |
| |
| |
| |
Negating Predicates | |
| |
| |
| |
Testing Integer Queries | |
| |
| |
| |
Reexamining Harvesting a Field | |
| |
| |
| |
Putting a Missing Thing | |
| |
| |
| |
Picking Up a Pile of Things | |
| |
| |
| |
Improving goToNextRow | |
| |
| |
| |
Using the if-else Statement | |
| |
| |
| |
An Example Using if-else | |
| |
| |
| |
Writing Predicates | |
| |
| |
| |
Writing frontIsBlocked | |
| |
| |
| |
Predicates Using Non-Boolean Queries | |
| |
| |
| |
Using Parameters | |
| |
| |
| |
Using a while Statement with a Parameter | |
| |
| |
| |
Using an Assignment Statement with a Loop | |
| |
| |
| |
Revisiting Stepwise Refinement | |
| |
| |
| |
GUI: Scaling Images | |
| |
| |
| |
Using Size Queries | |
| |
| |
| |
Scaling an Image | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Once or Not at All Pattern | |
| |
| |
| |
The Zero or More Times Pattern | |
| |
| |
| |
The Either This or That Pattern | |
| |
| |
| |
The Simple Predicate Pattern | |
| |
| |
| |
The Count-Down Loop Pattern | |
| |
| |
| |
The Scale an Image Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
More Decision Making | |
| |
| |
| |
Constructing while Loops | |
| |
| |
| |
Avoiding Common Errors | |
| |
| |
| |
A Four-Step Process for Constructing while Loops | |
| |
| |
| |
Temporary Variables | |
| |
| |
| |
Counting the Things on an Intersection | |
| |
| |
| |
Tracing with a Temporary Variable | |
| |
| |
| |
Storing the Result of a Query | |
| |
| |
| |
Writing a Query | |
| |
| |
| |
Using a boolean Temporary Variable | |
| |
| |
| |
Scope | |
| |
| |
| |
Nesting Statements | |
| |
| |
| |
Examples Using if and while | |
| |
| |
| |
Nesting with Helper Methods | |
| |
| |
| |
Cascading-if Statements | |
| |
| |
| |
Boolean Expressions | |
| |
| |
| |
Combining Boolean Expressions | |
| |
| |
| |
Simplifying Boolean Expressions | |
| |
| |
| |
Short-Circuit Evaluation | |
| |
| |
| |
Exploring Loop Variations | |
| |
| |
| |
Using a for Statement | |
| |
| |
| |
Using a do-while Loop (optional) | |
| |
| |
| |
Using a while-true Loop (optional) | |
| |
| |
| |
Choosing an Appropriate Looping Statement | |
| |
| |
| |
Coding with Style | |
| |
| |
| |
Use Stepwise Refinement | |
| |
| |
| |
Use Positively Stated Simple Expressions | |
| |
| |
| |
Visually Structure Code | |
| |
| |
| |
GUI: Using Loops to Draw | |
| |
| |
| |
Using the Loop Counter | |
| |
| |
| |
Nesting Selection and Repetition | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Loop-and-a-Half Pattern | |
| |
| |
| |
The Temporary Variable Pattern | |
| |
| |
| |
The Counting Pattern | |
| |
| |
| |
The Query Pattern | |
| |
| |
| |
The Predicate Pattern | |
| |
| |
| |
The Cascading-if Pattern | |
| |
| |
| |
The Counted Loop Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Using Variables | |
| |
| |
| |
Instance Variables in the Robot Class | |
| |
| |
| |
Implementing Attributes with Instance Variables | |
| |
| |
| |
Declaring Instance Variables | |
| |
| |
| |
Accessing Instance Variables | |
| |
| |
| |
Modifying Instance Variables | |
| |
| |
| |
Testing the SimpleBot Class | |
| |
| |
| |
Adding Another Instance Variable: direction | |
| |
| |
| |
Providing Accessor Methods | |
| |
| |
| |
Instance Variables versus Parameter and Temporary Variables | |
| |
| |
| |
Temporary and Parameter Variables | |
| |
| |
| |
Reviewing Temporary Variables | |
| |
| |
| |
Reviewing Parameter Variables | |
| |
| |
| |
Extending a Class with Variables | |
| |
| |
| |
Declaring and Initializing the Variables | |
| |
| |
| |
Maintaining and Using Instance Variables | |
| |
| |
| |
Blank Final Instance Variables | |
| |
| |
| |
Modifying vs. Extending Classes | |
| |
| |
| |
Comparing Kinds of Variables | |
| |
| |
| |
Similarities and Differences | |
| |
| |
| |
Rules of Thumb for Selecting a Variable | |
| |
| |
| |
Temporary versus Instance Variables | |
| |
| |
| |
Printing Expressions | |
| |
| |
| |
Using System.out | |
| |
| |
| |
Using a Debugger | |
| |
| |
| |
GUI: Repainting | |
| |
| |
| |
Instance Variables in Components | |
| |
| |
| |
Triggering a Repaint | |
| |
| |
| |
Animating the Thermometer | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Named Constant Pattern | |
| |
| |
| |
The Instance Variable Pattern | |
| |
| |
| |
The Accessor Method Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
More on Variables and Methods | |
| |
| |
| |
Using Queries to Test Classes | |
| |
| |
| |
Testing a Command | |
| |
| |
| |
Testing a Query | |
| |
| |
| |
Using Multiple Main Methods | |
| |
| |
| |
Using Numeric Types | |
| |
| |
| |
Integer Types | |
| |
| |
| |
Floating-Point Types | |
| |
| |
| |
Converting Between Numeric Types | |
| |
| |
| |
Formatting Numbers | |
| |
| |
| |
Taking Advantage of Shortcuts | |
| |
| |
| |
Using Non-Numeric Types | |
| |
| |
| |
The boolean Type | |
| |
| |
| |
The Character Type | |
| |
| |
| |
Using Strings | |
| |
| |
| |
Understanding Enumerations | |
| |
| |
| |
Example: Writing a Gas Pump Class | |
| |
| |
| |
Implementing Accessor Methods | |
| |
| |
| |
Implementing a Command/Query Pair | |
| |
| |
| |
Understanding Class Variables and Methods | |
| |
| |
| |
Using Class Variables | |
| |
| |
| |
Using Class Methods | |
| |
| |
| |
GUI: Using Java Interfaces | |
| |
| |
| |
Specifying Methods with Interfaces | |
| |
| |
| |
Implementing an Interface | |
| |
| |
| |
Developing Classes to a Specified Interface | |
| |
| |
| |
Informing the User Interface of Changes | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Test Harness Pattern | |
| |
| |
| |
The toString Pattern | |
| |
| |
| |
The Enumeration Pattern | |
| |
| |
| |
The Assign a Unique ID Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Collaborative Classes | |
| |
| |
| |
Example: Modeling a Person | |
| |
| |
| |
Using a Single Class | |
| |
| |
| |
Using Multiple Classes | |
| |
| |
| |
Diagramming Collaborating Classes | |
| |
| |
| |
Passing Arguments | |
| |
| |
| |
Temporary Variables | |
| |
| |
| |
Returning Object References | |
| |
| |
| |
Section Summary | |
| |
| |
| |
Reference Variables | |
| |
| |
| |
Memory | |
| |
| |
| |
Aliases | |
| |
| |
| |
Garbage Collection | |
| |
| |
| |
Testing for Equality | |
| |
| |
| |
Case Study: An Alarm Clock | |
| |
| |
| |
Step 1: Identifying Objects and Classes | |
| |
| |
| |
Step 2: Identifying Services | |
| |
| |
| |
Step 3: Solving the Problem | |
| |
| |
| |
Introducing Exceptions | |
| |
| |
| |
Throwing Exceptions | |
| |
| |
| |
Reading a Stack Trace | |
| |
| |
| |
Handling Exceptions | |
| |
| |
| |
Propogating Exceptions | |
| |
| |
| |
Enhancing the Alarm Clock with Sound (optional) | |
| |
| |
| |
Java's Collection Classes | |
| |
| |
| |
A List Class: ArrayList | |
| |
| |
| |
A Set Class: HashSet | |
| |
| |
| |
A Map Class: TreeMap | |
| |
| |
| |
Wrapper Classes | |
| |
| |
| |
GUIs and Collaborating Classes | |
| |
| |
| |
Using Libraries of Components | |
| |
| |
| |
Introducing the Model-View-Controller Pattern | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Has-a (Composition) Pattern | |
| |
| |
| |
The Equivalence Test Pattern | |
| |
| |
| |
The Throw an Exception Pattern | |
| |
| |
| |
The Catch an Exception Pattern | |
| |
| |
| |
The Process All Elements Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Input and Output | |
| |
| |
| |
Basic File Input and Output | |
| |
| |
| |
Reading from a File | |
| |
| |
| |
Writing to a File | |
| |
| |
| |
The Structure of Files | |
| |
| |
| |
Representing Records as Objects | |
| |
| |
| |
Reading Records as Objects | |
| |
| |
| |
File Formats | |
| |
| |
| |
Using the File Class | |
| |
| |
| |
Filenames | |
| |
| |
| |
Specifying File Locations | |
| |
| |
| |
Manipulating Files | |
| |
| |
| |
Interacting with Users | |
| |
| |
| |
Reading from the Console | |
| |
| |
| |
Checking Input for Errors | |
| |
| |
| |
Command Interpreters | |
| |
| |
| |
Using a Command Interpreter | |
| |
| |
| |
Implementing a Command Interpreter | |
| |
| |
| |
Separating the User Interface from the Model | |
| |
| |
| |
Constructing a Library | |
| |
| |
| |
Compiling without an IDE | |
| |
| |
| |
Creating and Using a Package | |
| |
| |
| |
Using .jar Files | |
| |
| |
| |
Streams (advanced) | |
| |
| |
| |
Character Input Streams | |
| |
| |
| |
Character Output Streams | |
| |
| |
| |
Byte Streams | |
| |
| |
| |
GUI: File Choosers and Images | |
| |
| |
| |
Using JFileChooser | |
| |
| |
| |
Displaying Images from a File | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Open File for Input Pattern | |
| |
| |
| |
The Open File for Output Pattern | |
| |
| |
| |
The Process File Pattern | |
| |
| |
| |
The Construct Record from File Pattern | |
| |
| |
| |
The Error-Checked Input Pattern | |
| |
| |
| |
The Command Interpreter Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Arrays | |
| |
| |
| |
Using Arrays | |
| |
| |
| |
Visualizing an Array | |
| |
| |
| |
Accessing One Array Element | |
| |
| |
| |
Swapping Array Elements | |
| |
| |
| |
Processing All the Elements in an Array | |
| |
| |
| |
Processing Matching Elements | |
| |
| |
| |
Searching for a Specified Element | |
| |
| |
| |
Finding an Extreme Element | |
| |
| |
| |
Sorting an Array | |
| |
| |
| |
Comparing Arrays and Files | |
| |
| |
| |
Creating an Array | |
| |
| |
| |
Declaration | |
| |
| |
| |
Allocation | |
| |
| |
| |
Initialization | |
| |
| |
| |
Passing and Returning Arrays | |
| |
| |
| |
Dynamic Arrays | |
| |
| |
| |
Partially Filled Arrays | |
| |
| |
| |
Resizing Arrays | |
| |
| |
| |
Combining Approaches | |
| |
| |
| |
Arrays of Primitive Types | |
| |
| |
| |
Using an Array of double | |
| |
| |
| |
Meaningful Indices | |
| |
| |
| |
Multi-Dimensional Arrays | |
| |
| |
| |
2D Array Algorithms | |
| |
| |
| |
Allocating and Initializing a 2D Array | |
| |
| |
| |
Arrays of Arrays | |
| |
| |
| |
GUI: Animation | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Process All Elements Pattern | |
| |
| |
| |
The Linear Search Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Building Quality Software | |
| |
| |
| |
Defining Quality Software | |
| |
| |
| |
Quality from a User's Perspective | |
| |
| |
| |
Quality from a Programmer's Perspective | |
| |
| |
| |
Using a Development Process | |
| |
| |
| |
Defining Requirements | |
| |
| |
| |
Designing the Architecture | |
| |
| |
| |
Iterative Development | |
| |
| |
| |
Designing Classes and Methods | |
| |
| |
| |
Rules of Thumb for Writing Quality Code | |
| |
| |
| |
Managing Complexity | |
| |
| |
| |
Programming Defensively | |
| |
| |
| |
Exceptions | |
| |
| |
| |
Design by Contract | |
| |
| |
| |
Assertions | |
| |
| |
| |
GUIs: Quality Interfaces | |
| |
| |
| |
Iterative User Interface Design | |
| |
| |
| |
User Interface Design Principles | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Polymorphism | |
| |
| |
| |
Introduction to Polymorphism | |
| |
| |
| |
Dancing Robots | |
| |
| |
| |
Polymorphism via Inheritance | |
| |
| |
| |
Examples of Polymorphism | |
| |
| |
| |
Polymorphism via Interfaces | |
| |
| |
| |
The Substitution Principle | |
| |
| |
| |
Choosing between Interfaces and Inheritance | |
| |
| |
| |
Case Study: Invoices | |
| |
| |
| |
Step 1: Identifying Objects and Classes | |
| |
| |
| |
Step 2: Identifying Services | |
| |
| |
| |
Step 3: Solving the Problem | |
| |
| |
| |
Polymorphism without Arrays | |
| |
| |
| |
Overriding Methods in object | |
| |
| |
| |
toString | |
| |
| |
| |
Equals | |
| |
| |
| |
Clone (advanced) | |
| |
| |
| |
Increasing Flexibility with Interfaces | |
| |
| |
| |
Using an Interface | |
| |
| |
| |
Using the Strategy Pattern | |
| |
| |
| |
Flexibility in Choosing Implementations | |
| |
| |
| |
GUI: Layout Managers | |
| |
| |
| |
The FlowLayout Strategy | |
| |
| |
| |
The GridLayout Strategy | |
| |
| |
| |
The BorderLayout Strategy | |
| |
| |
| |
Other Layout Strategies | |
| |
| |
| |
Nesting Layout Strategies | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Polymorphic Call Pattern | |
| |
| |
| |
The Strategy Pattern | |
| |
| |
| |
The Equals Pattern | |
| |
| |
| |
The Factory Method Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
| |
Graphical User Interfaces | |
| |
| |
| |
Overview | |
| |
| |
| |
Models, Views, and Controllers | |
| |
| |
| |
Using a Pattern | |
| |
| |
| |
Setting up the Model and View | |
| |
| |
| |
The Model's Infrastructure | |
| |
| |
| |
The View's Infrastructure | |
| |
| |
| |
The main Method | |
| |
| |
| |
Building and Testing the Model | |
| |
| |
| |
Building the View and Controllers | |
| |
| |
| |
Designing the Interface | |
| |
| |
| |
Laying Out the Components | |
| |
| |
| |
Updating the View | |
| |
| |
| |
Writing and Registering Controllers | |
| |
| |
| |
Refining the View | |
| |
| |
| |
View Pattern | |
| |
| |
| |
Using Multiple Views | |
| |
| |
| |
Implementing NimView | |
| |
| |
| |
Implementing NimPileView | |
| |
| |
| |
Implementing NimPlayerView | |
| |
| |
| |
Sequence Diagrams | |
| |
| |
| |
Controller Variations | |
| |
| |
| |
Using Inner Classes | |
| |
| |
| |
Using Event Objects | |
| |
| |
| |
Integrating the Controller and View | |
| |
| |
| |
Other Components | |
| |
| |
| |
Discover Available Components | |
| |
| |
| |
Identify Listeners | |
| |
| |
| |
Skim the Documentation | |
| |
| |
| |
Begin with Sample Code | |
| |
| |
| |
Work Incrementally | |
| |
| |
| |
Graphical Views | |
| |
| |
| |
Painting a Component | |
| |
| |
| |
Making a Graphical Component Interactive | |
| |
| |
| |
Patterns | |
| |
| |
| |
The Model-View-Controller Pattern | |
| |
| |
| |
Summary and Concept Map | |
| |
| |
| |
Problem Set | |
| |
| |
Epilogue | |
| |
| |
| |
Glossary | |
| |
| |
| |
Precedence Rules | |
| |
| |
| |
Variable Initialization Rules | |
| |
| |
| |
Unicode Character Set | |
| |
| |
| |
Selected Robot Documentation | |
| |
| |
Index | |