Olá por favor estou precisando da ajuda de algum de vocês aí eu sou iniciante em java esse programa a seguir é de uma calculadora em java.
Eu queria que vocês me explicassem o funcionamento desse programa como que é a logica dele como que as variáveis estão operando dentro dele, queria entender ele.
até a parte da adições do botões e painéis eu já consigo entender, quero saber agora algumas outras coisas…
1 - Nessas onde estão b1.addActionListener… isso aí ta mostrando cada método que vai acessar com cada botão quando for clicado é?
2 - Especificamente nessa linha de codigo aki : addWindowListener(new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0);}});
} o que está acontecendo?
3 - class Somar implements ActionListener{
public void actionPerformed( ActionEvent e ){
operacao = ‘+’;
a = Float.parseFloat(tf1.getText());
acumulador1 = true;
}
}
essa classe o que ocorre nela? nessa e nas de outras operações como subtrair e multiplicar… quero entender o funcionamento das variáveis aí dentro
tipo ele ta implementando ela com acionlistener né… depois ta criando um evento??? e a variável operação e a e acumulador1 como que estão interagindo me expliquem a lógica disso.
4 - class Resultado implements ActionListener{
public void actionPerformed( ActionEvent e ){
if(acumulador2 == false){
b = Float.parseFloat(tf1.getText());
}
switch (operacao){
case ‘+’:
a=a+b;
tf1.setText(String.valueOf(a));
acumulador1 = true;
acumulador2 = true;
break;
nesse trecho… o que acontece nesse if? e o que ele vai verificar no switch é o sinal que foi clicado né? caso for + por ex: entra no case do ‘+’?
mas o que ta acontecendo nessa linha onde ta passando parametro (String.valueOf(a)) para o textfield??
e os acumuladores estão recebendo true para o que?
5 - class Dois implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if(“0”.equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText(“2”);
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+“2”);
}
}
e essas classes ai que levam o nome dos números da calculadora? como é a funcionalidade logica dela?
esse acumulador2 recebendo false, esse if ta comparando 0 com o q? e ou acumulador foi igual a true não o que ta fazendo ai?
na seguinte ele está passando o o “2” pro textfield né? e acumulador1 recebendo false pra que? e se cair no else o que acontece?
Bom essas são minhas dúvidas. espero que possa contar com a ajuda de vocÊs amigos. principalmente nessas variáveis acumuladores 1 e 2 que não to entendendo nada da interação delas.
Desde já agradeço a todos.
AQUI A BAIXO VAI SEGUIR O CODIGO COMPLETO PARA VCS OLHAREM.
import java.awt.*;
import java.awt.event.*;
class Calculadora extends Frame{
//variáveis e botões
TextField tf1;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bSoma,bSub,bMult,bDiv,bIgual,bC;
float a=0, b=0;
char operacao;
boolean acumulador1=false, acumulador2=false;
// construtor
Calculadora(){
super("Calculadora");
setBackground(Color.blue);
setSize(210,230);
// Painéis
Panel pMostrador = new Panel();
Panel pBotoes = new Panel();
pBotoes.setLayout(new GridLayout(4,4,4,5));
// Instanciacão do TextField e dos Botões
tf1 = new TextField("0",22);
tf1.setEditable(false);
pMostrador.add(tf1);
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
bIgual = new Button("=");
bSoma = new Button("+");
bSub = new Button("-");
bMult = new Button("*");
bDiv = new Button("/");
bC = new Button("C");
// Adição dos botões no painel Botoes
pBotoes.add(b7);
pBotoes.add(b8);
pBotoes.add(b9);
pBotoes.add(bSoma);
pBotoes.add(b4);
pBotoes.add(b5);
pBotoes.add(b6);
pBotoes.add(bSub);
pBotoes.add(b1);
pBotoes.add(b2);
pBotoes.add(b3);
pBotoes.add(bDiv);
pBotoes.add(b0);
pBotoes.add(bC);
pBotoes.add(bIgual);
pBotoes.add(bMult);
// Adição dos painéis
add(pMostrador, BorderLayout.NORTH );
add(pBotoes, BorderLayout.CENTER );
// Adição dos ActionListener nos botões
bC.addActionListener(new Limpar());
b1.addActionListener(new Um());
b2.addActionListener(new Dois());
b3.addActionListener(new Tres());
b4.addActionListener(new Quatro());
b5.addActionListener(new Cinco());
b6.addActionListener(new Seis());
b7.addActionListener(new Sete());
b8.addActionListener(new Oito());
b9.addActionListener(new Nove());
b0.addActionListener(new Zero());
bSoma.addActionListener(new Somar());
bSub.addActionListener(new Subtrair());
bMult.addActionListener(new Multiplicar());
bDiv.addActionListener(new Dividir());
bIgual.addActionListener(new Resultado());
addWindowListener(new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit(0);}});
}
class Somar implements ActionListener{
public void actionPerformed( ActionEvent e ){
operacao = '+';
a = Float.parseFloat(tf1.getText());
acumulador1 = true;
}
}
class Subtrair implements ActionListener{
public void actionPerformed( ActionEvent e ){
operacao = '-';
a = Float.parseFloat(tf1.getText());
acumulador1 = true;
}
}
class Multiplicar implements ActionListener{
public void actionPerformed( ActionEvent e ){
operacao = '*';
a = Float.parseFloat(tf1.getText());
acumulador1 = true;
}
}
class Dividir implements ActionListener{
public void actionPerformed( ActionEvent e ){
operacao = '/';
a = Float.parseFloat(tf1.getText());
acumulador1 = true;
}
}
class Resultado implements ActionListener{
public void actionPerformed( ActionEvent e ){
if(acumulador2 == false){
b = Float.parseFloat(tf1.getText());
}
switch (operacao){
case '+':
a=a+b;
tf1.setText(String.valueOf(a));
acumulador1 = true;
acumulador2 = true;
break;
case '-':
a=a-b;
tf1.setText(String.valueOf(a));
acumulador1 = true;
acumulador2 = true;
break;
case '*':
a=a*b;
tf1.setText(String.valueOf(a));
acumulador1 = true;
acumulador2 = true;
break;
case '/':
a=a/b;
tf1.setText(String.valueOf(a));
a = Float.parseFloat(tf1.getText());
acumulador1 = true;
acumulador2 = true;
break;
}
}
}
class Limpar implements ActionListener{
public void actionPerformed( ActionEvent e ){
tf1.setText("0");
acumulador1 = false;
}
}
class Zero implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText()) || (acumulador1 == true)){
tf1.setText("0");
acumulador1 = false;
}
tf1.setText(tf1.getText()+"0");
}
}
class Um implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("1");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"1");
}
}
class Dois implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("2");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"2");
}
}
class Tres implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("3");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"3");
}
}
class Quatro implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("4");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"4");
}
}
class Cinco implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("5");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"5");
}
}
class Seis implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("6");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"6");
}
}
class Sete implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("7");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"7");
}
}
class Oito implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("8");
acumulador1 = false;
}
else
tf1.setText(tf1.getText()+"8");
}
}
class Nove implements ActionListener{
public void actionPerformed( ActionEvent e ){
acumulador2 = false;
if("0".equals(tf1.getText())|| (acumulador1 == true)){
tf1.setText("9");
acumulador1 = false;
else
tf1.setText(tf1.getText()+"9");
}
}
// método main
public static void main (String[] arg){
Calculadora f = new Calculadora();
f.addWindowListener (new CloseWindowAndExit());
f.show();
}
}
Obrigado.
EDIT - por favor, use sempre as tags [ code ] para postar código - fica mais legível. O compilador sabe ler código não indentado, mas nós meros mortais não conseguimos fazer isso rapidamente.