Java Reference
In-Depth Information
Entering Beginning Code
Several statements and commands are used as the beginning code in most
executable Java programs, including comments, import statements, the class
header, and the main method header. In the Commission program, one import
statement imports the JOptionPane class from the javax.swing package so that
the program can use some of its methods to create and display dialog boxes that
display a message or prompt users for an input value. A second import statement
imports the DecimalFormat class from the java.text package in order to format
the output displayed to the user. The DecimalFormat class will be covered in
detail later in the chapter.
The Commission program requires three variables: one to hold the dollar
sales amount, one to hold the calculated commission, and one to hold the
commission code. Table 4-2 summarizes the data types, variable names, and
purposes of the three variables.
Table 4-2
Variables for the Commission Program
DATA TYPE
VARIABLE NAME
PURPOSE
double
dollars
To hold the valid dollar amount of sales
double
answer
To hold the answer returned after commission is
calculated
int
empCode
To hold the commission code for the employee's
sales
Figure 4-6 displays the comments, import statements, headers, braces,
and variable declarations used in the Commission program. Even though
this program has no code inside the main() method — other than to declare
variables — Java can compile and execute the program successfully.
1 /*
2
Chapter 4: Sales Commission
3
Programmer: J. Starks
4
Date:
October 25, 2007
5
Filename:
Commission.java
6
Purpose:
This program calculates sales commission using five methods:
7
getSales(), getCode(), getComm(), output(), and finish().
8 */
9
10 import javax.swing.JOptionPane;
11 import java.text. DecimalFormat ;
12
13 public class Commission
14 {
15
public static void main ( String [] args )
16
{
17
//declare class variables
18
double dollars, answer;
19
int empCode;
20
}
21 }
FIGURE 4-6
 
Search WWH ::




Custom Search