
CHAPTER 1 THE PHASES OF SOFTWARE DEVELOPMENT 1 1.1 Specification, Design, Implementation 4 Design Technique: Decomposing the Problem 5 How to Write a Specification for a Java Method 6 Pitfall: Throw an Exception to Indicate a Failed Precondition 9 Temperature Conversion: Implementation 10 Programming Tip: Use Javadoc to Write Specifications 13 Programming Tip: Use Final Variables to Improve Clarity 13 Programming Tip: Make Exception Messages Informative 14 Programming Tip: Format Output with System.out.printf 14 Self-Test Exercises for Section 1.1 15 1.2 Running Time Analysis 16 The Stair-Counting Problem 16 Big-O Notation 21 Time Analysis of Java Methods 23 Worst-Case, Average-Case, and Best-Case Analyses 25 Self-Test Exercises for Section 1.2 26 1.3 Testing and Debugging 26 Choosing Test Data 27 Boundary Values 27 Fully Exercising Code 28 Pitfall: Avoid Impulsive Changes 29 Using a Debugger 29 Assert Statements 29 Turning Assert Statements On and Off 30 Programming Tip: Use a Separate Method for Complex Assertions 32 Pitfall: Avoid Using Assertions to Check Preconditions 34 Static Checking Tools 34 Self-Test Exercises for Section 1.3 34 Chapter Summary 35 Solutions to Self-Test Exercises 36 CHAPTER 2 JAVA CLASSES AND INFORMATION HIDING 38 2.1 Classes and Their Members 40 Defining a New Class 41 Instance Variables 41 Constructors 42 No-Arguments Constructors 43 Methods 43 Accessor Methods 44 Programming Tip: Four Reasons to Implement Accessor Methods 44 Pitfall: Division Throws Away the Fractional Part 45 Programming Tip: Use the Boolean Type for True or False Values 46 Modification Methods 46 Pitfall: Potential Arithmetic Overflows 48 Complete Definition of Throttle.java 48 Methods May Activate Other Methods 51 Self-Test Exercises for Section 2.1 51 2.2 Using a Class 52 Creating and Using Objects 52 A Program with Several Throttle Objects 53 Null References 54 NullPointerException 55 Assignment Statements with Reference Variables 55 Clones 58 Testing for Equality 58 Terminology Controversy: “The Throttle That t Refers To” 59 Self-Test Exercises for Section 2.2 59 2.3 Packages 60 Declaring a Package 60 The Import Statement to Use a Package 63 The JCL Packages 63 More about Public, Private, and Package Access 63 Self-Test Exercises for Section 2.3 65 2.4 Parameters, Equals Methods, and Clones 65 The Location Class 66 Static Methods 72 Parameters That Are Objects 73 Methods May Access Private Instance Variables of Objects in Their Own Class 74 The Return Value of a Method May Be an Object 75 Programming Tip: How to Choose the Names of Methods 76 Java’s Object Type 77 Using and Implementing an Equals Method 77 Pitfall: ClassCastException 80 Every Class Has an Equals Method 80 Using and Implementing a Clone Method 81 Pitfall: Older Java Code Requires a Typecast for Clones 81 Programming Tip: Always Use super.clone for Your Clone Method 85 Programming Tip: When to Throw a Runtime Exception 85 A Demonstration Program for the Location Class 85 What Happens When a Parameter Is Changed Within a Method? 86 Self-Test Exercises for Section 2.4 89 2.5 The Java Class Libraries 90 Chapter Summary 92 Solutions to Self-Test Exercises 93 Programming Projects 95 CHAPTER 3 COLLECTION CLASSES 103 3.1 A Review of Java Arrays 104 Pitfall: Exceptions That Arise from Arrays 106 The Length of an Array 106 Assignment Statements with Arrays 106 Clones of Arrays 107 The Arrays Utility Class 108 Array Parameters 110 Programming Tip: Enhanced For-Loops for Arrays 111 Self-Test Exercises for Section 3.1 112 3.2 An ADT for a Bag of Integers 113 The Bag ADT—Specification 114 OutOfMemoryError and Other Limitations for Collection Classes 118 The IntArrayBag Class—Specification 118 The IntArrayBag Class—Demonstration Program 122 The IntArrayBag Class—Design 125 The Invariant of an ADT 126 The IntArrayBag ADT—Implementation 127 Programming Tip: Cloning a Class That Contains an Array 136 The Bag ADT—Putting the Pieces Together 137 Programming Tip: Document the ADT Invariant in the Implementation File 141 The Bag ADT—Testing 141 Pitfall: An Object Can Be an Argument to Its Own Method 142 The Bag ADT—Analysis 142 Self-Test Exercises for Section 3.2 144 3.3 Programming Project: The Sequence ADT 145 The Sequence ADT—Specification 146 The Sequence ADT—Documentation 150 The Sequence ADT—Design 150 The Sequence ADT—Pseudocode for the Implementation 156 Self-Test Exercises for Section 3.3 158 3.4 Programming Project: The Polynomial 159 Self-Test Exercises for Section 3.4 162 3.5 The Java HashSet and Iterators 162 The HashSet Class 162 Some of the HashSet Members 162 Iterators 163 Pitfall: Do Not Access an Iterator’s next Item When hasNext Is False 164 Pitfall: Changing a Container Object Can Invalidate Its Iterator 164 Invalid Iterators 164 Self-Test Exercises for Section 3.5 165 Chapter Summary 165 Solutions to Self-Test Exercises 166 Programming Projects 169 CHAPTER 4 LINKED LISTS 175 4.1 Fundamentals of Linked Lists 176 Declaring a Class for Nodes 177 Head Nodes, Tail Nodes 177 The Null Reference 178 Pitfall: NullPointerExceptions with Linked Lists 179 Self-Test Exercises for Section 4.1 179 4.2 Methods for Manipulating Nodes 179 Constructor for the Node Class 180 Getting and Setting the Data and Link of a Node 180 Public Versus Private Instance Variables 181 Adding a New Node at the Head of a Linked List 182 Removing a Node from the Head of a Linked List 183 Adding a New Node That Is Not at the Head 185 Removing a Node That Is Not at the Head 188 Pitfall: NullPointerExceptions with removeNodeAfter 191 Self-Test Exercises for Section 4.2 191 4.3 Manipulating an Entire Linked List 192 Computing the Length of a Linked List 193 Programming Tip: How to Traverse a Linked List 196 Pitfall: Forgetting to Test the Empty List 197 Searching for an Element in a Linked List 197 Finding a Node by Its Position in a Linked List 198 Copying a Linked List 200 A Second Copy Method, Returning Both Head and Tail References 204 Programming Tip: A Method Can Return an Array 205 Copying Part of a Linked List 206 Using Linked Lists 207 Self-Test Exercises for Section 4.3 214 4.4 The Bag ADT with a Linked List 215 Our Second Bag–Specification 215 The grab Method 219 Our Second Bag–Class Declaration 219 The Second Bag–Implementation 220 Programming Tip: Cloning a Class That Contains a Linked List 223 Programming Tip: How to Choose between Different Approaches 225 The Second Bag–Putting the Pieces Together 229 Self-Test Exercises for Section 4.4 232 4.5 Programming Project: The Sequence ADT with a Linked List 232 The Revised Sequence ADT–Design Suggestions 232 The Revised Sequence ADT–Clone Method 235 Self-Test Exercises for Section 4.5 238 4.6 Beyond Simple Linked Lists 239 Arrays Versus Linked Lists and Doubly Linked Lists 239 Dummy Nodes 240 Java’s List Classes 241 ListIterators 242 Making the Decision 243 Self-Test Exercises for Section 4.6 244 Chapter Summary 244 Solutions to Self-Test Exercises 245 Programming Projects 248 CHAPTER 5 GENERIC PROGRAMMING 251 5.1 Java’s Object Type and Wrapper Classes 252 Widening Conversions 253 Narrowing Conversions 254 Wrapper Classes 256 Autoboxing and Auto-Unboxing Conversions 256 Advantages and Disadvantages of Wrapper Objects 257 Self-Test Exercises for Section 5.1 257 5.2 Object Methods and Generic Methods 258 Object Methods 259 Generic Methods 259 Pitfall: Generic Method Restrictions 260 Self-Test Exercises for Section 5.2 261 5.3 Generic Classes 262 Writing a Generic Class 262 Using a Generic Class 262 Pitfall: Generic Class Restrictions 263 Details for Implementing a Generic Class 263 Creating an Array to Hold Elements of the Unknown Type 263 Retrieving E Objects from the Array 264 Warnings in Generic Code 264 Programming Tip: Suppressing Unchecked Warnings 265 Using ArrayBag as the Type of a Parameter or Return Value 266 Counting the Occurrences of an Object 266 The Collection Is Really a Collection of References to Objects 267 Set Unused References to Null 269 Steps for Converting a Collection Class to a Generic Class 269 Deep Clones for Collection Classes 271 Using the Bag of Objects 279 Details of the Story-Writing Program 282 Self-Test Exercises for Section 5.3 282 5.4 Generic Nodes 283 Nodes That Contain Object Data 283 Pitfall: Misuse of the equals Method 283 Other Collections That Use Linked Lists 285 Self-Test Exercises for Section 5.4 285 5.5 Interfaces and Iterators 286 Interfaces 286 How to Write a Class That Implements an Interface 287 Generic Interfaces and the Iterable Interface 287 How to Write a Generic Class That Implements a Generic Interface 288 The Lister Class 289 Pitfall: Don’t Change a List While an Iterator Is Being Used 291 The Comparable Generic Interface 292 Parameters That Use Interfaces 293 Using instanceof to Test Whether a Class Implements an Interface 294 The Cloneable Interface 295 Self-Test Exercises for Section 5.5 295 5.6 A Generic Bag Class That Implements the Iterable Interface (Optional Section) 296 Programming Tip: Enhanced For-Loops for the Iterable Interface 297 Implementing a Bag of Objects Using a Linked List and an Iterator 298 Programming Tip: External Iterators Versus Internal Iterators 298 Summary of the Four Bag Implementations 299 Self-Test Exercises for Section 5.6 299 5.7 The Java Collection Interface and Map Interface (Optional Section) 300 The Collection Interface 30... |
商品评论(0条)