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

6 comentarios:

  1. Otro algoritmo para la calculadora

    private void calc() {
    int d1 = Integer.parseInt(campo1.getString());
    String d2 = campo2.getString();
    int d3 = Integer.parseInt(campo3.getString());
    char signo = d2.charAt(0);
    switch(signo){
    case '+' : res = d1+d3;
    case '/' : res = d1/d3;
    case '-' : res = d1-d3;
    case '*' : res = d1*d3;
    }
    campo4.setString(""+res);
    }

    ResponderEliminar
  2. //factorial
    package clases;

    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.TextField;
    import javax.microedition.midlet.*;

    /**
    * @author Wilmer Lopez
    */
    public class Factorial extends MIDlet implements CommandListener {

    public Display mostrar;
    public Form ventanaConfirma;
    public Form ventana;
    public Command calcular;
    public Command salir;
    public Command entrar;
    public Command volver;
    public Command aceptadatos;
    public TextField campo;
    public int res;

    public Factorial() {
    ventanaConfirma = new Form("Resultado factorial");
    calcular = new Command("Calcular", Command.SCREEN, 1);
    entrar = new Command("Entrar", Command.SCREEN, 1);
    volver = new Command("Volver", Command.SCREEN, 1);
    salir = new Command("Salir", Command.EXIT, 2);
    res = 1;
    }

    public void startApp() {
    ventanaInicial();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
    if (c==calcular) {
    calculaFactorial();
    resultado();
    }
    if (c==volver) {
    ventanaInicial();
    }
    if (c==salir) {
    destroyApp(false);
    notifyDestroyed();
    }
    }

    public void calculaFactorial(){
    int x = Integer.parseInt(campo.getString());
    System.out.println("xx"+x);
    for(int i=1; i<x; i++){
    res+= i*res;
    }
    }

    public void ventanaInicial(){
    ventana = new Form("Resultado");
    campo = new TextField("n", "", 20, TextField.NUMERIC);
    mostrar= Display.getDisplay(this);
    ventana.append(campo);
    ventana.addCommand(calcular);
    ventana.addCommand(salir);
    mostrar.setCurrent(ventana);
    ventana.setCommandListener((CommandListener)this);
    }

    public void resultado(){
    String datos = new String();
    datos = "El factorial de n es: "+res;
    mostrar = Display.getDisplay(this);
    ventanaConfirma.append(datos);
    ventanaConfirma.addCommand(volver);
    mostrar.setCurrent(ventanaConfirma);
    ventanaConfirma.setCommandListener(this);
    }
    }

    ResponderEliminar
  3. excelente por los compañeros que contribuyen con el conocimiento y crecimiento intelectual a toda la comunidad cibernauta en todos los rincones del planeta tierra.

    ResponderEliminar
  4. C:\Users\Athlon X2\j2mewtk\2.5.2\apps\Factorial\src\Factorial.java:38: cannot find symbol
    symbol : method ventanaInicial()
    location: class clases.Factorial
    ventanaInicial();
    ^
    C:\Users\Athlon X2\j2mewtk\2.5.2\apps\Factorial\src\Factorial.java:49: cannot find symbol
    symbol : method calculaFactorial()
    location: class clases.Factorial
    calculaFactorial();
    ^
    C:\Users\Athlon X2\j2mewtk\2.5.2\apps\Factorial\src\Factorial.java:50: cannot find symbol
    symbol : method resultado()
    location: class clases.Factorial
    resultado();
    ^
    C:\Users\Athlon X2\j2mewtk\2.5.2\apps\Factorial\src\Factorial.java:53: cannot find symbol
    symbol : method ventanaInicial()
    location: class clases.Factorial
    ventanaInicial();
    ^
    4 errors

    Tengo estos errores

    ResponderEliminar
  5. Revisa el nomnbre de la clase pues la del ejemplo en el comentario se llama Factorial, entonces cuando crees una clase midlet le pones ese nombre, yo lo pruebo y funciona.

    ResponderEliminar