Calculator Java Source code
import java.awt.*;
import java.awt.event.*;
class Calculator extends Frame implements ActionListener
{
String s1,s2,s3,s4,s5;
Frame f;
TextField tf;
int n, operator;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bAdd,bSub,bMultiple,bDivide,bAns,bClean;
Label l1;
Calculator()
{
// frame code for frame create and designing
f =new Frame("Calculator Prabhat kumar");
f.setSize(405,480);
f.setResizable(false);
f.setVisible(true);
f.setLayout(null);
f.setBackground(Color.GRAY);
f.setForeground(Color.DARK_GRAY);
f.setFont( new Font("Times new roman",Font.BOLD,20));
// closing
f.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
l1= new Label("Prabhat kumar");
l1.setBounds(5, 20, 400, 90);
l1.setAlignment(Label.CENTER);
l1.setBackground(Color.GRAY);
l1.setForeground(Color.WHITE);
f.add(l1);
// textfield for show output
tf = new TextField();
// tf.setBackground();
tf.setBounds(0,110,400,50);
tf.setFont( new Font("Times new roman",Font.BOLD,30));
f.add(tf);
// work with button1
b1 = new Button("1");
b1.setBounds(0,155,100,80);
b1.setBackground(Color.LIGHT_GRAY);
f.add(b1);
b1.addActionListener(this);
// work with button2
b2 = new Button("2");
b2.setBounds(100,155,100,80);
b2.setBackground(Color.LIGHT_GRAY);
f.add(b2);
b2.addActionListener(this);
// work with button3
b3 = new Button("3");
b3.setBounds(200,155,100,80);
b3.setBackground(Color.LIGHT_GRAY);
f.add(b3);
b3.addActionListener(this);
// work with button4
b4 = new Button("4");
b4.setBounds(300,155,100,80);
b4.setBackground(Color.LIGHT_GRAY);
f.add(b4);
b4.addActionListener(this);
// work with button5
b5 = new Button("5");
b5.setBounds(0,235,100,80);
b5.setBackground(Color.LIGHT_GRAY);
f.add(b5);
b5.addActionListener(this);
// work with button
b6 = new Button("6");
b6.setBounds(100,235,100,80);
b6.setBackground(Color.LIGHT_GRAY);
f.add(b6);
b6.addActionListener(this);
// work with button7
b7 = new Button("7");
b7.setBounds(200,235,100,80);
b7.setBackground(Color.LIGHT_GRAY);
f.add(b7);
b7.addActionListener(this);
// work with button8
b8 = new Button("8");
b8.setBounds(300,235,100,80);
b8.setBackground(Color.LIGHT_GRAY);
f.add(b8);
b8.addActionListener(this);
// work with button9
b9 = new Button("9");
b9.setBounds(0,315,100,80);
b9.setBackground(Color.LIGHT_GRAY);
f.add(b9);
b9.addActionListener(this);
// work with button0
b0 = new Button("0");
b0.setBounds(100,315,100,80);
b0.setBackground(Color.LIGHT_GRAY);
f.add(b0);
b0.addActionListener(this);
// work with buttonAns
bAns = new Button("ANSWER");
bAns.setBounds(200,315,200,80);
bAns.setBackground(Color.LIGHT_GRAY);
f.add(bAns);
bAns.addActionListener(this);
// work with buttonAdd
bAdd = new Button("+");
bAdd.setBounds(0,395,100,50);
bAdd.setBackground(Color.LIGHT_GRAY);
f.add(bAdd);
bAdd.addActionListener(this);
// work with buttonSub
bSub = new Button("-");
bSub.setBounds(100,395,100,50);
bSub.setBackground(Color.LIGHT_GRAY);
f.add(bSub);
bSub.addActionListener(this);
// work with buttonMultiple
bMultiple = new Button("*");
bMultiple.setBounds(200,395,100,50);
bMultiple.setBackground(Color.LIGHT_GRAY);
f.add(bMultiple);
bMultiple.addActionListener(this);
// work with buttonDivide
bDivide = new Button("/");
bDivide.setBounds(300,395,100,50);
bDivide.setBackground(Color.LIGHT_GRAY);
f.add(bDivide);
bDivide.addActionListener(this);
// work with buttonClean
bClean = new Button("Clean");
bClean.setBounds(0,445,400,30);
bClean.setBackground(Color.LIGHT_GRAY);
f.add(bClean);
bClean.addActionListener(this);
}
// b1 actionlistener method
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
s3 = tf.getText();
s4 = "0";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b1)
{
s3=tf.getText();
s4= "1";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b2)
{
s3=tf.getText();
s4= "2";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b3)
{
s3=tf.getText();
s4= "3";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b4)
{
s3=tf.getText();
s4= "4";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b5)
{
s3=tf.getText();
s4= "5";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b6)
{
s3=tf.getText();
s4= "6";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b7)
{
s3=tf.getText();
s4= "7";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b8)
{
s3=tf.getText();
s4= "8";
s5 = s3+s4;
tf.setText(s5);
}
if(e.getSource()==b9)
{
s3=tf.getText();
s4= "9";
s5 = s3+s4;
tf.setText(s5);
}
// operator chosing code
if(e.getSource()==bAdd)
{
s1=tf.getText();
tf.setText("");
operator=1;
}
if(e.getSource()==bSub)
{
s1=tf.getText();
tf.setText("");
operator=2;
}
if(e.getSource()==bMultiple)
{
s1=tf.getText();
tf.setText("");
operator=3;
}
if(e.getSource()==bDivide)
{
s1=tf.getText();
tf.setText("");
operator=4;
}
// for equal code
if(e.getSource()==bAns)
{
s2=tf.getText();
if(operator==1)
{
n=Integer.parseInt(s1)+Integer.parseInt(s2);
tf.setText(String.valueOf(n));
}
if(operator==2)
{
n=Integer.parseInt(s1)-Integer.parseInt(s2);
tf.setText(String.valueOf(n));
}
if(operator==3)
{
n=Integer.parseInt(s1)*Integer.parseInt(s2);
tf.setText(String.valueOf(n));
}
if(operator==4)
{
try
{
int p=Integer.parseInt(s2);
if(p!=0)
{
n = Integer.parseInt(s1)/Integer.parseInt(s2);
tf.setText(String.valueOf(n));
}
else{
tf.setText("infinite");
}
}catch(Exception i){}
}
}
if(e.getSource()==bClean)
{
tf.setText("");
}
}
public static void main(String[] args) {
Calculator cl = new Calculator();
}
}
CALCULATOR JAVA PROGRAM |
No comments:
Post a Comment