Java Reference
In-Depth Information
Sample Run: (Figure 3-12 shows a sample run of this program. The input screen is
shown first, then the output screen.)
3
(a)
(b)
FIGURE 3-12
Sample run of program to calculate a circle's area and perimeter
The preceding program works as follows. The statements in Lines 1 through 5 declare
the appropriate variables to manipulate the data. The statement in Line 6 displays the
input dialog box with the message
Enter the radius:
(in Figure 3-12(a), the entered value
is 12.50).
The string containing the input data is assigned to the
String
variable
radiusString
.
The statement in Line 7 converts the string containing the radius into a value of the type
double
and stores it in the variable
radius
.
The statements in Lines 8 and 9 calculate the area and circumference of the circle and
store them in the variables
area
and
circumference
, respectively. The statement in
Line 10 constructs the string containing the radius, area, and circumference of the circle.
The string is assigned to the variable
outputStr
. The statement in Line 11 uses the
message dialog box to display the circle's radius, area, and circumference, as shown in
Figure 3-12(b).
The statement in Line 12 terminates the program after the user clicks the
OK
button in the
dialog box.
The program in Example 3-12 does not output the area and circumference to two decimal
places. The next section explains how to format the output in an output dialog box.
If the amount of input data is small and the output is small, dialog boxes are an effective
and attractive way to build an application.
Search WWH ::
Custom Search