How to do basic math operations?

0

I'm developing a Java calculator for a college assignment where you can do the four basic operations (addition, subtraction, multiplication and division) plus a square root of a number, but for now I've only been able to do the calculator sums up the numbers, I can not make the other operations' methods work.

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

Follow the 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);
}
    
asked by anonymous 13.11.2014 / 17:25

1 answer

1

What must have happened Edson, is as follows:

  

Select number 1:
The read value, which was 0, is   multiplied by 10, remaining 0; The reading value and   increased by 1 to 1; The value 1 is displayed in the    Select "+" operation:
Memory, contains value   0, is added with reading 1, becoming 1; Reading becomes 0;   Display value "" on screen;
Select number 2:
O   reading value, which was 0, is multiplied by 10,   0; The reading value is incremented by 2 to 2; IS   the value 2 is displayed on the screen.
Select "=" operation:
  A check of the operation term occurs, "+" is recognized;
  Memory, value 1, receives the reading value, value 2, as increment,   going to be worth 3; Reading becomes 0; Memory value is displayed, 3.

This operation is correct, but as @Math commented on the question, let's perform the multiplication function table test:

  

Select number 1:
The read value, which was 0, is   multiplied by 10, remaining 0; The reading value and   increased by 1 to 1; The value 1 is displayed in the   
Select operation "":
Memory, which contains value 0,   has its value multiplied by reading, remaining 0; Reading   becomes 0; "" Is displayed on the screen;
Select   number 2:
The read value, which was 0, is multiplied by 10,   remaining 0; The reading value is incremented by 2,   to be 2; The value 2 is displayed on the screen.
Select operation   "=":
A check of the operation is performed, "" is   recognized; Memory, value 0, receives the reading value, 2, for   multiplication; The memory value remains 0; Reading goes on   have value 0; The memory value, 0, is displayed on the screen

I have corrected the code in some points, and I will pass it on to you. I hope you understand.

In each actionListener, instead of performing its function:

memoria /= leitura;
leitura = 0;

I put it as:

memoria = leitura;
leitura = 0;

No drastic change, we'll let the equal button solve the formulas. In the events of the Root and Quadratic buttons, I have added, in addition to the operation definition of the function:

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

In each% of% of button case I replace your content with:

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

In%% of "√" and "x²" I put a value btIgual auxiliary (value1, value2) which receives the corresponding cases function, and, of course, Double

Now it's working! Hope it helps!

    
14.11.2014 / 12:41