Dr.N.G.P INSTITUTE OF TECHNOLOGY
Coimbatore-641 048
Department of Information Technology
(B.Tech Information Technology)
LAB MANUAL
MOBILE COMPUTING LABORATORY
ACTIVITY
NAME OF THE FACULTY
DESIGNATION
PREPARED BY
MANI DEEPAK CHOUDHRY
Lecturer
APPROVED BY
V.VANI
Professor
1
SIGNATURE
CONTENTS
Ex. No
Name of the Experiment
1.
Study of WML and J2ME simulators
2.
Design of simple Calculator having +, * and / using
WML/J2ME
3.
Design of Calendar for any given month and year using
WML/J2ME
Page No
3
7
16
4.
Design a Timer to System Time using WML/J2ME
22
5.
Design of simple game using WML/J2ME
27
6.
Animate an image using WML
37
7.
Design a personal phone book containing the name,
phone no., address, e-mail etc.
8.
Simulation of Authentication and encryption technique
used in GSM
9.
Browsing the Internet using Mobile phone simulator
10.
Study of GlomoSim Simulator
2
39
Ex. No: 1
STUDY OF WML AND J2ME SIMULATORS
AIM:
To study WML and J2ME simulators.
Wireless Markup Language (WML)
WML is a markup language that is based on XML (eXtensible Markup Language). The
official WML specification is developed and maintained by the WAP Forum, an industry-wide
consortium founded by Nokia, Phone.com, Motorola, and Ericsson. This specification defines the
syntax, variables, and elements used in a valid WML file.
WML TAGS
WAP homepages are not very different from HTML homepages. The markup language used
for WAP is WML (Wireless Markup Language). WML uses tags - just like HTML - but the syntax
is stricter and conforms to the XML 1.0 standard. WML pages have the extension *.WML, just like
HTML pages have the extension *.HTML. WML is mostly about text. Tags that would slow down
the communication with handheld devices are not a part of the WML standard. The use of tables
and images is strongly restricted. Since WML is an XML application, all tags are case sensitive
(<wml> is not the same as </WML>), and all tags must be properly closed.
Element Tag
As WML is a language derived from XML, each element has an opening and a closing tag;
if no actual content exists, an abbreviation can be used.WML is therefore case sensitive, tags have
to be given in lowercase letters.
<tag>Content</tag>
<tag/>
<tag attribute="value1">Content</tag>
<tag attribute="value"/>
Meta Tag
Proliferate information to search engines and are surrounded by a <head> tag. These are
located at the head of the file after the prolog and the <wml> tag.
<head>
<meta name="keywords" content="keyword"/>
3
<meta name="description" content="a description"/>
<meta name="author" content="Thomas Ziegler"/>
</head>
Links
Links may be given in two ways, but the <a> tag is preferred.
<anchor>Go to URL<go href="url.wml"/></anchor>
<anchor>Go to URL<go href="http://wml.org.de/url.wml"/></anchor>
<a accesskey="1" href="#card">Go to card</a>
<a href="http://wml.org/deck.wml/#card">Go to card</a>
CDATA
With a CDATA section text can be displayed without interpretation of tags and special
characters.
<![CDATA[Dies ist ein langer, unformatierter Text, der <nicht> geparst wird, daher sind
auch "$%&" Zeichen erlaubt]]>
IMAGES
The WBMP format (Wireless Bitmap) is used to code images under WML. Nokia has a
DOS based conversion utility.
<img src="bild.wbmp" alt="Text">
src: URL des Bildes
alt: Alternative text (mandatory)
align: "top", "middle", "bottom" (alignment)
INPUT
<input> generates a text entry field and validates the entry against a format specification.
The value is returned in a Variable.
<input name="Name" value="Template"/>
name: Name of the receiving Variable
Value: Template
Type: "text", "password"
4
Format: Mask (e.g. A uppercase letters, N numbers)
Maxlength: Maximum length of entry field
Title: Title
A <fieldset> groups input tags logically.
J2ME
J2ME stands for Java 2, Micro Edition. It is a stripped-down version of Java targeted at devices
which have limited processing power and storage capabilities and intermittent or fairly lowbandwidth network connections. These include mobile phones, pagers, wireless devices and set-top
boxes among others.
A Sample Wireless Stack would consist of:
i. Profiles
ii. Configurations
iii. Java Virtual Machines
iv. Host Operating System
What is a J2ME Configuration?
A configuration defines the minimum Java technology that an application developer can
expect on a broad range of implementing devices.
J2ME Connected, Limited Device Configuration (CLDC)
i. Specifies the Java environment for mobile phone, pager and wireless devices
ii. CLDC devices are usually wireless
iii. 160 - 512k of memory available for Java
iv. Typically has limited power or battery operated
v. Network connectivity, often wireless, intermittent, low-bandwidth (9600bps or less)
5
J2ME Connected Device Configuration (CDC)
i. Describes the Java environment for digital television set-top boxes, high end wireless
devices and automotive telematics systems.
ii. Device is powered by a 32-bit processor
iii. 2MB or more of total memory available for Java
iv. Network connectivity, often wireless, intermittent, low-bandwidth (9600bps or less)
These two configurations differ only in their respective memory and display capabilities.
What is a J2ME Profile?
A specification layer above the configuration which describes the Java configuration for a
specific vertical market or device type.
J2ME Profiles
J2ME Mobile Information Device Profile (MIDP)
i. This is the application environment for wireless devices based on the CLDC
ii. Contains classes for user interface, storage and networking
J2ME Foundation Profile, Personal Basis, Personal and RMI profiles
1) these are profiles for devices based on the CDC, which are not addressed in this tutorial
Virtual Machines
The CLDC and the CDC each require their own virtual machine because of their different
memory and display capabilities. The CLDC virtual machine is far smaller than that required by the
CDC and supports less features. The virtual machine for the CLDC is called the Kilo Virtual
Machine (KVM) and the virtual machine for the CDC is called the CVM.
RESULT:
Thus the study is made on WML and J2ME.
6
Ex. No: 2
Design of simple Calculator having +, * and / using WML / J2ME
WML PROGRAM
AIM
To design a simple calculator using WML Script & WML Deck.
ALGORITHM
calc.wml
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML & Template to be displayed in each card.
Step 3: Call init () when deck is first entered using init card.
Step 4: Using display card display the values & operators by calling changeop () & compute using
domath ().
Step 5: Using num1 & num2 card get 2 input values.
Step 6: Using result card display the result of computation. Press Compile & then Show Button.
Step 7: Stop the program.
calc.wmls
Step 1: Start the script.
Step 2: When init () called from WML Deck perform the initialization of all variables, after
initializing go back to deck program.
Step 3: When changeop () is called from deck program perform change of operators & then return
back to deck program.
Step 4: When domath () is called from deck program perform the computation of values based on
the operators.
Step 5: Call back the result card in Deck program.
7
PROGRAM
Calc.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML
1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<!-- Call init routine when deck is first entered,
or when card is called -->
<card id="init">
<onevent type="onenterforward">
<go href="calc.wmls#init()"/>
</onevent>
<p>
Initializing...
</p>
</card>
<!-- Display current values and operator -->
<card id="display">
<p>
<select>
<option onpick="#num1">$(num1).$(dec1)</option>
<option onpick="calc.wmls#changeop()">$(operator)</option>
<option onpick="#num2">$(num2).$(dec2)</option>
<option onpick="calc.wmls#domath()">Compute</option>
</select>
</p>
</card>
<!-- Accept input for value 1 -->
<card id="num1">
<do type="accept">
<go href="#display"/>
</do>
<!-- Offer easy way to clear values
(options key) -->
8
<do type="options" label="clear">
<refresh>
<setvar name="num1" value=""/>
<setvar name="dec1" value=""/>
</refresh>
</do>
<p>
Enter the value:
<input name="num1" maxlength="8" format="*N"/><br/>.<br/>
<input name="dec1" maxlength="3" format="*N"/>
</p>
</card>
<!-- Accept input for value 2 -->
<card id="num2">
<do type="accept">
<go href="#display"/>
</do>
<!-- Offer easy way to clear values
(options key) -->
<do type="options" label="clear">
<refresh>
<setvar name="num2" value=""/>
<setvar name="dec2" value=""/>
</refresh>
</do>
<p>
Enter the value:
<input name="num2" maxlength="8" format="*N"/><br/>
<br/>
<input name="dec2" maxlength="3" format="*N"/>
</p>
</card>
<!-- Display result of calculation -->
<card id="result">
<do type="accept">
9
<go href="#init"/>
</do>
<p>
The result of <br/>
$(num1) $(operator) $(num2) is:<br/>
$(result)
</p>
</card>
</wml>
Calc.wmls
// Initialize variables
extern function init() {
WMLBrowser.setVar("num1","");
WMLBrowser.setVar("dec1","");
WMLBrowser.setVar("num2","");
WMLBrowser.setVar("dec2","");
WMLBrowser.setVar("operator","+");
WMLBrowser.setVar("result",0);
WMLBrowser.go("calc.wml#display");
}
// Rotate operator through + - * /
extern function changeop() {
var operator = WMLBrowser.getVar("operator");
if (operator == "+") {
WMLBrowser.setVar("operator","-");
}
if (operator == "-") {
WMLBrowser.setVar("operator","x");
}
if (operator == "x") {
WMLBrowser.setVar("operator","/");
}
if (operator == "/") {
WMLBrowser.setVar("operator","+");
10
}
WMLBrowser.go("calc.wml#display");
}
// Do the operation specified
extern function domath() {
var operator = WMLBrowser.getVar("operator");
var num1 = WMLBrowser.getVar("num1");
var dec1 = WMLBrowser.getVar("dec1");
var num2 = WMLBrowser.getVar("num2");
var dec2 = WMLBrowser.getVar("dec2");
var result = 0;
// Zero values if nothing was entered
if (String.isEmpty(num1)) { num1 = 0; }
if (String.isEmpty(dec1)) { dec1 = 0; }
if (String.isEmpty(num2)) { num2 = 0; }
if (String.isEmpty(dec2)) { dec2 = 0; }
// Assemble full numbers from whole values
// and decimals
num1 = num1 + "." + dec1;
num2 = num2 + "." + dec2;
// Do calculation, depending on what operator
// was selected
if (operator == "+") {
result = Lang.parseFloat(num1) + Lang.parseFloat(num2);
}
if (operator == "-") {
result = num1 - num2;
}
if (operator == "x") {
result = num1 * num2;
}
if (operator == "/") {
result = num1 / num2;
}
11
WMLBrowser.setVar("result", result);
WMLBrowser.setVar("num2",num2);
WMLBrowser.setVar("num1",num1);
WMLBrowser.go("calc.wml#result");
}
RESULT
Thus a simple calculator is designed using WML deck & WML Script & output is verified
successfully.
12
J2ME PROGRAM
AIM
To design a simple calculator using J2ME.
ALGORITHM
Step 1: Start Java ME SDK tool kit.
Step 2: File – New Project – Java ME SDK – MIDP Application – Next – finish.
Step 3: Right click project name – New – Midlet – Change the class name according to your
program.
Step 4: Declare 3 variable for calculation & 4 command variables.
Step 5: In constructor define the text fields and priority for commands.
Step6: In startApp() assign each command to CommandListner interface so as to call each
command.
Step 7: In commandAction () based on the calculator command perform the calculation.
Step 8: If Exit is clicked destroyApp() by calling the notifydestroyed().
Step 9: After typing the program RC Project – Build then RC Project – Run.
PROGRAM
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);
13
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);
}
14
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();
}
}
}
RESULT
Thus a simple calculator is designed using J2ME & output is verified successfully.
15
Ex. No: 3
Design of simple Calendar using WML / J2ME
WML PROGRAM
AIM
To design a simple calendar using WML Deck.
ALGORITHM
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML & Template to be displayed in each card.
Step 3: Define each card using table with 7 columns.
Step 4: After finishing the program press Compile & Show button.
Step 5: Stop the Program.
PROGRAM
Calendar.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<template>
<do type="prev"><prev/></do>
</template>
<card id ="card1" title="JAN">
<do type="unknown" label="Next"><go href="#card2"/></do>
<p>
<table columns="7" align="LCC">
<tr>
<td>su</td><td>mo</td><td>tu</td><td>we</td><td>th</td><td>fr</td><td>sa</td>
</tr>
<tr>
<td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td>
</tr>
<tr>
16
<td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td>
</tr>
<tr>
<td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td>
</tr>
<tr>
<td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td>
</tr>
<tr>
<td>29</td><td>30</td><td>31</td><td></td><td></td><td></td><td></td>
</tr>
</table>
</p>
</card>
<card id ="card2" title="FEB">
<do type="unknown" label="Next"><go href="#card3"/></do>
<p>
<table columns="7" align="LCC">
<tr>
<td>su</td><td>mo</td><td>tu</td><td>we</td><td>th</td><td>fr</td><td>sa</td>
</tr>
<tr>
<td></td><td></td><td></td><td>1</td> <td>2</td><td>3</td><td>4</td>
</tr>
<tr>
<td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td>
</tr>
<tr>
<td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td>
</tr>
<tr>
<td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td>
</tr>
<tr>
<td>26</td><td>27</td><td>28</td><td></td><td></td><td></td><td></td>
17
</tr>
</table>
</p>
</card>
<card id ="card3" title="MAR">
<do type="unknown" label="Next"><go href="#card4"/></do>
<p>
<table columns="7" align="LCC">
<tr>
<td>su</td><td>mo</td><td>tu</td><td>we</td><td>th</td><td>fr</td><td>sa</td>
</tr>
<tr>
<td></td><td></td><td></td><td>1</td><td>2</td><td>3</td><td>4</td>
</tr>
<tr>
<td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td>
</tr>
<tr>
<td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td>
</tr>
<tr>
<td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td>
</tr>
<tr>
<td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td><td></td>
</tr>
</table>
</p>
</card>
<card id ="card4" title="APR">
<do type="unknown" label="Next"><go href="#card1"/></do>
<p>
<table columns="7" align="LCC">
<tr>
<td>su</td><td>mo</td><td>tu</td><td>we</td><td>th</td><td>fr</td><td>sa</td>
18
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td>1</td>
</tr>
<tr>
<td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td>
</tr>
<tr>
<td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td>
</tr>
<tr>
<td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td>
</tr>
<tr>
<td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td>
</tr>
</table>
</p>
</card>
</wml>
RESULT
Thus a simple calendar is designed using WML Deck & output is verified successfully.
19
J2ME PROGRAM
AIM
To design a simple calendar using J2ME.
ALGORITHM
Step 1: Start Java ME SDK tool kit.
Step 2: File – New Project – Java ME SDK – MIDP Application – Next – finish.
Step 3: Right click project name – New – Midlet – Change the class name according to your
program.
Step 4: Create Form using Form class, date using Date class, DateField using DateField class & exit
command using command class.
Step 5: Using Constructor initialize all this.
Step 6: In startApp() set current form as created form.
Step 7: If exit is clicked call notifydestroyed().
Step 8: After typing the program RC Project – Build then RC Project – Run.
PROGRAM
import java.util.Date;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
public class DateToday extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("Today's Date");
private Date today = new Date(System.currentTimeMillis());
20
private Command exit = new Command("Exit", Command.EXIT, 1);
private DateField datefield = new DateField("", DateField.DATE_TIME);
public DateToday() {
display = Display.getDisplay(this);
datefield.setDate(today);
form.append(datefield);
form.addCommand(exit);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable displayable) {
if (command == exit) {
destroyApp(false);
notifyDestroyed();
}
}
}
RESULT
Thus a simple calendar was designed using J2ME & output is verified successfully.
21
Ex. No: 4
Design a Timer to System Time using WML/J2ME
WML PROGRAM
AIM
To design Timer to System using WML Deck.
ALGORITHM
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML.
Step 3: Using ontimer & timer attribute design 3 cards to implement timer.
Step 4: Compile & run the program.
Step 5: Stop the program.
PROGRAM
timer.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" ontimer="#card2" title="Toolkit Demo">
<timer value="50"/>
<p align="center">
<br/> <br/> <br/>
<big>
Welcome to ...
</big>
</p>
</card>
<card id="card2" ontimer="#card3" title="Toolkit Demo">
22
<timer value="50"/>
<p align="center">
<br/> <br/>
<b>
The Nokia<br/>
</b>
<u>
WAP
</u>
...
</p>
</card>
card id="card3" ontimer="#card1" title="Toolkit Demo">
<timer value="50"/>
<p align="center">
<br/> <br/> <br/>
<big>
<i>
Toolkit!
</i>
</big>
</p>
</card>
</wml>
RESULT
Thus timer was designed using WML Deck & output is verified successfully.
23
J2ME PROGRAM
AIM
To design timer to system using J2ME.
ALGORITHM
Step 1: Start Java ME SDK tool kit.
Step 2: File – New Project – Java ME SDK – MIDP Application – Next – finish.
Step 3: Right click project name – New – Midlet – Change the class name according to your
program.
Step 4: Create form & declare timer test variables & commands.
Step 5: In constructor add all commands to form and initialize them.
Step 6: In startApp () set the current element to be form created.
Step 7: If exit is clicked call notifydestroyed ().
Step 8: Build & run the program.
Step 9: Stop the program.
PROGRAM
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TimerTemplate extends MIDlet implements CommandListener
{
private Display display;
// Our display
private Form fmMain;
// Main form
private Command cmExit;
// Exit midlet
private Command cmStop;
// Stop the timer
private Timer tm;
// Timer
24
private TestTimerTask tt;
private int count = 0;
// Task
// How many times has task run
public TimerTemplate()
{
display = Display.getDisplay(this);
fmMain = new Form("Timer Test");
fmMain.append("waiting...\n");
cmExit = new Command("Exit", Command.EXIT, 1);
cmStop= new Command("Stop", Command.STOP, 2);
fmMain.addCommand(cmExit);
fmMain.addCommand(cmStop);
fmMain.setCommandListener(this);
tm = new Timer();
tt = new TestTimerTask();
tm.schedule(tt,5000);
}
public void startApp ()
{
display.setCurrent(fmMain);
}
public void destroyApp (boolean unconditional)
{
}
25
public void pauseApp ()
{
}
public void commandAction(Command c, Displayable d)
{
if (c == cmStop)
{
tm.cancel();
}
else if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
}
private class TestTimerTask extends TimerTask
{
public final void run()
{
fmMain.append("run count: " + ++count + "\n");
}
}
}
RESULT Thus a simple timer was designed using J2ME & output is verified successfully.
26
Ex.No 5
Design of Simple Game Using WML/J2ME
WML PROGRAM
AIM
To design a simple calculator using WML Script & WML Deck.
ALGORITHM
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML.
Step 3: Call init () in wmls program from WML program using card to initialize all variables used.
Step 4: Create a card status & call guess card using go href tag.
Step 5: In guess card call guess () defined in wmls program which check whether the given
character is correct or not if correct display the letter wherever it occurs in the word with the
guessed character & blank the text field.
Step 6: If the guessed letter is incorrect the display a man & blank the text field.
Step 7: If the man is hanged then call the hang card in wml program & display a message.
Step 8: If the word is filled then call win card in wml program & display a message.
Step 9: Build & run the program.
Step 10: Stop the program.
PROGRAM
hang1.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="init" title="Hangman">
<onevent type="onenterforward">
<go href="hang1.wmls#init()"/>
</onevent>
<p>
27
Initializing...
</p>
</card>
<card id="status" title="Hangman">
<do type="accept" label="Guess">
<go href="#guess" />
</do>
<p>
|<br/>
$man<br/><br/>
$blank
</p>
</card>
<card id="guess" title="Hangman">
<do type="accept">
<go href="hang1.wmls#guess()" />
</do>
<p>
$blank<br/>
Guess: <input name="guess" maxlength="1" format="a" />
</p>
</card>
<card id="hung" title="Hangman">
<do type="accept" label="Restart">
<go href="#init" />
</do>
<p>
You've been hanged!<br/><br/>
Word was:<br/>
$word<br/>
Your guess:<br/>
$blank
</p>
</card>
<card id="win" title="Hangman">
28
<do type="accept" label="Restart">
<go href="#init" />
</do>
<p>
You win!<br/><br/>
Word was:<br/>
$word<br/>
</p>
</card>
</wml>
hang1.wmls
extern function init()
{
var words = "animal, announce, banana, doctor, elephant, giraffe";
var idx,x,hang = 0;
var blank,word,man = "";
// Pick a random word from list
idx = Lang.random(6);
word = String.elementAt(words,idx," ");
// Build a blank string (letters all "*") that
// is the same length as our word
for (x = 1; x <= String.length(word); x++ )
{
blank = blank + "*";
}
// Pass all values to browser
WMLBrowser.setVar("word",word);
WMLBrowser.setVar("blank",blank);
WMLBrowser.setVar("hang",hang);
WMLBrowser.setVar("man",man);
// Display status card
WMLBrowser.go("hang1.wml#status");
}
29
// Evaluate current guess
extern function guess()
{
// Init vars
var x = 0;
var temp = "";
var correct = false;
var man = "";
// Pieces for the hanging man,comma delimited
var manpieces = " , 0,-,|,-,/, ";
// Get current values
var word = WMLBrowser.getVar("word");
var hang = WMLBrowser.getVar("hang");
var blank = WMLBrowser.getVar("blank");
var guess = WMLBrowser.getVar("guess");
// Walk one character at a time through word
// If guess = character, reveal character
// If guess != character, keep current value
// (revealed character or blank)
// Also, set "correct" if at least one char found
for (x = 0; x <= String.length(word); x++ )
{
if ( String.subString(word,x,1) == guess )
{
temp = temp + guess;
correct = true;
}
else
{
temp = temp + String.subString(blank,x,1);
}
}
// If letter wasn't found, add one to hanging counter
if (! correct)
{ hang++; }
30
// Build our hanging man
if (hang > 0)
{
for (x = 1; x <= hang; x++ )
{
man = man + String.elementAt(manpieces,x,",");
}
}
// Blank the guess so <input> is blank
guess = "";
// Pass current values to browser
WMLBrowser.setVar("blank",temp);
WMLBrowser.setVar("guess",guess);
WMLBrowser.setVar("hang",hang);
WMLBrowser.setVar("man",man);
// Determine whether player has won (no more // "*" in blank), has lost (6 pieces of man //
displayed, or keep playing (else).
if ( String.find(temp,"*") == -1 )
{
WMLBrowser.go("hang1.wml#win");
}
else
{
if (hang >= 6)
{
WMLBrowser.go("hang1.wml#hung");
}
else
{
WMLBrowser.go("hang1.wml#status");
}
}
}
RESULT – Thus a simple game was designed using WML Script & Deck & output is verified
successfully.
31
J2ME PROGRAM
AIM
To design a simple game using J2ME.
ALGORITHM
Step 1: Start Java ME SDK tool kit.
Step 2: File – New Project – Java ME SDK – MIDP Application – Next – finish.
Step 3: Right click project name – New – Midlet – Change the class name according to your
program.
Step 4: In Hangman class declare all required commands, variables & using constructor append all
the fields to the form created.
Step 5: In startApp () set the current form to be form created & randomly select the word from the
list to start the game.
Step 6: Now initialize the count to keep track of guesses to 0 & flag to whether the character is
found or not.
Step 7: In commandAction () if the command clicked is ok then check the guessed word with word
already selected for game.
Step 8: If true the replace the character in the word field using replaceCharAt () & blank the
guessed field then set flag to 1.
Step 9: If the character is not found then set flag to 0.
Step 10: If flag is 0 then increment count value & if count reaches 5 display a message you lost else
if all letters are guessed the display you win.
Step 11: Build & run the program.
Step 12: Stop the program.
PROGRAM
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
32
public class Hangman extends MIDlet implements CommandListener
{
private Display display;
private Form form;
private Command ok,exit;
private TextField w,g;
private String str="";
private int num;
private String res="";
private Alert alert;
private String[]
words={"idiot","human","boy","girl","weight","width","height","college","school","buy","s
ell"};
public Hangman()
{
display=Display.getDisplay(this);
form=new Form("Hangman Game");
ok=new Command("OK",Command.OK,0);
exit=new Command("Exit",Command.EXIT,1);
w=new TextField("Word is:","",30,TextField.ANY);
g=new TextField("Guessed:","",30,TextField.ANY);
form.append(w);
form.append(g);
form.addCommand(ok);
33
form.addCommand(exit);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
Random rand = new Random();
num = rand.nextInt(10);
for(int i=0;i<words[num].length();i++)
{
str=str+"*";
}
w.setString(str);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
int count=0;
public void commandAction(Command c, Displayable d)
{
int flag=0;
if(c==ok)
{
res=w.getString();
String gch=g.getString();
34
for(int i=0;i<str.length();i++)
{
if(words[num].charAt(i)==Character.toLowerCase(gch.charAt(0)))
{
res=replaceCharAt(res,i,gch.charAt(0));
flag=1;
}
if(res.equalsIgnoreCase(words[num]))
{
alert=new Alert("Congrats","You Won",null,AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,form);
}
}
if(flag==0)
count++;
if(count==5)
{
alert=new Alert("Hang Man","You Lost",null,AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,form);
}
w.setString(res);
g.setString("");
35
}
}
public static String replaceCharAt(String s, int pos, char c)
{
StringBuffer buf = new StringBuffer( s );
buf.setCharAt( pos, c );
return buf.toString( );
}
}
RESULT
Thus a simple game was designed using J2ME & output is verified successfully.
36
Ex.No : 6
Animate an Image using WML
AIM
To animate an image using WML Deck.
ALGORITHM
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML.
Step 3: Define 3 cards with 3 images which are displayed in card using img tag.
Step 4: Define Timer value using timer tag & when the time is reached call another card.
Step 5: Compile & run the program.
Step 6: Stop the program.
PROGRAM
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="anime1" ontimer="#card2">
<timer value="50"/>
<p align="center">
<img src="sunny.wbmp" alt="" width="50" height="50"/>
</p>
<do name="accept" type="accept">
<go href="#card2"/>
</do>
</card>
<card id="card2" title="anime2" ontimer="#card1">
<timer value="50"/>
<p align="center">
<img src="rainy.wbmp" alt="" width="50" height="50"/>
</p>
<do name="accept" type="accept">
37
<go href="#card1"/>
</do>
</card>
</wml>
RESULT
Thus animating an image is done through WML Deck & output is verified successfully.
38
Ex. No: 7
Design a personal phone book containing the name, phone no,
Address, e-mail using WML & J2ME
AIM
To design a Phonebook using WML deck.
Link.wml
ALGORITHM
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML.
Step 3: Design 3 cards with name, phone no, address & e-mail id & for navigation use #card name
in the go href tag.
Step 4: Stop the program.
phonebook.wml
ALGORITHM
Step 1: Start the WAP Tool Kit.
Step 2: Include the DTD for WML.
Step 3: Design a card displaying all contacts by calling the each card in link.wml file.
Step 4: Compile & run the program.
Step 5: Stop the program.
PROGRAM
link.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<template>
<do type="prev"><prev/></do>
39
</template>
<card id="card1" title="phone book">
<do type="unknown" label="next"><go href="#card2"/></do>
<p align="left">
<small><b>Name:</b></small><br/>
x<br/>
<small><b>Phone no:</b></small><br/>
1111111111<br/>
<small><b>Location:</b></small><br/>
chennai<br/>
<small><b>E-mail:</b></small><br/>
[email protected]
</p>
</card>
<card id="card2" title="phone book">
<do type="unknown" label="next"><go href="#card3"/></do>
<p align="left">
<small><b>Name:y</b></small><br/>
y<br/>
<small><b>Phone no:</b></small><br/>
22222222<br/>
<small><b>Location:</b></small><br/>
cbe<br/>
<small><b>E-mail:</b></small><br/>
[email protected]
</p>
</card>
<card id="card3" title="phone book">
<do type="unknown" label="next"><go href="#card1"/></do>
<p align="left">
<small><b>Name:</b></small><br/>
a<br/>
<small><b>Phone no:</b></small><br/>
3333333333<br/>
<small><b>Location:</b></small><br/>
40
banglore<br/>
<small><b>E-mail:</b></small><br/>
[email protected]
</p>
</card>
</wml>
phonebook.wml
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="main" title="Directory">
<p>
<anchor>Contact 1
<go href="link.wml#card1"></go>
</anchor><br/>
<anchor>Contact 2
<go href="link.wml#card2"></go>
</anchor><br/>
<anchor>Contact 3
<go href="link.wml#card3"></go>
</anchor><br/>
</p>
</card>
</wml>
RESULT
Thus phone book was generated using WML & output is verified successfully.
41
J2ME PROGRAM
ALGORITHM
Step 1: Start Java ME SDK tool kit.
Step 2: File – New Project – Java ME SDK – MIDP Application – Next – finish.
Step 3: Right click project name – New – Midlet – Change the class name according to your
program.
Step 4: In PhoneBook class declare all the required variables, commands & using constructor
initialize all commands & textboxes.
Step 5: Using startApp () append all command by using CommandListner Interface.
Step 6: In commandAction () check if label is exit or not if exit display the output in output window
& come out of the application.
Step 7: If the label is next then get number.
Step 8: If the label is new then display the output in output window & reset the fields.
Step 9: Compile & run the program.
Step 10: Stop the program.
PROGRAM
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PhoneBook extends MIDlet implements CommandListener
{
private Command exit, next, New;
private TextBox name, number;
private Display display;
public PhoneBook()
{
42
next = new Command("Next", Command.SCREEN, 2);
exit = new Command("Exit", Command.SCREEN, 2);
New = new Command("New", Command.SCREEN, 2);
name = new TextBox("Enter Name", "", 30, TextField.ANY);
number = new TextBox("Enter Number", "", 13, TextField.PHONENUMBER);
}
public void startApp()
{
display = Display.getDisplay(this);
name.addCommand(next);
name.setCommandListener(this);
number.addCommand(New);
number.setCommandListener(this);
number.addCommand(exit);
number.setCommandListener(this);
display.setCurrent(name);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command c, Displayable s)
{
43
String label = c.getLabel();
if (label.equals("Exit"))
{
System.out.println("Name = " + name.getString() + ",Number = "+
number.getString());
destroyApp(false);
}
else if (label.equals("Next"))
{
number.setString("");
display.setCurrent(number);
}
else if (label.equals("New"))
{
display.setCurrent(name);
System.out.println("Name = " + name.getString() + ",Number = "+
number.getString());
name.setString("");
}
}
}
RESULT
Thus a simple phonebook was generated using J2ME & output is verified successfully.
44
© Copyright 2026 Paperzz