Java Reference
In-Depth Information
Table 6-2
Private Variables in the Calculator Application
DATA TYPE
VARIABLE NAME
PURPOSE
Button
keys[]
An array of buttons numbered 0 through 15 representing the numeric and
operator keys on the calculator.
Panel
keypad
A Panel to display the buttons.
TextField
lcd
A TextField located across the top of the calculator to display numbers as
they are clicked and the results of calculations.
double
op1
A double flag that will hold the result of the first operation.
boolean
first
A boolean flag initially set to true for the first operand and then set to false
to indicate subsequent operands.
boolean
foundKey
A boolean flag initially set to false. When the user clicks a button, the
program will search through the array of buttons looking for a match.
When a match is found, the value is set to true to terminate the search.
boolean
clearText
A boolean value initially set to true, but changed when the user begins to
click numeric buttons. When the user clicks an operator button, the value
is set back to true in order to clear the display.
int
lastOp
An integer to hold the index number of the previous operator.
DecimalFormat
calcPattern
A DecimalFormat variable to hold the pattern for the output display.
Figure 6-6 displays the code to begin the Calculator application. Lines 17
through 25 declare the private variables listed in Table 6-2.
1 /*
2
Chapter 6: Java Calculator
3
Programmer: J. Starks
4
Date:
November 6, 2007
5
Filename:
Calculator.java
6
Purpose:
This program creates a calculator with a menu.
7 */
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.awt.datatransfer.*;
12 import java.text. DecimalFormat ;
13 import javax.swing.JOptionPane;
14
15 public class Calculator extends Frame implements ActionListener
16 {
17
private Button keys [] ;
18
private Panel keypad;
19
private TextField lcd;
20
private double op1;
21
private boolean first;
22
private boolean foundKey;
23
private boolean clearText;
24
private int lastOp;
25
private DecimalFormat calcPattern;
26
FIGURE 6-6
 
Search WWH ::




Custom Search