calculadora con eventos que suma,resta,divide,multiplica,numero mayor,raiz de 1 numero y raiz del 2 numero en java

Technology

calculadora con eventos que suma,resta,divide,multiplica,numero mayor,raiz de 1 numero y raiz del 2 numero en java

asi es una calculadora con eventos en java que hace operaciones con 2 numeros en un formulario

creo un pakete llamado actividadnumero1 y dentro de ese pakete creo la clase llamada
calculadoraporandres2288 y dentro de esa clase pego el siguiente codigo:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package actividadnumero1;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

//
public class calculadoraporandres2288 extends Applet implements ActionListener {
Label l1,l4,r,espa;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5,b6,b7,b8;

public calculadoraporandres2288() {
l1 = new Label("1 Numero");

l4 = new Label("2 numero");
r = new Label("resultado");
t1 = new TextField(8);

t2 = new TextField(8);
t3 = new TextField(8);
b1 = new Button("Suma");

b2 = new Button("Limpia");
b3 = new Button("resta");
b4 = new Button("multiplica");
b5 = new Button("divide");
b6 = new Button("Raiz de 1");
b7 = new Button("Raiz de 2");
b8 = new Button("Mayor");
espa = new Label(" ");
add(l1);

add(t1);
add(l4);
add(t3);
add(espa);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);


add(r);
add(t2);//es el resultado


b1. addActionListener(this);
b2. addActionListener(this);
b3. addActionListener(this);
b4. addActionListener(this);
b5. addActionListener(this);
b6. addActionListener(this);
b7. addActionListener(this);
b8. addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
int n = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t3.getText());
int suma = 0;/*
for (int i = 1; i<= n; i++) {
suma += i;
}*/
suma=n+n2;
t2.setText("" + suma);
}
if (ae.getSource() == b2) {
t1.setText("");
t2.setText("");
t3.setText("");
}
if(ae.getSource() == b3){
int n = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t3.getText());
int resta = 0;
resta=n2-n;
t2.setText("" + resta);
}
if(ae.getSource() == b4){
int n = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t3.getText());
int multipli = 0;
multipli=n2*n;
t2.setText("" +multipli);
}
if(ae.getSource() == b5){
int n = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t3.getText());
int divide = 0;
divide=n/n2;
t2.setText("" +divide);
}

if(ae.getSource() == b6){
int n = Integer.parseInt(t1.getText());

double raiz1 = (double) Math.sqrt(n);

t2.setText("" +raiz1);
}
if(ae.getSource() == b7){
int n = Integer.parseInt(t3.getText());

double raiz1 = (double) Math.sqrt(n);

t2.setText("" +raiz1);
}
if(ae.getSource() == b8){
int n = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t3.getText());
if(n>n2){
t2.setText("" +n);
}
else{
t2.setText("" +n2);
}


}
}
}




aqui una imagen de la salidad

Post a Comment

0 Comments