J2Me Coding

Only available on StudyMode
  • Topic: Class, C++, Java
  • Pages : 46 (4143 words )
  • Download(s) : 64
  • Published : April 12, 2013
Open Document
Text Preview
1
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

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 rdConverter()
{
display = Display.getDisplay(this);
form =new Form(" Input currency ");
rupees= new TextField("Amount in Rupees:","",30,TextField.NUMERIC);
dollar = new TextField("Amount in dollar","",30,TextField.NUMERIC);
OK =new Command("OK",Command.OK, 2);
}
public void startApp()
{
form.append(rupees);
form.append(dollar);
form.addCommand(OK);
form.setCommandListener(this);
display.setCurrent(form);
}

public void showResult()
{
Form f =new Form("Conversion");
display = Display.getDisplay(this);
String r = rupees.getString();
String d = dollar.getString();
double rup=(double)Integer.parseInt(d,10);
rup=rup*54.33;
double dol=(double)Integer.parseInt(r,10);
dol=dol/54.33;
String rupees= Double.toString(rup);
String dollers= Double.toString(dol);
String doller= r+" Rupees in doller is:\n "+ dollers;
String space="\n\n\n\n\n";
String rupee= d+"Dollers in rupees is:\n "+ rupees;
f.append(rupee);
f.append(space);
f.append(doller);
display.setCurrent(f);
}
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
if(label.equals("OK"))
{
showResult();
}
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}

3
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Area extends MIDlet implements CommandListener
{
private Display display;

private List list;
private Command cal,cala;
private TextField txtl,txtb,txtr;

public Area()
{

list = new List("Shape List", Choice.IMPLICIT);
cal = new Command("show", Command.OK, 1);
cala = new Command("calculate", Command.OK, 1);
txtl=new TextField("Length : ","",30,TextField.NUMERIC);
txtb=new TextField("Breath : ","",30,TextField.NUMERIC);
txtr=new TextField("Radius : ","",30,TextField.NUMERIC);

display = Display.getDisplay(this);
}

public void...
tracking img