0% found this document useful (0 votes)
94 views

Calculator Program

This document describes a Java program that implements a simple calculator application. The program uses Swing components like Frame, Panel, TextField, and Buttons to build the calculator user interface. It takes numeric input from the digit buttons, handles arithmetic operators, and displays results in a text field. Key aspects include storing user input in strings, parsing input as doubles for calculations, and updating the display after operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Calculator Program

This document describes a Java program that implements a simple calculator application. The program uses Swing components like Frame, Panel, TextField, and Buttons to build the calculator user interface. It takes numeric input from the digit buttons, handles arithmetic operators, and displays results in a text field. Key aspects include storing user input in strings, parsing input as doubles for calculations, and updating the display after operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Calculator Program

Program:
import java.awt.*;
import java .awt.event.*;
class Cal extends Frame implements ActionListener
{
TextField t1;
Button b1[],b2[],b3[],b4;
Panel p1,p2,p3,p4;
Label l1;
String opr,s1,s2;
String op[]={"+","-","*","/","=","clear"};
int f;
boolean flag;
Cal()
{
super("SIMPLE CALCULATOR");
setVisible(true);
t1=new TextField(20);
p1=new Panel();
p2=new Panel();
p3=new Panel();
b1=new Button[10];
for(int i=0;i<10;i++)
{
b1[i]=new Button(Integer.toString(i));
b1[i].addActionListener(this);
}
b2=new Button[6];
for(int i=0;i<6;i++)
{
b2[i]=new Button(op[i]);
b2[i].addActionListener(this);
}
b4=new Button(".");
b4.addActionListener(this);
p1.setLayout(new GridLayout(4,3));
for(int i=0;i<10;i++)
p1.add(b1[i]);
1

p1.add(b4);
p2.setLayout(new GridLayout(1,6));
for(int i=0;i<6;i++)
p2.add(b2[i]);
add(t1,"North");
add(p1,"Center");
add(p2,"South");
s1=" ";
s2=" ";
f=1;
flag=true;
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
char c=s.charAt(0);
if(c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9'||c=='.')
{
if(f==1)
{
t1.setText(" ");
s1="";
f=0;
}
s1=s1+s;
t1.setText(s1);
}
else if(c=='+'||c=='-'||c=='*'||c=='/')
{
f=1;
if(flag)
{
s2=s1;
s1=" ";
opr=s;
flag=false;
}
else
{
calculate();
opr=s;
2

}
}
else if(c=='=')
{
calculate();
s1=s2;
f=1;
flag=true;
}
else if(s.equals("clear"))
{
t1.setText("");
f=1;
flag=true;
s1=" ";
}
}
void calculate()
{
double a=Double.parseDouble(s2);
double b=Double.parseDouble(s1);
double c1=0;
if(opr=="+")
c1=a+b;
else if(opr=="-")
c1=a-b;
else if(opr=="*")
c1=a*b;
else if(opr=="/")
c1=a/b;
t1.setText(Double.toString(c1));
s2=Double.toString(c1);
}
}
class Tcal
{
public static void main(String args[])
{
Cal c=new Cal();
}
3

Output:
D:\java>javac Tcal.java
4

D:\java>java Tcal

You might also like