You are on page 1of 5

Femin Dharamshi BE3-26

Batch B

BASIC CALCULATOR
Source Code:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
public class Calculator extends MIDlet implements CommandListener
{
private Form form1;
private TextField n1,n2;
private Display display;
private Command add=new Command("Addition",Command.SCREEN,0);
private Command sub=new Command("Subtraction",Command.SCREEN,0);
private Command mul=new
Command("Multiplication",Command.SCREEN,0);
private Command div=new Command("Division",Command.SCREEN,0);
private Command exit=new Command("EXIT",Command.EXIT,1);
private StringItem result1,result2,result3,result4;
public void startApp()
{
display=Display.getDisplay(this);
n1=new TextField("Enter First Number: ","",8,TextField.NUMERIC);
n2=new TextField("Enter Second Number: ","",8,TextField.NUMERIC);
form1=new Form("Calculator");
form1.append(n1);
form1.append(n2);
form1.addCommand(add);
form1.addCommand(sub);
form1.addCommand(mul);
form1.addCommand(div);
form1.setCommandListener(this);
display.setCurrent(form1);
}
public void pauseApp() {}
public void destroyApp(boolean Unconditional) {}
public void commandAction(Command c,Displayable d)
{
if(c==add)
{
int no1=Integer.parseInt(n1.getString());
int no2= Integer.parseInt(n2.getString());
int add_result=no1+no2;
result1=new StringItem("Addition:","");
result1.setText(add_result + "" );
form1.append(result1);
}
if(c==sub)
{
int no1=Integer.parseInt(n1.getString());
int no2= Integer.parseInt(n2.getString());
int sub_result=no1-no2;
result2=new StringItem("Subtraction:","");
result2.setText(sub_result + "" );
form1.append(result2);
}
if(c==mul)
{
int no1=Integer.parseInt(n1.getString());
int no2= Integer.parseInt(n2.getString());
int mul_result=no1*no2;
result3=new StringItem("Multiplication:","");
result3.setText(mul_result + "" );
form1.append(result3);
}
if(c==div)
{
int no1=Integer.parseInt(n1.getString());
int no2= Integer.parseInt(n2.getString());
int div_result=no1/no2;
result4=new StringItem("Division:","");
result4.setText(div_result + "" );
form1.append(result4);
}
if(c==exit)
{
destroyApp(false);
notifyDestroyed();
}
}
}

Output:

You might also like