0% found this document useful (0 votes)
62 views

J 2 Meprax 2

The document provides code for two Java ME applications - a calculator application that allows users to perform basic math operations and displays the result, and a calendar application that shows the current date and time and allows the user to change the date in a date field. The code samples show how to create the user interfaces for these apps using forms, text fields, date fields, alerts and commands in Java ME.

Uploaded by

Marina Royan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

J 2 Meprax 2

The document provides code for two Java ME applications - a calculator application that allows users to perform basic math operations and displays the result, and a calendar application that shows the current date and time and allows the user to change the date in a date field. The code samples show how to create the user interfaces for these apps using forms, text fields, date fields, alerts and commands in Java ME.

Uploaded by

Marina Royan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICAL 3

AIM: Create a Calculator application in J2ME.

SOURCE CODE:

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

public class calculator extends MIDlet implements CommandListener


{
private StringItem sp,sm,smul,sd,se;
private TextField tf1,tf2,tf3;
private int n1,n2,res;
private Command exit,scmdp,scmdm,scmdmul,scmdd,scmde;
private Display display;
private Form form;

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);
}

public void pauseApp() { }


public void destroyApp(boolean unconditional) { }

public void commandAction(Command c, Displayable s)


{
if(c==exit)
notifyDestroyed();
}
}

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.*;

public class calendar extends MIDlet implements ItemStateListener,CommandListener


{
DateField df;
Display d;
Form frm;
Command cmExit;
Alert af;
Date dm;

public void startApp()


{
frm = new Form("Calendar");
cmExit = new Command("Exit", Command.EXIT, 0);

// DateField with todays date as a default


df = new DateField("Set Date and Time",DateField.DATE_TIME);
df.setDate(new Date());
frm.append(df);
frm.addCommand(cmExit);
frm.setCommandListener(this);
frm.setItemStateListener(this);
d = Display.getDisplay(this);
d.setCurrent(frm);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {


notifyDestroyed();
}

public void itemStateChanged(Item item)


{
dm=df.getDate();
String str= dm.toString();
//if(item.getLabel().equals("Set Date and Time"))
//{
af=new Alert("Date field Changed",str,null,AlertType.INFO);
System.out.println("Date field changed.");
d.setCurrent(af,frm);
//}
}

public void commandAction(Command c, Displayable s)


{
if (c == cmExit)
notifyDestroyed();
}
}

OUTPUT:

You might also like