Page 348 - Introduction to Programming with Java: A Problem Solving Approach
P. 348
314 Chapter 8 Software Engineering
not be an instance variable. For our example, we’ve elected to think of solidness as just a temporary display option, so we do not include it as another instance variable.
Returning to the top-down design guidelines, we see that the next step is to decide on the public methods. The problem description often determines what needs to be public. Here’s what we need:
• a constructor that sets the square’s width
• getArea—compute the square’s area
• draw—display the square with asterisks using either an asterisks border or a solid pattern of asterisks
Let’s now step back and look at what we’ve done so far. See Figure 8.5. It presents a first-cut UML class diagram for our solution’s classes, instance variables, and constructor and public methods.
Figure 8.5 Square program’s UML class diagrams: first-cut version
The next step in the top-down design process is to implement the main method in the top-level class
Apago PDF Enhancer
SquareDriver
+main() : void
Square
-width : int
+Square(width : int)
+getArea() : int +draw() : void
This implementation appears in Figure 8.6. The code in main includes calls to the Square constructor and
/***********************************************************
*
SquareDriver.java
Dean & Dean
This is the driver for the Square class.
***********************************************************/
*
*
*
import java.util.Scanner;
public class SquareDriver
{
}
// end class SquareDriver
public static void main(String[] args)
{
}
Scanner stdIn = new Scanner(System.in);
Square square;
System.out.print("Enter width of desired square: ");
square = new Square(stdIn.nextInt());
System.out.println("Area = " + square.getArea());
square.draw();
// end main
Figure 8.6
SquareDriver class