How to do basic mathematical operations?

Asked

Viewed 869 times

0

I’m developing a Java calculator for a college job where it’s possible to do the four basic operations (addition, subtraction, multiplication and division), plus square root and square number, but for now I only managed to make the calculator add up the numbers, I’m not getting the methods of other operations to work.

I wish someone could guide me and show me where I’m going wrong to do it right.

Follows code below:

public class CalculadoraSwing extends JFrame {

private static final long serialVersionUID = 1L;

private JButton btUm,
                btDois,
                btTres,
                btQuatro,
                btCinco,
                btSeis,
                btSete,
                btOito,
                btNove,
                btZero;

private JButton btMais,
                btMenos,
                btVezes,
                btDividido,
                btRaiz,
                btQuadrado,
                btIgual,
                btClear;                


private JTextField display;
private int leitura;
private int memoria;
private String operacao;

public CalculadoraSwing() {

    this.setTitle("Exemplo Botão Somar");
    this.setBounds(0, 0, 310, 330);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.getContentPane().setLayout(null);

    display = new JTextField();
    display.setBounds(25, 25, 245, 30);
    this.add(display);


    //------------OPERADORES-------------
    btMais = new JButton("+");
    btMais.setBounds(175, 70, 45, 45);
    this.add(btMais);

    btMenos = new JButton();
    btMenos.setText("-");
    btMenos.setBounds(225, 70, 45, 45);
    this.add(btMenos);

    btVezes = new JButton();
    btVezes.setText("*");
    btVezes.setBounds(175, 120, 45, 45);
    this.add(btVezes);

    btDividido = new JButton();
    btDividido.setText("/");
    btDividido.setBounds(225, 120, 45, 45);
    this.add(btDividido);

    btRaiz = new JButton();
    btRaiz.setText("√");
    btRaiz.setBounds(175, 170, 45, 45);
    this.add(btRaiz);

    btQuadrado = new JButton();
    btQuadrado.setText("x²");
    btQuadrado.setBounds(225, 170, 45, 45);
    this.add(btQuadrado);


    btIgual = new JButton();
    btIgual.setText("=");
    btIgual.setBounds(175, 220, 45, 45);
    this.add(btIgual);

    btClear = new JButton();
    btClear.setText("C");
    btClear.setBounds(225, 220, 45, 45);
    this.add(btClear);


    //------------NUMERICOS-------------
    btUm = new JButton();
    btUm.setText("1");
    btUm.setBounds(25, 70, 45, 45);
    this.add(btUm);

    btDois = new JButton();
    btDois.setText("2");
    btDois.setBounds(75, 70, 45, 45);
    this.add(btDois);

    btTres = new JButton();
    btTres.setText("3");
    btTres.setBounds(125, 70, 45, 45);
    this.add(btTres);

    btQuatro = new JButton();
    btQuatro.setText("4");
    btQuatro.setBounds(25, 120, 45, 45);
    this.add(btQuatro);

    btCinco = new JButton();
    btCinco.setText("5");
    btCinco.setBounds(75, 120, 45, 45);
    this.add(btCinco);

    btSeis = new JButton();
    btSeis.setText("6");
    btSeis.setBounds(125, 120, 45, 45);
    this.add(btSeis);

    btSete = new JButton();
    btSete.setText("7");
    btSete.setBounds(25, 170, 45, 45);
    this.add(btSete);

    btOito = new JButton();
    btOito.setText("8");
    btOito.setBounds(75, 170, 45, 45);
    this.add(btOito);

    btNove = new JButton();
    btNove.setText("9");
    btNove.setBounds(125, 170, 45, 45);
    this.add(btNove);

    btZero = new JButton();
    btZero.setText("0");
    btZero.setBounds(25, 220, 145, 45);
    this.add(btZero);


    btMais.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = "+";
            memoria += leitura;
            leitura = 0;
            display.setText("");
        }
    });

    btMenos.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = "-";
            memoria -= leitura;
            leitura = 0;
            display.setText("");
        }
    });

    btVezes.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = "*";
            memoria *= leitura;
            leitura = 0;
            display.setText("");
        }
    });

    btDividido.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = "/";
            memoria /= leitura;
            leitura = 0;
            display.setText("");
        }
    });

   btRaiz.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = "√";
            Math.pow(Double.valueOf(memoria),2);
            display.setText("");
        }
    });

    btQuadrado.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            operacao = "x²";
            Math.sqrt(Double.valueOf(memoria));
            display.setText("");
        }
    }); 

    btIgual.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            switch (operacao) {
            case "+":
                memoria += leitura;
                break;

            case "-":
                memoria -= leitura;
                break;

            case "*":
                memoria *= leitura;
                break;

            case "/":
                memoria /= leitura;
                break;

            case "x²":
               Math.pow(Double.valueOf(memoria),2);
               break;

            case "√":
                Math.sqrt(Double.valueOf(memoria));
                break;
           }
            leitura = 0;
            display.setText("" + memoria);

        }

    });

    btClear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
             memoria = 0;
             leitura = 0;
            display.setText("");
        }
    });

    btUm.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 1;
            display.setText(display.getText() + "1");
        }
    });

    btDois.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 2;
            display.setText(display.getText() + "2");
        }
    });

    btTres.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 3;
            display.setText(display.getText() + "3");
        }
    });

    btQuatro.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 4;
            display.setText(display.getText() + "4");
        }
    });

    btCinco.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 5;
            display.setText(display.getText() + "5");
        }
    });

    btSeis.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 6;
            display.setText(display.getText() + "6");
        }
    });

    btSete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 7;
            display.setText(display.getText() + "7");
        }
    });

    btOito.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 8;
            display.setText(display.getText() + "8");
        }
    });

    btNove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 9;
            display.setText(display.getText() + "9");
        }
    });

    btZero.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            leitura *= 10;
            leitura += 0;
            display.setText(display.getText() + "0");
        }
    });

}

public static void main(String[] args) {
    CalculadoraSwing exemplo = new CalculadoraSwing();
    exemplo.setVisible(true);
}
  • 4

    Welcome to Stack Overflow in Portuguese, aka Sopt, I suggest you explain your doubt better, be more specific about your difficulty, explain where you are missing what you want to do and what is happening when you try, so it becomes easier for someone to provide you with a possible solution

  • Thanks for the suggestion Rodrigo, I’ll edit the question.

  • 2

    There is a lot of error there, it is difficult to gather all to put in an answer. 1) When pressing the * or the / you already do the account right away, but one of the variables is zero, so you multiply by zero, or take zero and divide by any value, the result will always be 0. 2) At the root and exponent, you are not storing the result. For example you had to do: case "x²": memoria = (int) Math.pow(Double.valueOf(memoria),2); but then you’ll also be losing accuracy. Among several other errors...

  • 1

    Take a look at this other question: Point function in calculator (broken numbers), maybe it’ll help you..

  • The ideal is you go debugging, line by line to understand the logic that you developed, then you will be able to correct the errors little by little, and improve the logic of your program as well.

  • 3

    Swing calculator ( ° ʖ °)

Show 1 more comment

1 answer

1

What must have happened, Edson, is this:

I select number 1:
The reading value, which was 0, is multiplied by 10, remaining 0; The reading value and incremented by 1 to 1 screen.
I select operation "+":
Memory, contains a value 0, is summed with reading 1, becoming 1; Reading becomes 0; Value "" is displayed on the screen;
I select number 2:
The reading value, which was 0, is multiplied by 10, continuing to be 0; Read and incremented by 2 to be 2; Is displayed the value 2 on the screen.
I select operation "=":
Occurs a verification of the term operation, "+" is recognized;
Memory, value 1, takes the reading value, value 2, as increment, becoming 3; Reading becomes 0; Memory is shown, 3.

This operation is right, but as @Math commented under the question, we will perform the table test of the multiplication function:

I select number 1:
The reading value, which was 0, is multiplied by 10, continuing to be 0; The reading value and incremented by 1 to 1; Value 1 in screen.
I select operation "":
Memoria, which contains value 0, has its value multiplied by reading, still 0; Reading is set to 0; value "" is displayed on the screen;
Select number 2:
The reading value, which was 0, is multiplied by 10, still 0; The reading value is incremented by 2, passing to be 2; value 2 is displayed on the screen.
I select operation "=":
A verification of the term operation, "" is recognized; Memoria, value 0, receives the reading value, 2, for multiplication; The memory value remains 0; Reading becomes 0 have value 0; The memory value, 0, is displayed on the screen

I’ve corrected the code on a few points, and I’ll pass it on to you. I hope you understand.

In each actionListener, instead of performing its function:

memoria /= leitura;
leitura = 0;

I put as:

memoria = leitura;
leitura = 0;

No drastic changes, let’s let the equals button solve the formulas. In the events of the Root and Quadratic buttons, I put in addition to the function operation setting:

memoria = leitura;
btIgual.doClick(0); // Irá invocar o evento click do btIgual;

In each case button btIgual I replaced its contents with:

leitura = memoria # leitura;// Substituir # pela função correspondente
display.setText(leitura.toString());
break;

Us cases of " and "x²" put a value Double auxiliary (value1, value2) receiving the function Math corresponding, and, of course, the display.setText(valor.toString());

Now it is working! I hope it’s helpful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.