0% found this document useful (0 votes)
73 views5 pages

Experiment 5c - BASIC CALCULATOR

This document contains the source code for a basic calculator mobile application. It defines a MIDlet class called Calculator with methods for starting, pausing and destroying the application. The startApp method initializes the user interface with text fields for inputting numbers and command buttons for arithmetic operations. The commandAction method parses the user input, performs the selected calculation, and displays the result.

Uploaded by

PREET GADA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views5 pages

Experiment 5c - BASIC CALCULATOR

This document contains the source code for a basic calculator mobile application. It defines a MIDlet class called Calculator with methods for starting, pausing and destroying the application. The startApp method initializes the user interface with text fields for inputting numbers and command buttons for arithmetic operations. The commandAction method parses the user input, performs the selected calculation, and displays the result.

Uploaded by

PREET GADA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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