public class Calculator extends MIDlet implements CommandListener
{
private Display dsp; private Form fm; private TextField tf1,tf2; private StringItem sitres; private ChoiceGroup cg; private Command cmdq,cmdcalc,cmddate; public Calculator() { fm=new Form("Calculations"); tf1= new TextField("Number1:","",3,TextField.NUMERIC); tf2= new TextField("Number2:","",3,TextField.NUMERIC); sitres=new StringItem("Result",""); cg=new ChoiceGroup("Operation",ChoiceGroup.EXCLUSIVE); cg.append("Add",null); cg.append("Subtract",null); cg.append("Mul",null); cg.append("Div",null); cmdq=new Command("Quit",Command.EXIT,0); cmdcalc=new Command("Calculate",Command.SCREEN,0); fm.append(tf1); fm.append(tf2); fm.append(sitres); fm.append(cg); fm.addCommand(cmdcalc); fm.addCommand(cmdq); fm.setCommandListener(this); } private void calc() { int n1=0,n2=0,res=0; try { n1=Integer.parseInt(tf1.getString()); n2=Integer.parseInt(tf2.getString()); if(cg.getSelectedIndex()==0) res=n1+n2; if(cg.getSelectedIndex()==1) res=n1-n2; if(cg.getSelectedIndex()==2) res=n1*n2; if(cg.getSelectedIndex()==3) res=n1/n2; sitres.setText(res+""); } catch(NumberFormatException e) { sitres.setText("unknown"); } } public void commandAction(Command c, Displayable d) { if(c==cmdcalc) { calc(); return; } notifyDestroyed(); }
public void startApp() { dsp=Display.getDisplay(this); dsp.setCurrent(fm); } public void pauseApp(){} public void destroyApp(boolean unconditional){} }
2 import javax.microedition.lcdui.*; import javax.microedition.midlet.*;
public class rdConverter extends MIDlet implements CommandListener
{
private Display display; TextField rupees; TextField dollar; Form form; Command OK; public