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);
}
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
– RodrigoBorth
Thanks for the suggestion Rodrigo, I’ll edit the question.
– Edson Borge
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...– Math
Take a look at this other question: Point function in calculator (broken numbers), maybe it’ll help you..
– Math
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.
– Math
Swing calculator ( ° ʖ °)
– Oralista de Sistemas