package mycounter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.lang.Integer;
public class counter extends JFrame implements ActionListener {
JFrame Jf=new JFrame("Mycounter");
JPanel Jp1=new JPanel();
JPanel Jp2=new JPanel();
JTextField Jtf=new JTextField(25);
String str[]={"sin","1","2","3","+","cos","4","5","6","-","tan","7","8","9",
"*","Int","0",".","=","/"};
JButton JBu;
String operator=null;
String num,s;
double one;
static double two=0;
boolean bool=true;
public counter()
{
init();
}
public void init()
{
Jf.setBounds(500,200,300,240);
Jf.setLayout(new BorderLayout());
Jtf.setVisible(true);
Jp1.add(Jtf);
Jp1.setVisible(true);
Jf.add(Jp1,BorderLayout.NORTH);
Jp2.setLayout(new GridLayout(4,5,1,1));
for(int i=0;i<20;i++)
{
JBu=new JButton(str[i]);
JBu.addActionListener(this);
Jp2.add(JBu);
}
Jp2.setVisible(true);
Jf.add(Jp2,BorderLayout.CENTER);
Jf.setVisible(true);
}
public void actionPerformed( ActionEvent e)
{
s=e.getActionCommand();
if(s.charAt(0)=='0')
{
if(!bool&&s!="=")
{
Jtf.setText(Jtf.getText()+s);
}
else if(operator!=null)
{
Jtf.setText(s);
num = Jtf.getText();
two=0;
operator=null;
}
}
else if('1'<=s.charAt(0)&&s.charAt(0)<='9')
{
if(!bool)
{
Jtf.setText(Jtf.getText()+s);
}
else
{
Jtf.setText(s);
}
bool=false;
}
else if(s==".")
{
if(Jtf.getText().indexOf(".")==-1)
{
if(Jtf.getText()==null)
{
Jtf.setText("0.");
}
else if(!bool)
{
Jtf.setText(Jtf.getText()+s);
}
bool=false;
}
}
else if(s=="sin"||s=="cos"||s=="tan"||s=="Int")
{
num=Jtf.getText();
operation1();
}
else if(s=="=")
{
operation();
}
else
{
if(operator!=null)
operation();
else
{ operator=s;
num=Jtf.getText();
Jtf.setText("");
}
}
}
public void operation1()
{
one=Double.parseDouble(num);
if(s=="sin")
{
Jtf.setText(""+Math.sin(one));
}
if(s=="cos")
{
Jtf.setText(""+Math.cos(one));
}
if(s=="tan")
{
Jtf.setText(""+Math.tan(one));
}
if(s=="Int")
{
int i=(int)one;
// System.out.println(i);
Jtf.setText(""+i);
}
}
public void operation()
{
one=Double.parseDouble(num);
if(two==0)
two=Double.parseDouble(Jtf.getText());
char c=operator.charAt(0);
switch(c)
{
case '+':one+=two;Jtf.setText(""+one); num=Jtf.getText();bool=true;return;
case '-':one-=two;Jtf.setText(""+one);
num=Jtf.getText();bool=true;return;
case '*':one*=two;Jtf.setText(""+one);
num=Jtf.getText();bool=true;return;
case '/':one/=two;Jtf.setText(""+one);
num=Jtf.getText();bool=true;return;
}
}
public static void main(String arg[])
{
new counter();
}
}