j2me calculater example in netbeans IDE by R4R Team

j2me calculater example in netbeans:

Here we use the netbeans IDE to implement the calc program....see next ..

1)file---------->newproject------>javame--->mobile application-->next--- choose brouse-->finish.

than you see the hellomdiet.javafile ,this is bydefault file, now you change the code as you want..

see more given below...

calc1.java:

package hello;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

 

/**

* @alok sharma

*/

public class Calc1 extends MIDlet implements CommandListener

{

TextField inp;

Display display;

Command add,OK,Exit,sub,div,mul,History;

int result;

Ticker tk;

 

public Calc1()

{

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 ALOK SHARMA");

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("");

}

}

now you bulid and run the project....as

see below image.




output: now check  your calc.....



Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!