```
package com.company;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener { // main method with JFrame extension
    JTextField field; // Creates new text field
    double inp1; // initializing variables
    double inp2;
    double result;
    double answer;
    int x = 0;
    int y = 0;
    int z = 0;
    char ch; // initialized variables
    JButton num0, num1, num2, num3, // naming all buttons on the Calculator
            num4, num5, num6, num7,
            num8, num9, clear, square,
            cube, add, sub, mul, div,
            log, reciprocal, equal,
            dot, root, sin, cos, tan;
    Container container; // provides space for all future components
    JPanel panel; // organizes the components in the space in the container

    public Main() {
        container = getContentPane(); // gets content pane in order to add an object to it
        container.setLayout(new BorderLayout()); // allows to set layout of container
        JPanel textpanel = new JPanel();
        field = new JTextField(20);
        field.setHorizontalAlignment(SwingConstants.LEFT); // This sets the alignment of the numbers when input
        field.addKeyListener(new KeyAdapter() { // added so the numbers can also be input from keyboard
            public void keyTyped(KeyEvent keyevent) {
                char c = keyevent.getKeyChar();
                if (c >= '0' && c <= '9') {
                } else {
                    keyevent.consume();
                }
            }
        });
        textpanel.add(field); // adds field where the results will be displayed
        panel = new JPanel();
        panel.setLayout(new GridLayout(9, 3, 5, 5)); // hgap and vgap set the gap between buttons

        num1 = new JButton("1"); // title of what the button will be called on the panel
        panel.add(num1); // adding buttons on the panel
        num1.addActionListener(this); // "this" is used as a reference to the current object
        num2 = new JButton("2");
        panel.add(num2);
        num2.addActionListener(this);
        num3 = new JButton("3");
        panel.add(num3);
        num3.addActionListener(this);
        num4 = new JButton("4");
        panel.add(num4);
        num4.addActionListener(this);
        num5 = new JButton("5");
        panel.add(num5);
        num5.addActionListener(this);
        num6 = new JButton("6");
        panel.add(num6);
        num6.addActionListener(this);
        num7 = new JButton("7");
        panel.add(num7);
        num7.addActionListener(this);
        num8 = new JButton("8");
        panel.add(num8);
        num8.addActionListener(this);
        num9 = new JButton("9");
        panel.add(num9);
        num9.addActionListener(this);

        add = new JButton("+");
        panel.add(add);
        add.addActionListener(this);

        num0 = new JButton("0");
        panel.add(num0);
        num0.addActionListener(this);

        mul = new JButton("*");
        panel.add(mul);
        mul.addActionListener(this);

        sub = new JButton("-");
        panel.add(sub);
        sub.addActionListener(this);

        dot = new JButton(".");
        panel.add(dot);
        dot.addActionListener(this);

        div = new JButton("/");
        div.addActionListener(this);
        panel.add(div);

        equal = new JButton("=");
        panel.add(equal);
        equal.addActionListener(this);

        log = new JButton("log");
        panel.add(log);
        log.addActionListener(this);

        root = new JButton("√");
        panel.add(root);
        root.addActionListener(this);

        sin = new JButton("SIN");
        panel.add(sin);

        sin.addActionListener(this);
        cos = new JButton("COS");
        panel.add(cos);

        cos.addActionListener(this);
        tan = new JButton("TAN");
        panel.add(tan);

        reciprocal = new JButton("1/x");
        panel.add(reciprocal);
        reciprocal.addActionListener(this);

        tan.addActionListener(this);
        square = new JButton("x^2");
        panel.add(square);
        square.addActionListener(this);
        cube = new JButton("x^3");
        panel.add(cube);
        cube.addActionListener(this);

        clear = new JButton("CLEAR");
        panel.add(clear);
        clear.addActionListener(this); // panel buttons till here
        container.add(panel);
        container.add("North", textpanel); // sets position of text panel to the top of the window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // will close the program when clicked on x
    }

    public static void main(String args[]) {
        try { // tests the code for errors while running
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); // sets the way the calculator window looks
        }
        catch(Exception e) { // executes the code in catch if error in try
        }
        Main window = new Main(); // Calculator object created
        window.pack(); // this will size the window so that all contents are at their preferred size(or bigger)
        window.setTitle("SCIENTIFIC CALCULATOR"); // sets the title of the window (seen on top of window)
        window.setVisible(true); // window is made visible
    }
    public void actionPerformed(ActionEvent e) { // button clicks class
        String inp = e.getActionCommand();
        if (inp.equals("1")) {
            if (z == 0) {
                field.setText(field.getText() + "1"); // setText receives a string while getText returns it
            } else {
                field.setText("");
                field.setText(field.getText() + "1");
                z = 0;
            }
        }
        if (inp.equals("2")) {
            if (z == 0) {
                field.setText(field.getText() + "2");
            } else {
                field.setText("");
                field.setText(field.getText() + "2");
                z = 0;
            }
        }
        if (inp.equals("3")) {
            if (z == 0) {
                field.setText(field.getText() + "3");
            } else {
                field.setText("");
                field.setText(field.getText() + "3");
                z = 0;
            }
        }
        if (inp.equals("4")) {
            if (z == 0) {
                field.setText(field.getText() + "4");
            } else {
                field.setText("");
                field.setText(field.getText() + "4");
                z = 0;
            }
        }
        if (inp.equals("5")) {
            if (z == 0) {
                field.setText(field.getText() + "5");
            } else {
                field.setText("");
                field.setText(field.getText() + "5");
                z = 0;
            }
        }
        if (inp.equals("6")) {
            if (z == 0) {
                field.setText(field.getText() + "6");
            } else {
                field.setText("");
                field.setText(field.getText() + "6");
                z = 0;
            }
        }
        if (inp.equals("7")) {
            if (z == 0) {
                field.setText(field.getText() + "7");
            } else {
                field.setText("");
                field.setText(field.getText() + "7");
                z = 0;
            }
        }
        if (inp.equals("8")) {
            if (z == 0) {
                field.setText(field.getText() + "8");
            } else {
                field.setText("");
                field.setText(field.getText() + "8");
                z = 0;
            }
        }
        if (inp.equals("9")) {
            if (z == 0) {
                field.setText(field.getText() + "9");
            } else {
                field.setText("");
                field.setText(field.getText() + "9");
                z = 0;
            }
        }
        if (inp.equals("0")) {
            if (z == 0) {
                field.setText(field.getText() + "0");
            } else {
                field.setText("");
                field.setText(field.getText() + "0");
                z = 0;
            }
        }
        if (inp.equals("CLEAR")) {
            field.setText("");
            x = 0;
            y = 0;
            z = 0;
        }
        if (inp.equals("log")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.log(Double.parseDouble(field.getText()));
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("1/x")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = 1 / Double.parseDouble(field.getText());
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("x^2")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.pow(Double.parseDouble(field.getText()), 2);
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("x^3")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.pow(Double.parseDouble(field.getText()), 3);
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals(".")) {
            if (y == 0) {
                field.setText(field.getText() + ".");
                y = 1;
            } else {
                field.setText(field.getText());
            }
        }
        if (inp.equals("+")) {
            if (field.getText().equals("")) {
                field.setText("");
                inp1 = 0;
                ch = '+';
            } else {
                inp1 = Double.parseDouble(field.getText());
                field.setText("");
                ch = '+';
                y = 0;
                x = 0;
            }
        }
        if (inp.equals("-")) {
            if (field.getText().equals("")) {
                field.setText("");
                inp1 = 0;
                ch = '-';
            } else {
                x = 0;
                y = 0;
                inp1 = Double.parseDouble(field.getText());
                field.setText("");
                ch = '-';
            }
        }
        if (inp.equals("/")) {
            if (field.getText().equals("")) {
                field.setText("");
                inp1 = 1;
                ch = '/';
            } else {
                x = 0;
                y = 0;
                inp1 = Double.parseDouble(field.getText());
                ch = '/';
                field.setText("");
            }
        }
        if (inp.equals("*")) {
            if (field.getText().equals("")) {
                field.setText("");
                inp1 = 1;
                ch = '*';
            } else {
                x = 0;
                y = 0;
                inp1 = Double.parseDouble(field.getText());
                ch = '*';
                field.setText("");
            }
        }

        if (inp.equals("√")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.sqrt(Double.parseDouble(field.getText()));
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("SIN")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.sin(Double.parseDouble(field.getText()));
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("COS")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.cos(Double.parseDouble(field.getText()));
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("TAN")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                answer = Math.tan(Double.parseDouble(field.getText()));
                field.setText("");
                field.setText(field.getText() + answer);
            }
        }
        if (inp.equals("=")) {
            if (field.getText().equals("")) {
                field.setText("");
            } else {
                inp2 = Double.parseDouble(field.getText());
                if(ch == '+') { // if statement for all basic operations
                    result = inp1 + inp2;
                } else if(ch == '-'){
                    result = inp1 - inp2;
                } else if(ch == '/'){
                    result = inp1 / inp2;
                } else{
                    result = inp1 * inp2;
                }

                field.setText("");
                field.setText(field.getText() + result);
                z = 1;
            }
        }
    }

}

```

The code for this calculator works fine but I want to know what the purpose of the z variable is and what it is doing. I do understand the rest but I've just been wondering it's use for the past half hour so any help will be greatly appreciated

I saw that using the z variable in this fashion will make the code better and easier to understand so i wrote it, however am unsure what it's purpose is.