J 2 Meprax 2
J 2 Meprax 2
SOURCE CODE:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public calculator()
{
display = Display.getDisplay(this);
tf1 = new TextField("Enter 1st number","",20,TextField.DECIMAL);
tf2 = new TextField("Enter 2nd number","",20,TextField.DECIMAL);
tf3 = new TextField("Answer : ","",20,TextField.ANY);
sp= new StringItem("+","",Item.BUTTON);
sm= new StringItem("-","",Item.BUTTON);
smul= new StringItem("*","",Item.BUTTON);
sd= new StringItem("/","",Item.BUTTON);
se= new StringItem("=","",Item.BUTTON);
exit = new Command("Exit", Command.EXIT, 0);
scmdp = new Command("+", Command.ITEM,1);
scmdm = new Command("-", Command.ITEM,1);
scmdmul = new Command("*", Command.ITEM,1);
scmdd = new Command("/", Command.ITEM,1);
form = new Form("String example");
form.append(tf1);
form.append(tf2);
form.append(sp);
form.append(sm);
form.append(smul);
form.append(sd);
form.append(tf3);
form.addCommand(exit);
sp.setDefaultCommand(scmdp);
sm.setDefaultCommand(scmdm);
smul.setDefaultCommand(scmdmul);
sd.setDefaultCommand(scmdd);
sd.setItemCommandListener(new ItemCommandListener()
{
public void commandAction(Command c, Item i)
{
if(i.getLabel().equals("/"))
{
n1 = Integer.parseInt(tf1.getString());
n2 = Integer.parseInt(tf2.getString());
res = n1/n2;
String sx = ""+res;
tf3.setString(sx);
}
}});
smul.setItemCommandListener(new ItemCommandListener()
{
public void commandAction(Command c, Item i)
{
if(i.getLabel().equals("*"))
{
n1 = Integer.parseInt(tf1.getString());
n2 = Integer.parseInt(tf2.getString());
res = n1*n2;
String sx = ""+res;
tf3.setString (sx);
}
}});
sm.setItemCommandListener(new ItemCommandListener()
{
public void commandAction(Command c, Item i)
{
if(i.getLabel().equals("-"))
{
n1 = Integer.parseInt(tf1.getString());
n2 = Integer.parseInt(tf2.getString());
res = n1-n2;
String sx = ""+res;
tf3.setString(sx);
}
}
});
sp.setItemCommandListener(new ItemCommandListener()
{
public void commandAction(Command c, Item i)
{
if(i.getLabel().equals("+"))
{
n1 = Integer.parseInt(tf1.getString());
n2 = Integer.parseInt(tf2.getString());
res = n1+n2;
String sx = ""+res;
tf3.setString(sx);
}
}
});
}//end constructor
public void startApp()
{
form.setCommandListener(this);
display.setCurrent(form);
}
OUTPUT
PRACTICAL 4
AIM: Create a calendar application in J2ME which shows the current date and time.
SOURCE CODE:
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
OUTPUT: