Page 352 - Introduction to Programming with Java: A Problem Solving Approach
P. 352
318 Chapter 8 Software Engineering
Notice that the drawBorderSquare method and the drawSolidSquare method both call the same drawHorizontalLine helper method. Being able to share the drawHorizontalLine method is a nice reward for our diligent use of helper methods, and it provides a good example for this general principle:
If two or more methods perform the same subtask, avoid redundant code by having those methods call a shared helper method that performs the subtask.
By writing final code for the drawBorderSquare and drawSolidSquare methods and writing stub code for the drawHorizontalLine and drawSides methods, we complete the coding for the Square program’s second-cut version. When executed with appropriate print statements in the two stub methods, drawHorizontalLine and drawSides, the second-cut version produces either this sample session:
Enter width of desired square: 5
Area = 25.0
Print with (b)order or (s)olid? b
In drawHorizontalLine
In drawSides
In drawHorizontalLine
or this sample session:
Enter width of desired square: 5
Area = 25.0
Print with (b)order or (s)olid? s
In drawHorizontalLineApago PDF Enhancer In drawHorizontalLine
In drawHorizontalLine
In drawHorizontalLine
In drawHorizontalLine
Square Program Example: Final Version
Keep documentation current.
To facilitate management, it’s a good idea to formalize your program’s design at various points during the design process. The formalization usually takes the form of UML class diagrams. Having up-to-date UML class diagrams helps to ensure project coherence. At a minimum, current UML class diagrams ensure that all members of a project are using
the same classes, instance variables, and method headings. See Figure 8.8.
SquareDriver
+main() : void
Square
-width : int
+Square(width : int) : void
+getArea() : int
+draw() : void -drawBorderSquare() : void -drawSolidSquare() : void -drawHorizontalLine() : void -drawSides() : void
Figure 8.8 Square program’s UML class diagram: final version