Tuesday, May 25, 2010

Hyeee frndz m back after ma exms.......
In this demo I'll show how easily you can create your own calculator application for your mobile phone !
Juss open NetBeans IDE create a new project named as CALCULATOR
Now add a new midlet named calc
Full coding for the above project as follows........

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


/**
* @author Krishan
*/
public class Calc extends MIDlet implements CommandListener
{

TextField inp;
Display display;
Command add,OK,Exit,sub,div,mul,History;
int result;
Ticker tk;


public Calc()
{

tk=new Ticker("Start");
History=new Command("History", Command.OK, 6);
inp = new TextField("Input", "", 10, TextField.NUMERIC);
add = new Command("ADD", Command.OK, 1);
div=new Command("Divide", Command.OK, 3);
mul=new Command("Multiply", Command.OK, 5);
sub=new Command("Subtract", Command.OK, 2);
OK = new Command("OK", Command.STOP, 4);
Exit=new Command("EXIT", Command.EXIT, 7);

}

public void startApp()
{
display =Display.getDisplay(this);
Form f1=new Form("CALCULATOR BY KRISHAN");
f1.setTicker(tk);
f1.append(inp);
f1.addCommand(History);
f1.addCommand(add);
f1.addCommand(OK);
f1.addCommand(Exit);
f1.addCommand(sub);
f1.addCommand(div);
f1.addCommand(mul);
f1.setCommandListener(this);
display.setCurrent(f1);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}

int kpress=0;
void ad()
{
kpress=0;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"+"+inp.getString());
String input=inp.getString();
result+=Integer.parseInt(input);
}
}

void sb()
{
kpress=1;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"-"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result-=Integer.parseInt(input);
}

}

void ml()
{
kpress=2;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"x"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result*=Integer.parseInt(input);
}

}

void dv()
{
try
{
kpress=3;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"/"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result/=Integer.parseInt(input);
}
}
catch(Exception ex)
{
result=0;
}
}

public void commandAction(Command c,Displayable d)
{
if(c==add)
ad();
if(c==sub)
sb();
if(c==mul)
ml();
if(c==div)
dv();
if(c==History)
{
Alert nalt=new Alert("History",tk.getString(),null,AlertType.INFO);
nalt.setTimeout(Alert.FOREVER);
display.setCurrent(nalt);
}
if(c==OK)
{
try
{
if(kpress==0)
ad();
else if(kpress==1)
sb();
else if(kpress==2)
ml();
else if(kpress==3)
dv();
else{}

}
catch(Exception ex)
{}
finally
{
tk.setString(tk.getString()+"="+result);
Alert res=new Alert("Result","RESULT = "+result, null, AlertType.INFO);
res.setTimeout(5000);
display.setCurrent(res);
}
}
if(c==Exit)
destroyApp(true);
inp.setString("");
}
}



Hope this will help you implementing command,TextField, Ticker and some more controls in J2ME
If anyone want any explaination den reply me.............

Download NetBeans Project

7 comments:

coolguy said...

how to install this calculator application in mobile? pls reply me

Anonymous said...

ya tell me what is the process to start this program i mean we have to download J2me software, and then do it and after that how to send to mobile

Anonymous said...

convert the command to button

Anonymous said...

How to pack it to .jar plz

Krishan Gahlot said...

You need netbeans IDE to built this program.
After successful built IDE itself generates .jar file in your project folder. Just copy and paste .jar file in your mobile and start using it :-)

cetin arslan said...

yard─▒m l├╝tfennnnnnnnnnnnnnnnnnnnnnn

Anonymous said...

not working

Post a Comment