Skip to main content

calculator program using Java Programming

 Java calculator can be easily created by creating buttons and texbox. For handling event we implement ActionListener event listener interface.



import java.awt.*;
import java.awt.event.*;
public class calculator extends Frame implements ActionListener
{
String s;
int n=0,val,sum,dif,mult,divs;
int op;
TextField tf;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,plus,sub,mul,div,eq,cc;
public calculator()
{
super("Calculator");
Panel pn1=new Panel();
Panel pn2=new Panel();
pn2.setLayout(new FlowLayout(FlowLayout.CENTER));
tf=new TextField(10);
pn2.add(tf);
pn1.add(pn2);
Panel pn3=new Panel();
pn3.setLayout(new GridLayout(4,4));
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
plus=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
eq=new Button("=");
cc=new Button("C");
pn3.add(b7);
pn3.add(b8);
pn3.add(b9);
pn3.add(div);
pn3.add(b4);
pn3.add(b5);
pn3.add(b6);
pn3.add(mul);
pn3.add(b1);
pn3.add(b2);
pn3.add(b3);
pn3.add(sub);
pn3.add(b0);
pn3.add(cc);
pn3.add(eq);
pn3.add(plus);
pn1.add(pn3);
add(pn1);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
cc.addActionListener(this);
plus.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
eq.addActionListener(this);
addWindowListener(new winadp());
}
public class winadp extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
tf.setText(tf.getText()+"1");
if(ae.getSource()==b2)
tf.setText(tf.getText()+"2");
if(ae.getSource()==b3)
tf.setText(tf.getText()+"3");
if(ae.getSource()==b4) 
tf.setText(tf.getText()+"4");
if(ae.getSource()==b5)
tf.setText(tf.getText()+"5");
if(ae.getSource()==b6)
tf.setText(tf.getText()+"6");
if(ae.getSource()==b7)
tf.setText(tf.getText()+"7");
if(ae.getSource()==b8)
tf.setText(tf.getText()+"8");
if(ae.getSource()==b9)
tf.setText(tf.getText()+"9");
if(ae.getSource()==b0)
tf.setText(tf.getText()+"0");
if(ae.getSource()==cc)
tf.setText(null);
if(ae.getSource()==plus)
{
s=plus.getLabel();
n=Integer.parseInt(tf.getText());
tf.setText("");
}
if(ae.getSource()==sub)
{
s=sub.getLabel();
n=Integer.parseInt(tf.getText());
tf.setText("");
}
if(ae.getSource()==mul)
{
s=mul.getLabel();
n=Integer.parseInt(tf.getText());
tf.setText("");
}
if(ae.getSource()==div)
{
s=div.getLabel();
n=Integer.parseInt(tf.getText());
tf.setText("");
}
if(ae.getSource()==eq)
{
val=Integer.parseInt(tf.getText());
if(s.equals("+"))
{
sum=n+val;
tf.setText(String.valueOf(sum));
}
if(s.equals("-"))
{
dif=n-val;
tf.setText(String.valueOf(dif));
}
if(s.equals("*"))
{
mult=n*val;
tf.setText(String.valueOf(mult));
}
if(s.equals("/"))
{
divs=n/val;
tf.setText(String.valueOf(divs));
}
}
}
public static void main(String args[])
{
calculator cal=new calculator();
cal.setSize(200,250);
cal.setVisible(true);
}
}

 

Calculator Program using java

 

Thank You. Like Share and Subscribe 👍 folowlect.com

Comments