import java.awt.*;
import java.awt.event.*;
class CalcFrame extends Frame
{
CalcFrame( String str)
{
super(str);
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent we)
{
System.exit(0);
}
});
}
}
public class Calculator5 implements ActionListener, ItemListener
{
CalcFrame fr;
MenuBar mb;
Menu about;
MenuItem author;
CheckboxGroup cbg;
Checkbox degrees,radians;
TextField display;
Button clearAll, clearEntry, round;
Button scientificKey[] = new Button[34];
boolean addButtonPressed, subtractButtonPressed,multiplyButtonPressed;
boolean divideButtonPressed, decimalPointPressed, powerButtonPressed;
boolean roundButtonPressed =false;
boolean logButtonPressed,expButtonPressed,sqrtButtonPressed;
double initialNumber;
double currentNumber = 0;
int decimalPlaces = 0;
public static void main (String args[])
{
Calculator5 calc = new Calculator5();
calc.makeCalculator();
}
public void makeCalculator()
{
final int BWIDTH = 25;
final int BHEIGHT = 25;
int count =1;
fr = new CalcFrame("Scientific Calculator");
fr.setSize(330,300);
fr.setBackground(Color.black);
mb = new MenuBar();
about = new Menu("About");
author = new MenuItem("Author");
author.addActionListener(this);
about.add(author);
mb.add(about);
fr.setMenuBar(mb);
fr.setLayout(null);
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 3; ++col)
{
scientificKey[count] = new Button(Integer.toString(count));
scientificKey[count].addActionListener(this);
scientificKey[count].setBounds(30*(col + 1), 30*(row +
4),BWIDTH,BHEIGHT);
scientificKey[count].setBackground(Color.white);
fr.add(scientificKey[count++]);
}
}
scientificKey[0] = new Button("0");
scientificKey[0].addActionListener(this);
scientificKey[0].setBounds(30,210,BWIDTH,BHEIGHT);
scientificKey[0].setBackground(Color.white);
fr.add(scientificKey[0]);
scientificKey[1] = new Button(".");
scientificKey[1].addActionListener(this);
scientificKey[1].setBounds(60,210,BWIDTH,BHEIGHT);
scientificKey[1].setBackground(Color.white);
fr.add(scientificKey[1]);
scientificKey[2] = new Button("=");
scientificKey[2].addActionListener(this);
scientificKey[2].setBounds(90,210,BWIDTH,BHEIGHT);
scientificKey[2].setBackground(Color.white);
fr.add(scientificKey[2]);
scientificKey[3] = new Button("*");
scientificKey[3].addActionListener(this);
scientificKey[3].setBounds(120,120,BWIDTH,BHEIGHT);
scientificKey[3].setBackground(Color.white);
fr.add(scientificKey[3]);
scientificKey[4] = new Button("/");
scientificKey[4].addActionListener(this);
scientificKey[4].setBounds(120,150,BWIDTH,BHEIGHT);
scientificKey[4].setBackground(Color.white);
fr.add(scientificKey[4]);
scientificKey[5] = new Button("+");
scientificKey[5].addActionListener(this);
scientificKey[5].setBounds(120,180,BWIDTH,BHEIGHT);
scientificKey[5].setBackground(Color.white);
fr.add(scientificKey[5]);
scientificKey[6] = new Button("-");
scientificKey[6].addActionListener(this);
scientificKey[6].setBounds(120,210,BWIDTH,BHEIGHT);
scientificKey[6].setBackground(Color.white);
fr.add(scientificKey[6]);
scientificKey[7] = new Button("1/x");
scientificKey[7].addActionListener(this);
scientificKey[7].setBounds(150,120,BWIDTH,BHEIGHT);
scientificKey[7].setBackground(Color.white);
fr.add(scientificKey[7]);
scientificKey[8] = new Button("x^n");
scientificKey[8].addActionListener(this);
scientificKey[8].setBounds(150,150,BWIDTH,BHEIGHT);
scientificKey[8].setBackground(Color.white);
fr.add(scientificKey[8]);
scientificKey[9] = new Button("+/-");
scientificKey[9].addActionListener(this);
scientificKey[9].setBounds(150,180,BWIDTH,BHEIGHT);
scientificKey[9].setBackground(Color.white);
fr.add(scientificKey[9]);
//factorial
scientificKey[10] = new Button("x!");
scientificKey[10].addActionListener(this);
scientificKey[10].setBounds(150,210,BWIDTH,BHEIGHT);
scientificKey[10].setBackground(Color.white);
fr.add(scientificKey[10]);
// CA
clearAll = new Button("CA");
clearAll.addActionListener(this);
clearAll.setBounds(240, 120, BWIDTH+30, BHEIGHT);
clearAll.setBackground(Color.white);
fr.add(clearAll);
// CE
clearEntry = new Button("CE");
clearEntry.addActionListener(this);
clearEntry.setBounds(180, 120, BWIDTH+30, BHEIGHT);
clearEntry.setBackground(Color.white);
fr.add(clearEntry);
//round
round = new Button("Round");
round.addActionListener(this);
round.setBounds(30, 240,BWIDTH+20, BHEIGHT);
round.setBackground(Color.white);
fr.add(round);
// set display area
display = new TextField("0");
display.setBounds(30,90,150,20);
display.setBackground(Color.white);
// Sine
scientificKey[11] = new Button("Sin");
scientificKey[11].addActionListener(this);
scientificKey[11].setBounds(180, 150, BWIDTH + 10, BHEIGHT);
scientificKey[11].setVisible(true);
scientificKey[11].setBackground(Color.white);
fr.add(scientificKey[11]);
// cosine
scientificKey[12] = new Button("Cos");
scientificKey[12].addActionListener(this);
scientificKey[12].setBounds(180, 180, BWIDTH + 10, BHEIGHT);
scientificKey[12].setBackground(Color.white);
scientificKey[12].setVisible(true);
fr.add(scientificKey[12]);
// Tan
scientificKey[13] = new Button("Tan");
scientificKey[13].addActionListener(this);
scientificKey[13].setBounds(180, 210, BWIDTH + 10, BHEIGHT);
scientificKey[13].setBackground(Color.white);
scientificKey[13].setVisible(true);
fr.add(scientificKey[13]);
// PI
scientificKey[14] = new Button("Pi");
scientificKey[14].addActionListener(this);
scientificKey[14].setBounds(80, 240, BWIDTH + 20, BHEIGHT);
scientificKey[14].setBackground(Color.white);
scientificKey[14].setVisible(true);
fr.add(scientificKey[14]);
// aSine
scientificKey[15] = new Button("aSin");
scientificKey[15].addActionListener(this);
scientificKey[15].setBounds(220, 150, BWIDTH + 10, BHEIGHT);
scientificKey[15].setBackground(Color.white);
scientificKey[15].setVisible(true);
fr.add(scientificKey[15]);
// aCos
scientificKey[16] = new Button("aCos");
scientificKey[16].addActionListener(this);
scientificKey[16].setBounds(220, 180, BWIDTH + 10, BHEIGHT);
scientificKey[16].setBackground(Color.white);
scientificKey[16].setVisible(true);
fr.add(scientificKey[16]);
// aTan
scientificKey[17] = new Button("aTan");
scientificKey[17].addActionListener(this);
scientificKey[17].setBounds(220, 210, BWIDTH + 10, BHEIGHT);
scientificKey[17].setBackground(Color.white);
scientificKey[17].setVisible(true);
fr.add(scientificKey[17]);
// E
scientificKey[18] = new Button("E");
scientificKey[18].addActionListener(this);
scientificKey[18].setBounds(130, 240, BWIDTH +20, BHEIGHT);
scientificKey[18].setBackground(Color.white);
scientificKey[18].setVisible(true);
fr.add(scientificKey[18]);
// to degrees
scientificKey[19] = new Button("toDeg");
scientificKey[19].addActionListener(this);
scientificKey[19].setBounds(180, 240, BWIDTH + 30, BHEIGHT);
scientificKey[19].setBackground(Color.white);
scientificKey[19].setVisible(true);
fr.add(scientificKey[19]);
// to radians
scientificKey[20] = new Button("toRad");
scientificKey[20].addActionListener(this);
scientificKey[20].setBounds(240, 240, BWIDTH + 30, BHEIGHT);
scientificKey[20].setBackground(Color.white);
scientificKey[20].setVisible(true);
fr.add(scientificKey[20]);
//log
scientificKey[21] = new Button("Log");
scientificKey[21].addActionListener(this);
scientificKey[21].setBounds(260, 210, BWIDTH + 10, BHEIGHT);
scientificKey[21].setBackground(Color.white);
scientificKey[21].setVisible(true);
fr.add(scientificKey[21]);
//exp
scientificKey[22] = new Button("Exp");
scientificKey[22].addActionListener(this);
scientificKey[22].setBounds(260,150, BWIDTH + 10, BHEIGHT);
scientificKey[22].setBackground(Color.white);
scientificKey[22].setVisible(true);
fr.add(scientificKey[22]);
//sqrt
scientificKey[23] = new Button("Sqrt");
scientificKey[23].addActionListener(this);
scientificKey[23].setBounds(260, 180, BWIDTH + 10, BHEIGHT);
scientificKey[23].setBackground(Color.white);
scientificKey[23].setVisible(true);
fr.add(scientificKey[23]);
cbg = new CheckboxGroup();
degrees = new Checkbox("Degrees", cbg, false);
degrees.setForeground(Color.white);
radians = new Checkbox("Radians", cbg, false);
radians.setForeground(Color.white);
degrees.addItemListener(this);
radians.addItemListener(this);
degrees.setBounds(185, 75, 3 * BWIDTH, BHEIGHT);
radians.setBounds(185, 95, 3 * BWIDTH, BHEIGHT);
degrees.setVisible(true);
radians.setVisible(true);
fr.add(degrees);
fr.add(radians);
fr.add(display);
fr.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String buttonText = ae.getActionCommand();
double displayNumber =
Double.valueOf(display.getText()).doubleValue();
if((buttonText.charAt(0) >= '0') & (buttonText.charAt(0) <= '9'))
{
if(decimalPointPressed)
{
for (int i=1;i <=decimalPlaces; ++i)
currentNumber *= 10;
currentNumber +=(int)buttonText.charAt(0)- (int)'0';
for (int i=1;i <=decimalPlaces; ++i)
{
currentNumber /=10;
}
++decimalPlaces;
display.setText(Double.toString(currentNumber));
}
else if (roundButtonPressed)
{
int decPlaces = (int)buttonText.charAt(0) - (int)'0';
for (int i=0; i< decPlaces; ++i)
displayNumber *=10;
displayNumber = Math.round(displayNumber);
for (int i = 0; i < decPlaces; ++i)
{
displayNumber /=10;
}
display.setText(Double.toString(displayNumber));
roundButtonPressed = true;
}
else
{
currentNumber = currentNumber * 10 +
(int)buttonText.charAt(0)-(int)'0';
display.setText(Integer.toString((int)currentNumber));
}
}
if(buttonText == "+")
{
addButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "-")
{
subtractButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "/")
{
divideButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "*")
{
multiplyButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "1/x")
{
display.setText(reciprocal(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "+/-")
{
display.setText(changeSign(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "x!")
{
display.setText(factorial(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "x^n")
{
powerButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "Sin")
{
if (degrees.getState())
display.setText(Double.toString(Math.sin(Math.PI *
displayNumber/180)));
else
{
display.setText(Double.toString(Math.sin(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "Cos")
{
if (degrees.getState())
display.setText(Double.toString(Math.cos(Math.PI *
displayNumber/180)));
else
{
display.setText(Double.toString(Math.cos(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "Tan")
{
if (degrees.getState())
display.setText(Double.toString(Math.tan(Math.PI *
displayNumber/180)));
else
{
display.setText(Double.toString(Math.tan(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "aSin")
{
if (degrees.getState())
display.setText(Double.toString(Math.asin(displayNumber)* 180/Math.PI ));
else
{
display.setText(Double.toString(Math.asin(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "aCos")
{
if (degrees.getState())
display.setText(Double.toString(Math.acos(displayNumber)* 180/Math.PI ));
else
{
display.setText(Double.toString(Math.acos(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "aTan")
{
if (degrees.getState())
display.setText(Double.toString(Math.atan(displayNumber)* 180/Math.PI ));
else
{
display.setText(Double.toString(Math.atan(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "toDeg")
display.setText(Double.toString(Math.toDegrees(displayNumber)));
if (buttonText == "toRad")
display.setText(Double.toString(Math.toRadians(displayNumber)));
if (buttonText == "Log")
display.setText(Double.toString(Math.log(displayNumber)));
if (buttonText == "Exp")
display.setText(Double.toString(Math.exp(displayNumber)));
if (buttonText == "Sqrt")
display.setText(Double.toString(Math.sqrt(displayNumber)));
if (buttonText == "Pi")
{
display.setText(Double.toString(Math.PI));
currentNumber =0;
decimalPointPressed = false;
}
if (buttonText == "Round")
{
roundButtonPressed = true;
display.setText(Double.toString(Math.round(displayNumber)));
}
if (buttonText == ".")
{
String displayedNumber = display.getText();
boolean decimalPointFound = false;
int i;
decimalPointPressed = true;
for (i =0; i < displayedNumber.length(); ++i)
{
if(displayedNumber.charAt(i) == '.')
{
decimalPointFound = true;
continue;
}
}
if (!decimalPointFound)
decimalPlaces = 1;
}
if(buttonText == "CA")
{
resetAllButtons();
display.setText("0");
currentNumber = 0;
}
if (buttonText == "CE")
{
display.setText("0");
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "E")
{
display.setText(Double.toString(Math.E));
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "=")
{
currentNumber = 0;
if(addButtonPressed)
display.setText(Double.toString(initialNumber +
displayNumber));
if(subtractButtonPressed)
display.setText(Double.toString(initialNumber -
displayNumber));
if (divideButtonPressed)
{
if(displayNumber == 0)
{
MessageBox mb = new MessageBox ( fr, "Error ",
true, "Cannot divide by zero.");
mb.show();
}
else
display.setText(Double.toString(initialNumber/displayNumber));
}
if(multiplyButtonPressed)
display.setText(Double.toString(initialNumber
*displayNumber));
if (powerButtonPressed)
display.setText(power(initialNumber, displayNumber));
resetAllButtons();
}
if (buttonText == "Author")
{
MessageBox mb = new MessageBox ( fr, "Calculator ver 1.0 beta ",
true, " Author: MscIT Group 2");
mb.show();
}
}
public void itemStateChanged(ItemEvent ie)
{
if (scientificKey[0].isVisible())
{
for (int i=0; i < 8; ++i)
scientificKey[i].setVisible(false);
radians.setVisible(false);
degrees.setVisible(false);
}
if (!scientificKey[0].isVisible())
{
for (int i=0; i < 10; ++i)
scientificKey[i].setVisible(true);
radians.setVisible(true);
degrees.setVisible(true);
}
}
public void resetAllButtons()
{
addButtonPressed = false;
subtractButtonPressed = false;
multiplyButtonPressed = false;
divideButtonPressed = false;
decimalPointPressed = false;
powerButtonPressed = false;
roundButtonPressed = false;
logButtonPressed = false;
expButtonPressed = false;
sqrtButtonPressed = false;
}
public String factorial(double num)
{
int theNum = (int)num;
if (theNum < 1)
{
MessageBox mb = new MessageBox (fr, "Facorial Error",
true,"Cannot find the factorial of numbers less than 1.");
mb.show();
return ("0");
}
else
{
for (int i=(theNum -1); i > 1; --i)
theNum *= i;
return Integer.toString(theNum);
}
}
public String reciprocal(double num)
{
if (num ==0)
{
MessageBox mb = new MessageBox(fr,"Reciprocal Error",
true,"Cannot find the reciprocal of 0");
mb.show();
}
else
num = 1/num;
return Double.toString(num);
}
public String power (double base, double index)
{
return Double.toString(Math.pow(base, index));
}
public String changeSign(double num)
{
return Double.toString(-num);
}
}
class MessageBox extends Dialog implements ActionListener
{
Button ok;
MessageBox(Frame f, String title, boolean mode, String message)
{
super(f, title, mode);
Panel centrePanel = new Panel();
Label lbl = new Label(message);
centrePanel.add(lbl);
add(centrePanel, "Center");
Panel southPanel = new Panel();
ok = new Button ("OK");
ok.addActionListener(this);
southPanel.add(ok);
add(southPanel, "South");
pack();
addWindowListener (new WindowAdapter()
{
public void windowClosing (WindowEvent we)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
}