Java Reference
In-Depth Information
25
output ( answer, dollars ) ;
105
106
//The output() method displays the commission and sales.
107
public static void output ( double commission, double sales )
108
{
109
DecimalFormat twoDigits = new DecimalFormat ( "$#,000.00" ) ;
110
111
JOptionPane.showMessageDialog ( null , "Your commission on sales of " + twoDigits.format
( sales ) + " is " + twoDigits.format ( commission ) , "Commission Totals" ,JOptionPane.
INFORMATION_MESSAGE ) ;
112
}
113
FIGURE 4-30
When the output() method is called from the main() method in line 25, it
sends the arguments, answer and dollars, to the output() method itself. It does
not receive a return value.
In line 109 in the output() method, an instance of the DecimalFormat
object, named twoDigits, is constructed with a pattern allowing for thousands of
dollars, formatted with a dollar sign, a comma thousands separator, and two
decimal places.
In line 111, the second argument of the showMessageDialog() method repre-
sents the message that will display in the dialog box. The argument is composed
of a string of characters concatenated, or joined, with the values for the sales
amount and the commission amount. As you learned in Chapter 2, the plus
sign (+) is a concatenation symbol used to join String values. In line 111, the
twoDigits.format() methods take the numeric arguments, sales and commission,
and apply the format pattern defined and assigned in line 109.
The following steps enter code for the output() method.
To Code the output() Method
1. Enter line 25 as shown in Figure 4-30.
TextPad displays the statement to call the output() method and send two
arguments (Figure 4-31).
method call
with arguments
FIGURE 4-31
(continued)
Search WWH ::




Custom Search