Java Reference
In-Depth Information
￿ Perimeter is not needed, because it can be computed from length and
width. Perimeter is not a data member.
￿ Area is not needed, because it can be computed from length and width.
Area is not a data member.
￿ Length is required. Length is a data member.
￿ Width is required. Width is a data member.
Having made these choices, the class Rectangle can be represented with data mem-
bers, as shown in Figure 6-12.
Rectangle
length
width
FIGURE 6-12 class Rectangle with data members
Step 4: Identify the operations for each of the classes.
Many operations for a class or an object can be determined by looking at the list of verbs.
Let us consider the verbs input, calculate, and print. The possible operations on a rectangle
object are input the length and width, calculate the perimeter and area, and print the
perimeter and area. In this step, we focus on the functionalities of the class(es) involved.
By carefully reading the problem statement, you may conclude that you need at least the
following operations:
￿ setLength : Set the length of the rectangle.
￿ setWidth : Set the width of the rectangle.
￿ computePerimeter : Calculate the perimeter of the rectangle.
￿ computeArea : Calculate the area of the rectangle.
￿ print : Print the perimeter and area of the rectangle.
It is customary to include operations to retrieve the values of the data members of an
object. Therefore, you also need the following operations:
￿ getLength : Retrieve the length of the rectangle.
￿ getWidth : Retrieve the width of the rectangle.
Figure 6-13 shows the class Rectangle with data members and operations.
 
Search WWH ::




Custom Search