Current problem only includes addition function. Here is what i did..is this correct for my problem where I need to modify it to include buttons and calculation for multiplication,division and subtraction.?
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class BasicCalculator { private static final int FRAME_WIDTH = 150; private static final int FRAME_HEIGHT = 120; // Keeps track of the current operation (subtract, add, etc) private static final int NO_OPERATION = 0; private static final int ADDITION = 1; public static int operation = NO_OPERATION; public static JTextField textFieldDisplay; public static double Value1 = 0; // holds the value before the operation public static void main(String[] args) { // Set up the user interface JFrame frame = new JFrame(); JPanel buttonPanel = new JPanel(); frame.add(buttonPanel); // create two buttons, plus and equal and a text box for answers textFieldDisplay = new JTextField(10); buttonPanel.add(textFieldDisplay); JButton buttonPlus = new JButton(" + "); buttonPanel.add(buttonPlus); JButton buttonEqual = new JButton(" = "); buttonPanel.add(buttonEqual); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Basic Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E); frame.setVisible(true); // called when the equal sign '=' is pressed class EqualSignListener implements ActionListener { public void actionPerformed(ActionEvent event) { double Value2 = Double.parseDouble(textFieldDisplay.getText()); if (operation == ADDITION) { // plus sign pressed before the equal sign Value2 += Value1; } // Convert from a answer to a string Double answer = new Double(Value2); textFieldDisplay.setText( answer.toString() ); // Reset the operation to show no current operation operation = NO_OPERATION; } } // called when a plus sign '+' is pressed class PlusSignListener implements ActionListener { public void actionPerformed(ActionEvent event) { Value1 = Double.parseDouble(textFieldDisplay.getText()); operation = ADDITION; } } // Add the methods that will be called when these buttons are pressed ActionListener plusSignListener = new PlusSignListener(); buttonPlus.addActionListener(plusSignListener); ActionListener equalSignListener = new EqualSignListener(); buttonEqual.addActionListener(equalSignListener); } } // called when a subtraction sign '-' is pressed class SubtractionSignListener implements ActionListener { public void actionPerformed(ActionEvent event) { Value1 = Double.parseDouble(textFieldDisplay.getText()); operation = Subtraction; } } // Add the methods that will be called when these buttons are pressed ActionListener subtractionSignListener = new SubtractionSignListener(); buttonPlus.addActionListener(subtractionSignListen er); ActionListener equalSignListener = new EqualSignListener(); buttonEqual.addActionListener(equalSignListener); } ) // called when a division sign '/' is pressed class DivisionSignListener implements ActionListener { public void actionPerformed(ActionEvent event) { Value1 = Double.parseDouble(textFieldDisplay.getText()); operation = DIVISION; } } // Add the methods that will be called when these buttons are pressed ActionListener divisionSignListener = new DivisionSignListener(); buttonPlus.addActionListener(plusSignListener); ActionListener equalSignListener = new EqualSignListener(); buttonEqual.addActionListener(equalSignListener); } ) // called when a plus sign '*' is pressed class MulitplicationSignListener implements ActionListener { public void actionPerformed(ActionEvent event) { Value1 = Double.parseDouble(textFieldDisplay.getText()); operation = Multiplication; } } // Add the methods that will be called when these buttons are pressed ActionListener multiplicationSignListener = new MultiplicationSignListener(); buttonPlus.addActionListener(plusSignListener); ActionListener equalSignListener = new EqualSignListener(); buttonEqual.addActionListener(equalSignListener); } }