Page 351 - Introduction to Programming with Java: A Problem Solving Approach
P. 351

                          Using stubs lets programmers test their partially implemented programs to determine
whether their behavior is correct down to the stub level. Second, it makes debugging eas-
ier. After compiling and running the program successfully with stubs, replace the stubs
with actual code one method at a time. As each stub is replaced, test and debug the updated program. If a bug appears, it should be easy to find since you know it’s probably in the most recently replaced method.
Square Program Example: Second-Cut Version
The next step in the top-down design process is to replace the helper methods’ stub implementations with actual implementations. We have two helper methods to work on—drawBorderSquare and drawSolidSquare.
Let’s start with the drawBorderSquare helper method. It prints a horizontal line of asterisks, prints the square’s sides, and then prints another horizontal line of asterisks. Here’s pseudocode for this algorithm:
drawBorderSquare method
draw horizontal line of asterisks draw sides
draw horizontal line of asterisks
All three of drawBorderSquare’s draw statements represent non-trivial tasks. Thus, when we trans- late the drawBorderSquare pseudocode into a Java method, we use method calls for each of the draw
private void drawBorderSquare()
{
}
drawHorizontalLine();
drawSides();
drawHorizontalLine();
// end drawBorderSquare
Now let’s consider the drawSolidSquare helper method. It prints a series of horizontal lines of aster- isks. Here’s pseudocode for its algorithm:
drawSolidSquare method
for (int i􏰀0; i􏰁square’s width; i􏰂􏰂)
draw horizontal line of asterisks
Once again, the draw statement represents a non-trivial task. Thus, when we translate the drawSolidSquare pseudocode into a Java method, we use a repeated method call for the draw subtask:
private void drawSolidSquare()
{
}
// end drawSolidSquare
for (int i=0; i<this.width; i++)
{
}
drawHorizontalLine();
8.6 Top-Down Design 317
 Test one thing at a time.
   subtasks:
Apago PDF Enhancer
































































   349   350   351   352   353