/**JAVA PROGRAM FOR CALCULATOR**/
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class calc extends Frame implements ActionListener
{
int val1,val2,res,flag;
TextField t1;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
public calc()
{
b0=new Button("0");
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");
b10=new Button("+");
b11=new Button("-");
b12=new Button("*");
b13=new Button("/");
b14=new Button(".");
b15=new Button("=");
b16=new Button("OFF");
b17=new Button("CLEAR");
t1=new TextField(5);
setLayout(new FlowLayout());
add(t1); add(b17); add(b16); add(b1); add(b2); add(b3); add(b10); add(b4); add(b5); add(b6);
add(b11); add(b7); add(b8); add(b9); add(b12); add(b0); add(b14); add(b15); add(b13);
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);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b0)
{
t1.setText(t1.getText()+"0");
}
if(ae.getSource()==b1)
{
t1.setText(t1.getText()+"1");
}
if(ae.getSource()==b2)
{
t1.setText(t1.getText()+"2");
}
if(ae.getSource()==b3)
{
t1.setText(t1.getText()+"3");
}
if(ae.getSource()==b4)
{
t1.setText(t1.getText()+"4");
}
if(ae.getSource()==b5)
{
t1.setText(t1.getText()+"5");
}
if(ae.getSource()==b6)
{
t1.setText(t1.getText()+"6");
}
if(ae.getSource()==b7)
{
t1.setText(t1.getText()+"7");
}
if(ae.getSource()==b8)
{
t1.setText(t1.getText()+"8");
}
if(ae.getSource()==b9)
{
t1.setText(t1.getText()+"9");
}
if(ae.getSource()==b10)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=1;
}
if(ae.getSource()==b11)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=2;
}
if(ae.getSource()==b12)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=3;
}
if(ae.getSource()==b13)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=4;
}
if(ae.getSource()==b17)
{
t1.setText("");
}
if(ae.getSource()==b16)
{
System.exit(0);
}
if(ae.getSource()==b14)
{
val1=Integer.parseInt(t1.getText());
t1.setText(val1+".");
}
if(ae.getSource()==b15)
{
val2=Integer.parseInt(t1.getText());
switch(flag)
{
case 1: res=val1+val2;
break;
case 2: res=val1-val2;
break;
case 3: res=val1*val2;
break;
case 4: res=val1/val2;
break;
}
t1.setText(""+res);
}
}
public static void main(String args[])
{
calc y=new calc();
y.setSize(100,200);
y.setVisible(true);
}
}
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class calc extends Frame implements ActionListener
{
int val1,val2,res,flag;
TextField t1;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
public calc()
{
b0=new Button("0");
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");
b10=new Button("+");
b11=new Button("-");
b12=new Button("*");
b13=new Button("/");
b14=new Button(".");
b15=new Button("=");
b16=new Button("OFF");
b17=new Button("CLEAR");
t1=new TextField(5);
setLayout(new FlowLayout());
add(t1); add(b17); add(b16); add(b1); add(b2); add(b3); add(b10); add(b4); add(b5); add(b6);
add(b11); add(b7); add(b8); add(b9); add(b12); add(b0); add(b14); add(b15); add(b13);
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);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b0)
{
t1.setText(t1.getText()+"0");
}
if(ae.getSource()==b1)
{
t1.setText(t1.getText()+"1");
}
if(ae.getSource()==b2)
{
t1.setText(t1.getText()+"2");
}
if(ae.getSource()==b3)
{
t1.setText(t1.getText()+"3");
}
if(ae.getSource()==b4)
{
t1.setText(t1.getText()+"4");
}
if(ae.getSource()==b5)
{
t1.setText(t1.getText()+"5");
}
if(ae.getSource()==b6)
{
t1.setText(t1.getText()+"6");
}
if(ae.getSource()==b7)
{
t1.setText(t1.getText()+"7");
}
if(ae.getSource()==b8)
{
t1.setText(t1.getText()+"8");
}
if(ae.getSource()==b9)
{
t1.setText(t1.getText()+"9");
}
if(ae.getSource()==b10)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=1;
}
if(ae.getSource()==b11)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=2;
}
if(ae.getSource()==b12)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=3;
}
if(ae.getSource()==b13)
{
val1=Integer.parseInt(t1.getText());
t1.setText("");
flag=4;
}
if(ae.getSource()==b17)
{
t1.setText("");
}
if(ae.getSource()==b16)
{
System.exit(0);
}
if(ae.getSource()==b14)
{
val1=Integer.parseInt(t1.getText());
t1.setText(val1+".");
}
if(ae.getSource()==b15)
{
val2=Integer.parseInt(t1.getText());
switch(flag)
{
case 1: res=val1+val2;
break;
case 2: res=val1-val2;
break;
case 3: res=val1*val2;
break;
case 4: res=val1/val2;
break;
}
t1.setText(""+res);
}
}
public static void main(String args[])
{
calc y=new calc();
y.setSize(100,200);
y.setVisible(true);
}
}
No comments:
Post a Comment