package homework;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Calculater extends Frame implements ActionListener
{
TextField tf=new TextField(30);
Panel p=new Panel();
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9= new Button("9");
Button b10 = new Button("0");
Button b11 = new Button("+");
Button b12 = new Button("-");
Button b13 = new Button("*");
Button b14 = new Button("/");
Button b15 = new Button("=");
Button b16 = new Button("C");
private String op = "";
private String str = "";
public String getOp()
{
return op;
}
public void setOp(String op)
{
this.op = op;
}
public String getStr()
{
return str;
}
public void setStr(String str)
{
this.str = str;
}
public Calculater(String name)
{
super(name);
this.b1.addActionListener(this);
this.b2.addActionListener(this);
this.b3.addActionListener(this);
this.b4.addActionListener(this);
this.b5.addActionListener(this);
this.b6.addActionListener(this);
this.b7.addActionListener(this);
this.b8.addActionListener(this);
this.b9.addActionListener(this);
this.b10.addActionListener(this);
this.b11.addActionListener(this);
this.b12.addActionListener(this);
this.b13.addActionListener(this);
this.b14.addActionListener(this);
this.b15.addActionListener(this);
this.b16.addActionListener(this);
this.setLayout(new BorderLayout());
this.add(tf,BorderLayout.NORTH);
this.add(p,BorderLayout.CENTER);
this.p.setLayout(new GridLayout(4,4));
this.p.add(b7);
this.p.add(b8);
this.p.add(b9);
this.p.add(b13);
this.p.add(b4);
this.p.add(b5);
this.p.add(b6);
this.p.add(b12);
this.p.add(b1);
this.p.add(b2);
this.p.add(b3);
this.p.add(b11);
this.p.add(b10);
this.p.add(b16);
this.p.add(b15);
this.p.add(b14);
this.setSize(400, 300);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setVisible(true);
}
public static void main(String[] args)
{
new Calculater("Calculater").setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if("0".equals(command))
{
this.setStr(str+="0");
this.tf.setText(str);
}
if("1".equals(command))
{
this.setStr(str+="1");
this.tf.setText(str);
}
if("2".equals(command))
{
this.setStr(str+="2");
this.tf.setText(str);
}
if("3".equals(command))
{
this.setStr(str+="3");
this.tf.setText(str);
}
if("4".equals(command))
{
this.setStr(str+="4");
this.tf.setText(str);
}
if("5".equals(command))
{
this.setStr(str+="5");
this.tf.setText(str);
}
if("6".equals(command))
{
this.setStr(str+="6");
this.tf.setText(str);
}
if("7".equals(command))
{
this.setStr(str+="7");
this.tf.setText(str);
}
if("8".equals(command))
{
this.setStr(str+="8");
this.tf.setText(str);
}
if("9".equals(command))
{
this.setStr(str+="9");
this.tf.setText(str);
}
if("+".equals(command))
{
this.setStr(str+="+");
this.setOp("+");
this.tf.setText(str);
}
if("-".equals(command))
{
this.setStr(str+="-");
this.setOp("-");
this.tf.setText(str);
}
if("*".equals(command))
{
this.setStr(str+="*");
this.setOp("*");
this.tf.setText(str);
}
if("/".equals(command))
{
this.setStr(str+="/");
this.setOp("/");
this.tf.setText(str);
}
if("=".equals(command))
{
String result[]=this.getStr().split("\\"+this.getOp());
int a=Integer.parseInt(result[0]);
int b=Integer.parseInt(result[1]);
int d=0;
if(op.equals("+"))
{
d=a+b;
this.tf.setText(new Integer(d).toString());
}
else if(op.equals("-"))
{
d=a-b;
this.tf.setText(new Integer(d).toString());
}
else if(op.equals("*"))
{
d=a*b;
this.tf.setText(new Integer(d).toString());
}
else if(op.equals("/"))
{
double dd=(double)a/(double)b;
this.tf.setText(dd+"");
}
}
if("C".equals(command))
{
this.tf.setText("");
this.setStr("");
this.setOp("");
}
}
}