/**
* @(#)calculadora.java
*
* calculadora Applet application
*
* @author
* @version 1.00 2008/5/1
*/
//importo librerias qu necesito
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.applet.Applet;
import java.math.*;
public class calculadora extends Applet {
// Declaro los botones
Button boton0;
Button boton1;
Button boton2;
Button boton3;
Button boton4;
Button boton5;
Button boton6;
Button boton7;
Button boton8;
Button boton9;
Button boton10;
Button botonsuma;
Button botonresta;
Button botonmultiplicaion;
Button botondivision;
Button botonresultado;
//Declaro los text
TextField pantalla;
//variables que necesito
double num1,num2,resultado,suma,resta,multiplicacion,division;
String acumulador;
//METODO DE LA CLASE
public void init() {
//CONSTRUYO los botones con los constructores
boton0 = new Button("0");
boton1 = new Button("1");
boton2 = new Button("2");
boton3 = new Button("3");
boton4 = new Button("4");
boton5 = new Button("5");
boton6 = new Button("6");
boton7 = new Button("7");
boton8 = new Button("8");
boton9 = new Button("9");
boton10 = new Button("10");
botonsuma = new Button("+");
botonresta = new Button("-");
botonmultiplicaion = new Button("*");
botondivision = new Button("/");
botonresultado = new Button("=");
acumulador="";
//costruyo el TextField
pantalla=new TextField(10);
pantalla.setText("0");
//LOS PONGO EN LA VENTANA
add(pantalla);
add(boton0);
add(boton1);
add(boton2);
add(boton3);
add(boton4);
add(boton5);
add(boton6);
add(boton7);
add(boton8);
add(boton9);
add(boton10);
add(botonsuma);
add(botonresta);
add(botonmultiplicaion);
add(botondivision);
add(botonresultado);
}
public boolean action ( Event e, Object o)
{
if(e.target instanceof Button)
{
if(e.target==boton1){
acumulador=acumulador +"1";
pantalla.setText(acumulador);
}
if(e.target==boton2) {
acumulador=acumulador +"2";
pantalla.setText(acumulador);
}
if(e.target==boton3) {
acumulador=acumulador +"3";
pantalla.setText(acumulador);
}
if(e.target==boton4) {
acumulador=acumulador +"4";
pantalla.setText(acumulador);
}
if(e.target==boton5) {
acumulador=acumulador +"5";
pantalla.setText(acumulador);
}
if(e.target==boton6) {
acumulador=acumulador +"6";
pantalla.setText(acumulador);
}
if(e.target==boton7) {
acumulador=acumulador +"7";
pantalla.setText(acumulador);
}
if(e.target==boton8) {
acumulador=acumulador +"8";
pantalla.setText(acumulador);
}
if(e.target==boton9) {
acumulador=acumulador +"9";
pantalla.setText(acumulador);
}
if(e.target==boton0) {
acumulador=acumulador +"0";
pantalla.setText(acumulador);
}
//le doy eventos a los botones de las operaciones
if(e.target==botonsuma){
num1=Double.parseDouble(pantalla.getText());
num2=Double.parseDouble(pantalla.getText());
}
if(e.target==botonresta){
num1=Double.parseDouble(pantalla.getText());
num2=Double.parseDouble(pantalla.getText());
}
if(e.target==botonmultiplicacion){
num1=Double.parseDouble(pantalla.getText());
num2=Double.parseDouble(pantalla.getText());
}
if(e.target==botondivision){
num1=Double.parseDouble(pantalla.getText());
num2=Double.parseDouble(pantalla.getText());
}
if(e.target==botonresultado){
resultado=Double.parseDouble(pantalla.getText());
}
{
acumulador= Double.parseDouble(pantalla.getText());
// raiz1.setText(String.valueOf(x1));
// raiz2.setText(String.valueOf(x2));
switch(resultado) {
case 1:suma
resultado=num1+num2;
pantalla.setText(string.valueof(resultado));
break;
case 2:resta
resultado=num1-num2;
pantalla.setText(string.valueof(resultado));
break;
case 3:multiplicacion
resultado=num1*num2;
pantalla.setText(string.valueof(resultado));
break;
case 4:division
resultado=num1/num2;
pantalla.setText(string.valueof(resultado));
break;
default
}
return true;
}
}
}