Post on 10-Sep-2014
Write a Program in J2ME to create currency converter.
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;
/** * @author student */public class currency extends MIDlet implements CommandListener { Form d1;
TextField t1,t2,t3;Command c1,c2,c3,c4;
ChoiceGroup cg,cg1; double rs;
public void startApp() { String[] arr={"USD","Pounds","Yen","Euro"}; cg=new ChoiceGroup("",ChoiceGroup.EXCLUSIVE,arr,null); t1=new TextField("Rs","",10,TextField.NUMERIC); t2=new TextField("","",10,TextField.UNEDITABLE); d1=new Form("Form1"); c1=new Command("OK",Command.SCREEN,0); d1.append(t1); d1.append(cg); d1.addCommand(c1);d1.append(t2);
d1.setCommandListener(this);
Display.getDisplay(this).setCurrent(d1); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { } public void commandAction(Command c,Displayable d) { double num=Double.parseDouble(t1.getString()); if(c==c1)
{
if(cg.getSelectedIndex()==0) {
rs=(num*0.0217); t2.setString(""+rs);
} else if(cg.getSelectedIndex()==1) { rs=(num*1.0084); t2.setString(""+rs);
} else if(cg.getSelectedIndex()==2) { rs=(num*0.146); t2.setString(""+rs);
} else if(cg.getSelectedIndex()==3) { rs=(num*0.0166); t2.setString(""+rs);
}
}
}}
Develop a J2ME program for Quiz.
Program :
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class Quiz extends MIDlet implements CommandListener{
Display d;List L1;Form F,F1;int qcnt;int cnt;String[] opt1=new String[]{"881","888","889","880","Score is"};String[] opt2=new String[]{"163","160","167","177"};String[] opt3=new String[]{"21","25","22","23"};String[] quo=new String[]{"Q1.(222/2)*8=?","Q2.55*3+2=?","Q3.(100-31)/3=?"};Command Next=new Command("Next",Command.SCREEN,1);Command Exit=new Command("Exit",Command.EXIT,0);public Quiz(){ F=new Form("Quiz");
}public void startApp(){
qcnt=0;cnt=0;F.addCommand(Next); F.setCommandListener(this);F.addCommand(Exit);F.setCommandListener(this);F.append("Click Next to Start Quiz");d=Display.getDisplay(this); d.setCurrent(F);
}public void pauseApp(){}public void destroyApp(boolean unconditional) {}
public void commandAction(Command c,Displayable d1){
if(c==Exit){
destroyApp(true);notifyDestroyed();
}
if(c==Next){
if(cnt>=3){
if((L1.getSelectedIndex()+1)==4)qcnt=qcnt+1;if(qcnt==0)F1=new Form("Result:Fail");
elseF1=new Form("Result:Pass");
F1.append("Score is:"+qcnt+"");F1.addCommand(Exit);F1.setCommandListener(this);d.setCurrent(F1);}else{
if(cnt==0){L1=new List(quo[0],List.EXCLUSIVE,opt1,null);}else if(cnt==1){
if((L1.getSelectedIndex()+1)==2)qcnt++;
L1=new List(quo[1],List.EXCLUSIVE,opt2,null);}else{
if((L1.getSelectedIndex()+1)==3)qcnt++;
L1=new List (quo [2],List.EXCLUSIVE,opt3,null);}
L1.addCommand(Next);L1.setCommandListener(this);L1.addCommand(Exit);L1.setCommandListener(this);cnt=cnt+1; d.setCurrent(L1);
}}
}}
Output :
Write a Program in J2ME to design a calculator.
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class demo22 extends MIDlet implements CommandListener{ Form d1;
Alert a1;TextField t1,t2,t3;ChoiceGroup cg;Command c1,amdAlert;
public void startApp() { String[] op={"Add","Multiply","Divide","Subtract"};
cg=new ChoiceGroup("Operations",ChoiceGroup.POPUP,op,null);
d1=new Form("Form1"); t1=new TextField("Enter First Number","",10,TextField.NUMERIC); t2=new TextField("Enter Second Number","",10,TextField.NUMERIC); t3=new TextField("","",10,TextField.NUMERIC|TextField.UNEDITABLE); c1=new Command("OK",Command.SCREEN,0);
d1.append(t1);d1.append(t2);d1.append(t3);d1.append(cg);
d1.addCommand(c1);d1.setCommandListener(this);
Display.getDisplay(this).setCurrent(d1); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { notifyDestroyed(); } public void commandAction(Command c,Displayable d)
{int a=Integer.parseInt(t1.getString());int b=Integer.parseInt(t2.getString());if(c==c1){ switch(cg.getSelectedIndex()) {
case 0:if(t1.getString().equals("")||t2.getString().equals("")){a1=new Alert("Alarm","Field Empty",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);
}else if(b==0){a1=new Alert("Alarm","Divide By Zero
occurred",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);}else{
t3.setString(""+(a+b));} break;
case 1: if(t1.getString().equals("")||t2.getString().equals("")){a1=new Alert("Alarm","Field Empty",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);
}else if(b==0){a1=new Alert("Alarm","Divide By Zero
occurred",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);}else{
t3.setString(""+(a*b));} break;
case 2:
if(t1.getString().equals("")||t2.getString().equals("")){a1=new Alert("Alarm","Field Empty",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);
}else if(b==0){a1=new Alert("Alarm","Divide By Zero
occurred",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);}else{
t3.setString(""+(a/b));} break;
case 3: if(t1.getString().equals("")||t2.getString().equals("")){a1=new Alert("Alarm","Field Empty",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);
}else if(b==0){a1=new Alert("Alarm","Divide By Zero
occurred",null,AlertType.ALARM);Display.getDisplay(this).setCurrent(a1);}else{
t3.setString(""+(a-b));}
}}
}}
Develop a J2ME program for Calculator.
Program :
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class Calculator extends MIDlet implements CommandListener{
TextField T1,T2,T3;String[] str=new String[] {"+","-","/","*"};ChoiceGroup ch=new ChoiceGroup("Choose Operation",ChoiceGroup.EXCLUSIVE,str,null);
public Calculator(){}
public void startApp(){
T1=new TextField("First Number","",10,TextField.DECIMAL);T2=new TextField("Second Number","",10,TextField.DECIMAL);T3=new TextField("Result","",10,TextField.UNEDITABLE);
Form F=new Form("Calculator");Command c=new Command("Cal",Command.SCREEN,1);F.addCommand(c);F.setCommandListener(this);F.append(T1);F.append(T2);F.append(ch);F.append(T3);Display.getDisplay(this).setCurrent(F);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c,Displayable s){
double ans=0.0;double no1=Double.parseDouble(T1.getString());double no2=Double.parseDouble(T2.getString());
if(ch.getSelectedIndex()==0)ans=no1+no2;
else if(ch.getSelectedIndex()==1)ans=no1-no2;
else if(ch.getSelectedIndex()==2)ans=no1/no2;
elseans=no1*no2;
String res=Double.toString(ans);T3.setString(res);
}}
Output:
Develop a J2ME program for Calender.
Program :
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;
public class Calendar extends MIDlet{
public Calendar(){}
public void startApp(){
Form mainForm=new Form("Calendar");mainForm.append(new DateField("Date",DateField.DATE));Display.getDisplay(this).setCurrent(mainForm);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}}
Output :