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);
}