Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. Handle any possible exceptions like divided by zero.

 import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


public class SimpleCalculator extends JFrame implements ActionListener {

    private JTextField resultField;

    private String operator = "";

    private double num1 = 0;


    public SimpleCalculator() {

        // Set up the frame

        setTitle("Simple Calculator");

        setSize(400, 600);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLayout(new BorderLayout());


        // Create the text field

        resultField = new JTextField();

        resultField.setEditable(false);

        add(resultField, BorderLayout.NORTH);


        // Create a panel for the buttons

        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(4, 4, 10, 10));

        

        // Adding buttons for digits 0-9 and operations

        String[] buttonLabels = {

            "7", "8", "9", "/",

            "4", "5", "6", "*",

            "1", "2", "3", "-",

            "0", "C", "=", "+"

        };

        

        for (String label : buttonLabels) {

            JButton button = new JButton(label);

            button.addActionListener(this);

            panel.add(button);

        }


        add(panel, BorderLayout.CENTER);

    }


    @Override

    public void actionPerformed(ActionEvent e) {

        String command = e.getActionCommand();


        // Handle clear command

        if (command.equals("C")) {

            resultField.setText("");

            operator = "";

            num1 = 0;

            return;

        }


        // Handle equals command

        if (command.equals("=")) {

            double num2 = Double.parseDouble(resultField.getText());

            double result;


            switch (operator) {

                case "+":

                    result = num1 + num2;

                    break;

                case "-":

                    result = num1 - num2;

                    break;

                case "*":

                    result = num1 * num2;

                    break;

                case "/":

                    if (num2 == 0) {

                        resultField.setText("Error: Div by 0");

                        return;

                    }

                    result = num1 / num2;

                    break;

                default:

                    return;

            }

            resultField.setText(String.valueOf(result));

            operator = "";

            return;

        }


        // If an operator is selected

        if ("+-*/".contains(command)) {

            num1 = Double.parseDouble(resultField.getText());

            operator = command;

            resultField.setText("");

            return;

        }


        // Handle number buttons

        resultField.setText(resultField.getText() + command);

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {

            SimpleCalculator calculator = new SimpleCalculator();

            calculator.setVisible(true);

        });

    }

}

Comments