Page 350 - Introduction to Programming with Java: A Problem Solving Approach
P. 350
316 Chapter 8 Software Engineering
variable. Is it OK to create this “false” impression? Yes, it is, because the instance variable is private and therefore hidden from public view. In fact, as noted above, we might actually have used area as the lone instance variable! A user of a class does not have to know exactly how it’s implemented. Don’t worry about the implementation when you pick a method name. It’s the effect that matters, and getArea accurately describes the effect of calling that method.
The draw method prompts the user to choose a border format or a solid format for the square’s dis- play. It’s now becoming apparent that the draw method is not trivial. The drawBorderSquare and drawSolidSquare method calls are examples of subtasks that we should split off into separate helper methods.
Stubs
Top-down design tells us to implement helper methods initially as stubs. For our Square program, that means implementing drawBorderSquare and drawSolidSquare as stubs. Note the stubs in Figure 8.7b.
}
// end class Square
//**********************************************************
private void drawBorderSquare()
{
// a STUB
Apago PDF Enhancer
}
System.out.println("In drawBorderSquare");
//**********************************************************
private void drawSolidSquare()
// a STUB
{
}
System.out.println("In drawSolidSquare");
Figure 8.7b
Square class: first-cut version—part B
As you can probably surmise from the examples, a stub doesn’t do much. Its main purpose is to satisfy the compiler so that the program is able to compile and run. Its secondary purpose is to provide an output that confirms that the method was called, and (where appropriate) show values passed into that method. When the stubbed Square program runs, it produces either this sample session:
Enter width of desired square: 5
Area = 25.0
Print with (b)order or (s)olid? b
In drawBorderSquare
or this sample session:
Enter width of desired square: 5
Area = 25.0
Print with (b)order or (s)olid? s
In drawSolidSquare