jueves, 24 de febrero de 2011

Calculadora y Factorial en J2ME



package gui;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
public class main extends MIDlet implements CommandListener{
//Manejamos la pantalla con el display
public Display display;
//Aqui cargamos los texfield, list y group
public Form screen;
//Opciones del menu principal
public Command salir, calcular;
//Menu de opciones
public List menu;
public TextField dato1, operacion, dato2, total;
public ChoiceGroup operador;
public int mc;
public main(){
mc = 0;
dato1 = new TextField("1er Numero: ", "", 15, TextField.NUMERIC);
operador = new ChoiceGroup("Operacion:", ChoiceGroup.EXCLUSIVE);
dato2 = new TextField("2do Numero", "", 15, TextField.NUMERIC);
total = new TextField("Resultado", "", 15, TextField.UNEDITABLE);
calcular = new Command("Calcular", Command.OK, 2);
salir = new Command("Salir", Command.EXIT, 1);
}
public void startApp() {
display = Display.getDisplay(this);
menu();
}
public void menu(){
menu = new List("MENU PRINCIPAL", List.IMPLICIT);
menu.append("Calculadora", null);
menu.append("Factorial", null);
menu.addCommand(salir);
display.setCurrent(menu);
menu.setCommandListener(this);
}
private void organizaCalculadora() {
mc = 1;
screen = new Form("Calculadora");
display.setCurrent(screen);
screen.append(dato1);
operador.append("+", null);
operador.append("-", null);
operador.append("*", null);
operador.append("/", null);
screen.append(operador);
screen.append(dato2);
screen.append(total);
screen.addCommand(calcular);
screen.addCommand(salir);
screen.setCommandListener(this);
}
private void organizaFactoria(){
mc = 2;
screen = new Form("Factorial");
display.setCurrent(screen);
screen.append(dato1);
screen.append(total);
screen.addCommand(calcular);
screen.addCommand(salir);
screen.setCommandListener(this);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional){notifyDestroyed();}
public void commandAction(Command c, Displayable d) {
//Si seleccionamos alguna de las opciones del menú
if (d == menu){
switch (menu.getSelectedIndex()){
case 0:
organizaCalculadora();
break;
case 1:
organizaFactoria();
break;
}
}
//Si seleccionamos calculadora realizamos esta operación
if (c == calcular && mc == 1){
int n = 0;
switch (operador.getSelectedIndex()){
case 0:
n = Integer.parseInt(dato1.getString()) + Integer.parseInt(dato2.getString());
break;
case 1:
n = Integer.parseInt(dato1.getString()) - Integer.parseInt(dato2.getString());
break;
case 2:
n = Integer.parseInt(dato1.getString()) * Integer.parseInt(dato2.getString());
break;
case 3:
n = Integer.parseInt(dato1.getString()) / Integer.parseInt(dato2.getString());
break;
}
total.setString(""+n);
}
//Si seleccionamos factorial realizamos esta operación
if (c == calcular && mc == 2){
int n,x = 0;
n = Integer.parseInt(dato1.getString());
if (n == 0 || n == 1){
n = 1;
}else{
for (x = n; x >= 2; --x){
n = n * (x - 1) ;
}
}
total.setString(""+n);
}
if (c == salir){
destroyApp(true);
}
}
}