Вы находитесь на странице: 1из 5

Calculator Coding -1:

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class CalcMidlet extends MIDlet implements CommandListener { private Display display; private Form frm; private TextField txtfld1=new TextField("Number 1","",20,TextField.ANY); private TextField txtfld2=new TextField("Number 2","",20,TextField.ANY); private TextField txtfld3=new TextField("Result","",20,TextField.ANY); Command calculate=new Command("calculate",Command.SCREEN,1); Command exit= new Command("Exit",Command.EXIT,0); String menuArr[]={"+","-","*","/","%"}; private ChoiceGroup radioChoice=new ChoiceGroup("Select",Choice.EXCLUSIVE,menuArr,null); public void startApp() { frm=new Form("Calculator"); frm.append(txtfld1); frm.append(txtfld2); frm.append(txtfld3); frm.append(radioChoice); frm.addCommand(calculate); frm.addCommand(exit); frm.setCommandListener(this); Display.getDisplay(this).setCurrent(frm); } public void pauseApp() {} public void destroyApp(boolean unConditional) {} public void commandAction(Command cmd, Displayable displayable) { if(cmd==exit) { destroyApp(false); notifyDestroyed(); } else if(cmd==calculate) { int i1,i2,res=0; try { i1=Integer.parseInt(txtfld1.getString());

i2=Integer.parseInt(txtfld2.getString()); switch(radioChoice.getSelectedIndex()) { case 0:{ res=i1+i2; break; } case 1:{ res=i1-i2; break; } case 2:{ res=i1*i2; break; } case 3:{ res=i1/i2; break; } case 4:{ res=i1%i2; break; } default: break; } } catch(Exception e) { System.out.println("Exception is caught"+e); e.printStackTrace(); } txtfld3.setString(Integer.toString(res)); } } }

Output:

Calculator Coding -2:


import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Calculator extends MIDlet implements CommandListener { private Display display; // The display for this MIDlet private Form form; private TextField a,b,c; private Command add,sub,mul,exit; private Alert alert; public Calculator() { a = new TextField("n1:", "", 30, TextField.ANY); b = new TextField("n2:", "", 30, TextField.ANY); add = new Command("Add", Command.OK, 1); sub = new Command("Sub", Command.OK, 2); mul = new Command("Mul", Command.OK, 3); exit= new Command("Exit", Command.EXIT, 0); display = Display.getDisplay(this); } public void startApp() { form = new Form("Calculator"); form.append(a); form.append(b); form.addCommand(add); form.setCommandListener(this); form.addCommand(sub); form.setCommandListener(this); form.addCommand(mul); form.setCommandListener(this); form.addCommand(exit); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) {

String n1 = a.getString(); String n2 = b.getString(); if (c == add) { display = Display.getDisplay(this); int res=Integer.parseInt(n1)+Integer.parseInt(n2); alert=new Alert("result",res+"",null,AlertType.INFO); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert,form); } if (c == sub) { display = Display.getDisplay(this); int res=Integer.parseInt(n1)-Integer.parseInt(n2); alert=new Alert("result",res+"",null,AlertType.INFO); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert,form); } if (c == mul) { display = Display.getDisplay(this); int res=Integer.parseInt(n1)*Integer.parseInt(n2); alert=new Alert("result",res+"",null,AlertType.INFO); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert,form); } if(c==exit) { destroyApp(false); notifyDestroyed(); } } }