Programming and Problem Solving with Java

(やまだぃちぅ) #1
427

8.Most of the code in the Calculatorapplication involves input/output. The code
is long, but the concepts are simple. Examine the code below and mark the
statements associated with creating the frame and content pane as F; the
statements associated with handling buttons as B; and the statements that
create the buttons, register the listeners with them, and place the buttons into
the frame as FB. Notice that all the comments have been removed; you have to
read and understand the actual code to answer this question and Exercise 9.


importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;

public classCalculator
{
staticJTextField inputField;
staticJLabel register;
static doubleresult;
staticJFrame calcFrame;
staticContainer calcPane;
staticNumericHandler operation;
staticClearHandler clearOperation;
staticJLabel resultLabel;
staticJLabel entryLabel;
staticJButton add;
staticJButton subtract;
staticJButton clear;

static classNumericHandler implementsActionListener
{
public voidactionPerformed(ActionEvent event)
{
doublesecondOperand;
String whichButton;
secondOperand =
Double.parseDouble(inputField.getText());
whichButton = event.getActionCommand();

if(whichButton.equals("+"))
result = result + secondOperand;
else
result = result – secondOperand;
Free download pdf