Question

In: Computer Science

Gui calculator in Java with additon, subtraction, multiplication, division, sine, cosine, tangent, square, cube, raise to...

Gui calculator in Java with additon, subtraction, multiplication, division, sine, cosine, tangent, square, cube, raise to x to the power of y functionality. It should also have memory button, memory recall button and clear.

Solutions

Expert Solution

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

public class Calculator extends JFrame implements ActionListener {
        JTextField tfield;
        double temp, temp1, result, a;
        static double m1, m2;
        int k = 1, x = 0, y = 0, z = 0;
        char ch;
        JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, pow3, exp,
                        fac, plus, min, div, log, rec, mul, eq, addSub, dot, mr, mc, mp,
                        mm, sqrt, sin, cos, tan;
        Container contain;
        JPanel textPanel, buttonpanel;

            Calculator() {
                contain = getContentPane();
                contain.setLayout(new BorderLayout());
                JPanel textpanel = new JPanel();
                tfield = new JTextField(25);
                tfield.setHorizontalAlignment(SwingConstants.RIGHT);
                tfield.addKeyListener(new KeyAdapter() {
                        public void keyTyped(KeyEvent keyevent) {
                                char c = keyevent.getKeyChar();
                                if (c >= '0' && c <= '9') {
                                } else {
                                        keyevent.consume();
                                }
                        }
                });
                textpanel.add(tfield);
                buttonpanel = new JPanel();
                buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));
                boolean t = true;
                mr = new JButton("MR");
                buttonpanel.add(mr);
                mr.addActionListener(this);
                mc = new JButton("MC");
                buttonpanel.add(mc);
                mc.addActionListener(this);

                mp = new JButton("M+");
                buttonpanel.add(mp);
                mp.addActionListener(this);

                mm = new JButton("M-");
                buttonpanel.add(mm);
                mm.addActionListener(this);

                b1 = new JButton("1");
                buttonpanel.add(b1);
                b1.addActionListener(this);
                b2 = new JButton("2");
                buttonpanel.add(b2);
                b2.addActionListener(this);
                b3 = new JButton("3");
                buttonpanel.add(b3);
                b3.addActionListener(this);

                b4 = new JButton("4");
                buttonpanel.add(b4);
                b4.addActionListener(this);
                b5 = new JButton("5");
                buttonpanel.add(b5);
                b5.addActionListener(this);
                b6 = new JButton("6");
                buttonpanel.add(b6);
                b6.addActionListener(this);

                b7 = new JButton("7");
                buttonpanel.add(b7);
                b7.addActionListener(this);
                b8 = new JButton("8");
                buttonpanel.add(b8);
                b8.addActionListener(this);
                b9 = new JButton("9");
                buttonpanel.add(b9);
                b9.addActionListener(this);

                zero = new JButton("0");
                buttonpanel.add(zero);
                zero.addActionListener(this);

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

                min = new JButton("-");
                buttonpanel.add(min);
                min.addActionListener(this);

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

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

                addSub = new JButton("+/-");
                buttonpanel.add(addSub);
                addSub.addActionListener(this);

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

                eq = new JButton("=");
                buttonpanel.add(eq);
                eq.addActionListener(this);

                rec = new JButton("1/x");
                buttonpanel.add(rec);
                rec.addActionListener(this);
                sqrt = new JButton("Sqrt");
                buttonpanel.add(sqrt);
                sqrt.addActionListener(this);
                log = new JButton("log");
                buttonpanel.add(log);
                log.addActionListener(this);

                sin = new JButton("SIN");
                buttonpanel.add(sin);
                sin.addActionListener(this);
                cos = new JButton("COS");
                buttonpanel.add(cos);
                cos.addActionListener(this);
                tan = new JButton("TAN");
                buttonpanel.add(tan);
                tan.addActionListener(this);
                pow2 = new JButton("x^2");
                buttonpanel.add(pow2);
                pow2.addActionListener(this);
                pow3 = new JButton("x^3");
                buttonpanel.add(pow3);
                pow3.addActionListener(this);
                exp = new JButton("Exp");
                exp.addActionListener(this);
                buttonpanel.add(exp);
                fac = new JButton("n!");
                fac.addActionListener(this);
                buttonpanel.add(fac);

                clr = new JButton("AC");
                buttonpanel.add(clr);
                clr.addActionListener(this);
                cont.add("Center", buttonpanel);
                cont.add("North", textpanel);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public void actionPerformed(ActionEvent e) {
                String s = e.getActionCommand();
                if (s.equals("1")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "1");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "1");
                                z = 0;
                        }
                }
                if (s.equals("2")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "2");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "2");
                                z = 0;
                        }
                }
                if (s.equals("3")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "3");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "3");
                                z = 0;
                        }
                }
                if (s.equals("4")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "4");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "4");
                                z = 0;
                        }
                }
                if (s.equals("5")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "5");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "5");
                                z = 0;
                        }
                }
                if (s.equals("6")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "6");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "6");
                                z = 0;
                        }
                }
                if (s.equals("7")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "7");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "7");
                                z = 0;
                        }
                }
                if (s.equals("8")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "8");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "8");
                                z = 0;
                        }
                }
                if (s.equals("9")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "9");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "9");
                                z = 0;
                        }
                }
                if (s.equals("0")) {
                        if (z == 0) {
                                tfield.setText(tfield.getText() + "0");
                        } else {
                                tfield.setText("");
                                tfield.setText(tfield.getText() + "0");
                                z = 0;
                        }
                }
                if (s.equals("AC")) {
                        tfield.setText("");
                        x = 0;
                        y = 0;
                        z = 0;
                }
                if (s.equals("log")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.log(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("1/x")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = 1 / Double.parseDouble(tfield.getText());
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("Exp")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.exp(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("x^2")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.pow(Double.parseDouble(tfield.getText()), 2);
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("x^3")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.pow(Double.parseDouble(tfield.getText()), 3);
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("+/-")) {
                        if (x == 0) {
                                tfield.setText("-" + tfield.getText());
                                x = 1;
                        } else {
                                tfield.setText(tfield.getText());
                        }
                }
                if (s.equals(".")) {
                        if (y == 0) {
                                tfield.setText(tfield.getText() + ".");
                                y = 1;
                        } else {
                                tfield.setText(tfield.getText());
                        }
                }
                if (s.equals("+")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                                temp = 0;
                                ch = '+';
                        } else {
                                temp = Double.parseDouble(tfield.getText());
                                tfield.setText("");
                                ch = '+';
                                y = 0;
                                x = 0;
                        }
                        tfield.requestFocus();
                }
                if (s.equals("-")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                                temp = 0;
                                ch = '-';
                        } else {
                                x = 0;
                                y = 0;
                                temp = Double.parseDouble(tfield.getText());
                                tfield.setText("");
                                ch = '-';
                        }
                        tfield.requestFocus();
                }
                if (s.equals("/")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                                temp = 1;
                                ch = '/';
                        } else {
                                x = 0;
                                y = 0;
                                temp = Double.parseDouble(tfield.getText());
                                ch = '/';
                                tfield.setText("");
                        }
                        tfield.requestFocus();
                }
                if (s.equals("*")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                                temp = 1;
                                ch = '*';
                        } else {
                                x = 0;
                                y = 0;
                                temp = Double.parseDouble(tfield.getText());
                                ch = '*';
                                tfield.setText("");
                        }
                        tfield.requestFocus();
                }
                if (s.equals("MC")) {
                        m1 = 0;
                        tfield.setText("");
                }
                if (s.equals("MR")) {
                        tfield.setText("");
                        tfield.setText(tfield.getText() + m1);
                }
                if (s.equals("M+")) {
                        if (k == 1) {
                                m1 = Double.parseDouble(tfield.getText());
                                k++;
                        } else {
                                m1 += Double.parseDouble(tfield.getText());
                                tfield.setText("" + m1);
                        }
                }
                if (s.equals("M-")) {
                        if (k == 1) {
                                m1 = Double.parseDouble(tfield.getText());
                                k++;
                        } else {
                                m1 -= Double.parseDouble(tfield.getText());
                                tfield.setText("" + m1);
                        }
                }
                if (s.equals("Sqrt")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.sqrt(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("SIN")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.sin(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("COS")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.cos(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("TAN")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = Math.tan(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                if (s.equals("=")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                temp1 = Double.parseDouble(tfield.getText());
                                switch (ch) {
                                case '+':
                                        result = temp + temp1;
                                        break;
                                case '-':
                                        result = temp - temp1;
                                        break;
                                case '/':
                                        result = temp / temp1;
                                        break;
                                case '*':
                                        result = temp * temp1;
                                        break;
                                }
                                tfield.setText("");
                                tfield.setText(tfield.getText() + result);
                                z = 1;
                        }
                }
                if (s.equals("n!")) {
                        if (tfield.getText().equals("")) {
                                tfield.setText("");
                        } else {
                                a = fact(Double.parseDouble(tfield.getText()));
                                tfield.setText("");
                                tfield.setText(tfield.getText() + a);
                        }
                }
                tfield.requestFocus();
        }

        double fact(double x) {
                int er = 0;
                if (x < 0) {
                        er = 20;
                        return 0;
                }
                double i, s = 1;
                for (i = 2; i <= x; i += 1.0)
                        s *= i;
                return s;
        }

        public static void main(String args[]) {
                try {
                        UIManager
                                        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (Exception e) {
                }
                Calculator f = new Calculator();
                f.setTitle("Calculator");
                f.pack();
                f.setVisible(true);
        }
}

Related Solutions

Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
Find the exact values of the sine, cosine, and tangent of the angle. −195° = 30°...
Find the exact values of the sine, cosine, and tangent of the angle. −195° = 30° − 225° sin(−195°) = cos(−195°) = tan(−195°) =
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2?...
Create a script that calculates the sine, cosine, and tangent of a vector from 0 to2? with steps of ?/16. The numbers should have three digits to the right of the decimal. Determine an appropriate field width. Use fprintf to display the output in tabular format with the following headers. x sin(x) cos(x) tan(x) Also, plot the three equations in the same figure window, but on separate axes. Plot all thee equations from 0 to 2? on the x-axis. Plot...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication,...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should request a numerical input from the user, followed by the operation to be performed, and the second number to complete the equation. The result should be displayed to the user after each equation is performed. For example, if the user performs 3+3, the program should display 6 as the result. The program should continue running so the user can...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Ahrithmetic calculation about scientific notation. Mixed unit and multiplication, addition, subtraction, division too. this is my...
Ahrithmetic calculation about scientific notation. Mixed unit and multiplication, addition, subtraction, division too. this is my question above, please help me I need it to study.
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT