Simple Calculator

Aim: 
To write a java program that works as a simple calculator. Use a grid layout to arrangebuttons for the digits +,-,*,/,% operations. Add a text field to display the result.

Description:
Write a class by extending JFrame class of javax.swing package and implementing ActionListener interface. Attach the components like JTextField, JButtons, two JPanels to the contentPane() of JFrame by setting grid layout. Attach listeners to the buttons and implement the actionPerformed method to perform operations. One JPanel is used for placing textbox and clear buttons, the second JPanel is used to add remaining components.

Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator extends JFrame implements ActionListener
{ Container c;
JTextField t1;
String a="",b;
String oper="",s="",p="";
int first=0,second=0,result=0;
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
JButton add,sub,mul,div,mod,res,clear;
JPanel p1,p2;
Calculator()
{ c =getContentPane();
p1 = new JPanel();
p2 = new JPanel(new GridLayout(4,4));
t1=new JTextField(a,10);
c.setLayout(new GridLayout(2,1));
b0=new JButton("0");
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
add=new JButton("+");
sub=new JButton("-");
mul=new JButton("*");
div=new JButton("/");
mod=new JButton("%");
res=new JButton("=");
clear = new JButton("CE");
b0.addActionListener(this);
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);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
res.addActionListener(this);
clear.addActionListener(this);
p1.add(t1); p1.add(clear);
p2.add(b0); p2.add(b1); p2.add(b2);
p2.add(b3); p2.add(b4); p2.add(b5);
p2.add(b6); p2.add(b7); p2.add(b8);
p2.add(b9); p2.add(add); p2.add(sub);
p2.add(mul); p2.add(div); p2.add(mod);
p2.add(res);
c.add(p1);
c.add(p2);
}
public void actionPerformed(ActionEvent ae)
{ a=ae.getActionCommand();
if(a=="0" || a=="1" || a=="2" || a=="3" || a=="4" || a=="5"
|| a=="6" || a=="7" || a=="8" || a=="9")
{
t1.setText(t1.getText()+a);
}
if(a=="+" || a=="-" || a=="*" || a=="/" || a=="%")
{
first = Integer.parseInt(t1.getText());
oper = a;
t1.setText("");
}
if(a=="=")
{ if(oper=="+")
result=first+Integer.parseInt(t1.getText());
if(oper=="-")
result=first-Integer.parseInt(t1.getText());
if(oper=="*")
result=first*Integer.parseInt(t1.getText());
if(oper=="/")
result=first/Integer.parseInt(t1.getText());
if(oper=="%")
result=first%Integer.parseInt(t1.getText());
t1.setText(result+"");
}
if(a == "CE")
t1.setText("");
}
public static void main(String args[])
{ Calculator ob = new Calculator();
ob.setSize(200,200);
ob.setVisible(true);
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}