Java Reference
In-Depth Information
EXAMPLE 6-2
Consider the problem presented in Example 6-1. In simple terms, the problem can be
stated as follows:
''Write a program to input the length and width of a rectangle and calculate and print
the perimeter and area of the rectangle.''
Step 1: Identify all the (relevant) nouns.
￿ Length
￿ Width
￿ Perimeter
￿ Area
￿ Rectangle
Step 2: Identify the class(es).
Considering all five nouns, it is clear that:
￿ Length is the length of a rectangle.
￿ Width is the width of a rectangle.
￿ Perimeter is the perimeter of a rectangle.
￿ Area is the area of a rectangle.
Notice that four of the five nouns are related to the fifth one, namely, rectangle .
Therefore, choose Rectangle as a class. From the class Rectangle , you can instantiate
rectangles of various dimensions. The class Rectangle can be graphically represented
as in Figure 6-11.
6
Rectangle
FIGURE 6-11 class Rectangle
Step 3: Identify the data members for each of the classes.
In this step, you evaluate the remaining nouns and determine the information that is
essential to fully describing each class. Therefore, consider each noun—length, width,
perimeter, and area—and ask:
''Is each of these nouns essential
for describing the
rectangle?''
Search WWH ::




Custom Search