Java Reference
In-Depth Information
widthStr =
JOptionPane.showInputDialog("Enter the width: "); //Line 5
width = Double.parseDouble(widthStr);
//Line 6
area = length * width;
//Line 7
perimeter = 2 * (length + width);
//Line 8
outputStr = "Length: " + length + "\n" +
"Width: " + width + "\n" +
"Area: " + area + " square units\n" +
"Perimeter: " + perimeter + " units\n";
//Line 9
JOptionPane.showMessageDialog( null , outputStr,
"Rectangle",
JOptionPane.INFORMATION_MESSAGE);
//Line 10
System.exit(0);
//Line 11
}
6
}
Sample Run: (Figure 6-1 shows the sample run.)
FIGURE 6-1 Sample run for Rectangle
The program in Example 6-1 works as follows: The statements in Lines 1 and 2 declare
various variables to manipulate the data. The statement in Line 3 displays the first dialog
box of the sample run and prompts the user to enter the length of the rectangle. The
entered length is assigned as a string to lengthStr . The statement in Line 4 retrieves the
length and stores it in the variable length .
Search WWH ::




Custom Search