// calculator program
import java.awt.*;
import java.awt.event.*;
public class Class1 extends Frame implements ActionListener
{
//private Menu E, V, H;
private TextField t1,lcdMemory;
private Button BackSp, CE, C, keyArray[];
private Panel keyPad, keyPad1;
private boolean opflag, foundkey;
private int op;
private char oper;
private String s;
private double num, result;
public Class1()
{
/*E = new Menu("Edit");
V = new Menu("View");
H = new Menu("Help");
key = new Panel();
key.add(E);
key.add(V);
key.add(H);
add(key, BorderLayout.WEST);*/
t1 = new TextField(20);
add(t1, BorderLayout.NORTH);
lcdMemory = new TextField(3);
BackSp = new Button("Backspace");
BackSp.setForeground(Color.red);
CE = new Button(" CE ");
CE.setForeground(Color.red);
C = new Button(" C ");
C.setForeground(Color.red);
keyPad1 = new Panel();
keyPad1.add(lcdMemory);
keyPad1.add(BackSp);
BackSp.addActionListener(this);
keyPad1.add(CE);
CE.addActionListener(this);
keyPad1.add(C);
C.addActionListener(this);
keyPad1.setLayout(new GridLayout(1,4));
add(keyPad1, BorderLayout.CENTER);
keyArray = new Button[24];
keyArray[0] = new Button("MC");
keyArray[0].setForeground(Color.red);
keyArray[1] = new Button("7");
keyArray[1].setForeground(Color.blue);
keyArray[2] = new Button("8");
keyArray[2].setForeground(Color.blue);
keyArray[3] = new Button("9");
keyArray[3].setForeground(Color.blue);
keyArray[4] = new Button("/");
keyArray[4].setForeground(Color.red);
keyArray[5] = new Button("sqrt");
keyArray[5].setForeground(Color.blue);
keyArray[6] = new Button("MR");
keyArray[6].setForeground(Color.red);
keyArray[7] = new Button("4");
keyArray[7].setForeground(Color.blue);
keyArray[8] = new Button("5");
keyArray[8].setForeground(Color.blue);
keyArray[9] = new Button("6");
keyArray[9].setForeground(Color.blue);
keyArray[10] = new Button("*");
keyArray[10].setForeground(Color.red);
keyArray[11] = new Button("%");
keyArray[11].setForeground(Color.blue);
keyArray[12] = new Button("MS");
keyArray[12].setForeground(Color.red);
keyArray[13] = new Button("1");
keyArray[13].setForeground(Color.blue);
keyArray[14] = new Button("2");
keyArray[14].setForeground(Color.blue);
keyArray[15] = new Button("3");
keyArray[15].setForeground(Color.blue);
keyArray[16]= new Button("-");
keyArray[16].setForeground(Color.red);
keyArray[17]= new Button("1/x");
keyArray[17].setForeground(Color.blue);
keyArray[18] = new Button("M+");
keyArray[18].setForeground(Color.red);
keyArray[19] = new Button("0");
keyArray[19].setForeground(Color.blue);
keyArray[20] = new Button("+/-");
keyArray[20].setForeground(Color.blue);
keyArray[21] = new Button(".");
keyArray[21].setForeground(Color.blue);
keyArray[22] = new Button("+");
keyArray[22].setForeground(Color.red);
keyArray[23] = new Button("=");
keyArray[23].setForeground(Color.blue);
keyPad = new Panel();
keyPad.setLayout(new GridLayout(4,6,4,4));
for(int i = 0; i<24; i++)
{
keyPad.add(keyArray[i]);
}
for(int i = 0; i<keyArray.length; i++)
keyArray[i].addActionListener(this);
add(keyPad,BorderLayout.SOUTH);
WindowAdapter m = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void addToDisplay(String s)
{
t1.setText(t1.getText() + s);
}
public double PerformCalc() //handle the operator keys
{
switch(oper )
{
case '/':
num = Double.valueOf(t1.getText()).doubleValue();
result /=num;
break;
case '*':
num = Double.valueOf(t1.getText()).doubleValue();
result *= num;
break;
case '-': num = Double.valueOf(t1.getText()).doubleValue();
result -= num;
break;
case '+': num = Double.valueOf(t1.getText()).doubleValue();
result += num;
break;
case 's': result = Math.sqrt(result);
break;
case '%':
num = Double.valueOf(t1.getText()).doubleValue();
result %= num;
break;
case 'x':
result = 1/result;
break;
};
return result;
}
public void clearAll() //handle C key
{
t1.setText("");
result = num = 0;
}
public void clearEntry() //handle CE key
{
t1.setText("");
num = 0;
}
public void actionPerformed(ActionEvent e)
{
String s1 = t1.getText();
foundkey = false;
if(e.getSource()== BackSp)
{
if(s1.length()>1)
{
s1 = s1.substring(0, s1.length()-1);
t1.setText("" + s1);
}
else if(s1.length()==1)
clearEntry();
}
if(e.getSource()== CE)
clearEntry();
else if(e.getSource()== C)
clearAll();
for(int i=0; i<keyArray.length; i++)
{
if(e.getSource()==keyArray[i])
{
foundkey = true;
switch(i)
{
case 0:
break;
case 1:
addToDisplay("7");
break;
case 2:
addToDisplay("8");
break;
case 3:
addToDisplay("9");
break;
case 4:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = "";
oper = '/';
break;
case 5:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = "";
oper = 's';
break;
case 6:
break;
case 7: addToDisplay("4");
break;
case 8: addToDisplay("5");
break;
case 9: addToDisplay("6");
break;
case 10:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = "";
oper = '*';
break;
case 11:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = t1.getText();
oper = '%';
break;
case 12:
break;
case 13: addToDisplay("1");
break;
case 14: addToDisplay("2");
//if(oper=='!')//handle the sign of the first number
break;
case 15: addToDisplay("3");
break;
case 16:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = "";
oper ='-';
break;
case 17:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = "";
oper = 'x';
case 18:
break;
case 19: addToDisplay("0");
break;
case 20:
result = -Double.valueOf(t1.getText()).doubleValue();
t1.setText("" + result);
break;
case 21:
if(s1.indexOf(".")<=0)
{
s1 += ".";
}
t1.setText(s1);
break;
case 22:
result = Double.valueOf(t1.getText()).doubleValue();
t1.setText("");
s1 = t1.getText();
oper = '+';
continue;
case 23:
//t1.setText(String.valueOf(PerformCalc()));
t1.setText("" + PerformCalc());
break;
};
}
}
}
public static void main (String[] args)
{
Class1 c = new Class1();
c.setBounds(320,320,320,220);
c.setTitle("Calculator");
c.setVisible(true);
System.out.println();
}
}