write a following code in netbeans IDE 7.1 and save file as calc.java and run it and you will have your own calculator...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;
/**
*
* @author NISH
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
JTextField txt;
JButton btn[]=new JButton[9];
JPanel jp1,jp2,jp3;
JButton plus,minus,mul,div,zero,dot,equal,cncl,square,sqrt,fact;
String str;
Boolean flag=false,flag_eq=false,flag_op=false,flag_a=false,flag_b=false;
Character opr=null;
String first=null,second=null;
Double a,b;
Double ans=0.0;
Integer x,y,z=0;
public Calculator()
{
this.setLayout(new FlowLayout());
txt=new JTextField("",19);
txt.setBounds(0, 0, 200, 20);
jp1=new JPanel();
jp2=new JPanel(new GridLayout(5, 4, 5, 5));
jp1.add(txt);
plus=new JButton("" + "+" + "");
minus=new JButton("" + "-" + "");
mul=new JButton("" + "*" + "");
div=new JButton("" + "/" + "");
zero=new JButton("" + "0" + "");
dot=new JButton("" + "." + "");
equal=new JButton("" + "=" + "");
cncl=new JButton("" + "C" + "");
square=new JButton("" + "^" + "");
sqrt=new JButton("" + "VX" + "");
fact=new JButton("" + "X!" + "");
for(int i=0;i<13;i++)
{
if(i==3)
{
jp2.add(plus);
}
if(i==6)
{
jp2.add(minus);
}
if(i==9)
{
jp2.add(mul);
}
if(i<9)
{
btn[i]=new JButton(" " + (i +1) +" ");
jp2.add(btn[i]);
}
}
jp2.add(zero);
jp2.add(dot);
jp2.add(div);
jp2.add(cncl);
jp2.add(square);
jp2.add(sqrt);
jp2.add(fact);
jp2.add(equal);
add(jp1);
add(jp2);
for(int i=0;i<9;i++)
{
btn[i].addActionListener(this);
}
plus.addActionListener(this);
minus.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
zero.addActionListener(this);
dot.addActionListener(this);
equal.addActionListener(this);
cncl.addActionListener(this);
square.addActionListener(this);
sqrt.addActionListener(this);
fact.addActionListener(this);
}
public static void main(String args[])
{
Calculator cl=new Calculator();
cl.setTitle("Calculator");
cl.setSize(240, 230);
cl.setVisible(true);
cl.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<9;i++)
{
if(e.getSource().equals(btn[i]))
{
if(flag_eq==true)
{
txt.setText(null);
flag_eq=false;
}
if(flag_op==true)
{
txt.setText(null);
flag_op=false;
}
if(txt.getText()!=null)
{
str=txt.getText();
txt.setText(str + String.valueOf((i+1)));
}
else
{
txt.setText(String.valueOf((i+1)));
}
}
}
if(e.getSource().equals(plus))
{
first=txt.getText().toString();
opr='+';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(minus))
{
first=txt.getText().toString();
opr='-';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(mul))
{
first=txt.getText().toString();
opr='*';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(div))
{
first=txt.getText().toString();
opr='/';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(zero))
{
if(txt.getText()!=null)
{
str=txt.getText();
txt.setText(str + "0");
}
else
{
txt.setText("0");
}
}
if(e.getSource().equals(cncl))
{
txt.setText("");
}
if(e.getSource().equals(square))
{
first=txt.getText().toString();
opr='^';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(fact))
{
long l=1;
for(int i=Integer.parseInt(txt.getText().toString());i>=1;i--)
{
l=l*i;
}
txt.setText(String.valueOf(l));
}
if(e.getSource().equals(sqrt))
{
txt.setText(String.valueOf(Math.sqrt(Double.parseDouble(txt.getText().toString()))));
}
if(e.getSource().equals(dot))
{
if(txt.getText()!=null)
txt.setText(txt.getText().toString() + ".");
else
txt.setText(".");
}
if(e.getActionCommand().equals("="))
{
a=Double.parseDouble(first);
b=Double.parseDouble(txt.getText().toString());
if(opr=='/')
{
if(first.indexOf(".")!=-1 && txt.getText().indexOf(".")!=-1)
{
txt.setText(String.valueOf((Double.parseDouble(first) / Double.parseDouble(txt.getText().toString()))));
}
else
{
txt.setText(String.valueOf(Float.parseFloat(first) / Float.parseFloat(txt.getText().toString())));
}
}
else
{
switch(opr)
{
case '+':
ans=a+b;
break;
case '*':
ans=a*b;
break;
case '-':
ans=a-b;
break;
case '^':
ans=Math.pow(a, b);
break;
}
String st= String.valueOf(ans);
st=st+0;
int indx=st.indexOf(".");
if( st.charAt(indx+1)=='0' && st.charAt(indx+2) =='0')
{
String st1=st.substring(0, indx);
txt.setText(st1);
}
else
{
txt.setText(String.valueOf(ans));
}
flag_eq=true;
}
}
}
}
OUTPUT:-
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calculator;
/**
*
* @author NISH
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
JTextField txt;
JButton btn[]=new JButton[9];
JPanel jp1,jp2,jp3;
JButton plus,minus,mul,div,zero,dot,equal,cncl,square,sqrt,fact;
String str;
Boolean flag=false,flag_eq=false,flag_op=false,flag_a=false,flag_b=false;
Character opr=null;
String first=null,second=null;
Double a,b;
Double ans=0.0;
Integer x,y,z=0;
public Calculator()
{
this.setLayout(new FlowLayout());
txt=new JTextField("",19);
txt.setBounds(0, 0, 200, 20);
jp1=new JPanel();
jp2=new JPanel(new GridLayout(5, 4, 5, 5));
jp1.add(txt);
plus=new JButton("" + "+" + "");
minus=new JButton("" + "-" + "");
mul=new JButton("" + "*" + "");
div=new JButton("" + "/" + "");
zero=new JButton("" + "0" + "");
dot=new JButton("" + "." + "");
equal=new JButton("" + "=" + "");
cncl=new JButton("" + "C" + "");
square=new JButton("" + "^" + "");
sqrt=new JButton("" + "VX" + "");
fact=new JButton("" + "X!" + "");
for(int i=0;i<13;i++)
{
if(i==3)
{
jp2.add(plus);
}
if(i==6)
{
jp2.add(minus);
}
if(i==9)
{
jp2.add(mul);
}
if(i<9)
{
btn[i]=new JButton(" " + (i +1) +" ");
jp2.add(btn[i]);
}
}
jp2.add(zero);
jp2.add(dot);
jp2.add(div);
jp2.add(cncl);
jp2.add(square);
jp2.add(sqrt);
jp2.add(fact);
jp2.add(equal);
add(jp1);
add(jp2);
for(int i=0;i<9;i++)
{
btn[i].addActionListener(this);
}
plus.addActionListener(this);
minus.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
zero.addActionListener(this);
dot.addActionListener(this);
equal.addActionListener(this);
cncl.addActionListener(this);
square.addActionListener(this);
sqrt.addActionListener(this);
fact.addActionListener(this);
}
public static void main(String args[])
{
Calculator cl=new Calculator();
cl.setTitle("Calculator");
cl.setSize(240, 230);
cl.setVisible(true);
cl.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<9;i++)
{
if(e.getSource().equals(btn[i]))
{
if(flag_eq==true)
{
txt.setText(null);
flag_eq=false;
}
if(flag_op==true)
{
txt.setText(null);
flag_op=false;
}
if(txt.getText()!=null)
{
str=txt.getText();
txt.setText(str + String.valueOf((i+1)));
}
else
{
txt.setText(String.valueOf((i+1)));
}
}
}
if(e.getSource().equals(plus))
{
first=txt.getText().toString();
opr='+';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(minus))
{
first=txt.getText().toString();
opr='-';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(mul))
{
first=txt.getText().toString();
opr='*';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(div))
{
first=txt.getText().toString();
opr='/';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(zero))
{
if(txt.getText()!=null)
{
str=txt.getText();
txt.setText(str + "0");
}
else
{
txt.setText("0");
}
}
if(e.getSource().equals(cncl))
{
txt.setText("");
}
if(e.getSource().equals(square))
{
first=txt.getText().toString();
opr='^';
flag_op=true;
//txt.setText("");
}
if(e.getSource().equals(fact))
{
long l=1;
for(int i=Integer.parseInt(txt.getText().toString());i>=1;i--)
{
l=l*i;
}
txt.setText(String.valueOf(l));
}
if(e.getSource().equals(sqrt))
{
txt.setText(String.valueOf(Math.sqrt(Double.parseDouble(txt.getText().toString()))));
}
if(e.getSource().equals(dot))
{
if(txt.getText()!=null)
txt.setText(txt.getText().toString() + ".");
else
txt.setText(".");
}
if(e.getActionCommand().equals("="))
{
a=Double.parseDouble(first);
b=Double.parseDouble(txt.getText().toString());
if(opr=='/')
{
if(first.indexOf(".")!=-1 && txt.getText().indexOf(".")!=-1)
{
txt.setText(String.valueOf((Double.parseDouble(first) / Double.parseDouble(txt.getText().toString()))));
}
else
{
txt.setText(String.valueOf(Float.parseFloat(first) / Float.parseFloat(txt.getText().toString())));
}
}
else
{
switch(opr)
{
case '+':
ans=a+b;
break;
case '*':
ans=a*b;
break;
case '-':
ans=a-b;
break;
case '^':
ans=Math.pow(a, b);
break;
}
String st= String.valueOf(ans);
st=st+0;
int indx=st.indexOf(".");
if( st.charAt(indx+1)=='0' && st.charAt(indx+2) =='0')
{
String st1=st.substring(0, indx);
txt.setText(st1);
}
else
{
txt.setText(String.valueOf(ans));
}
flag_eq=true;
}
}
}
}
OUTPUT:-