The following is a problem that just does addition. I need to modify it to include buttons and calculation for multiplication,division and subtraction. Regarding the section that shows the addition portion of program, do i just take those same lines and,inserting the different operations, add those lines to the code after the addition or am i way off? just want to know in order to modify program to include all operations if i just simply need to add other operations after the addition or is there something i am not seeing. Thank you
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_CLOSE); 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); }