Calculator
Calculator
*;
import java.awt.event.*;
import javax.swing.*;
public Calculator() {
textfield = new JTextField("", 10);
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(INPUT_FONT);
textfield.setPreferredSize( new Dimension( 8, 150 ) );
textfield.setBackground(Color.decode("#353C51"));
textfield.setForeground(Color.WHITE);
textfield.setOpaque(true);
ActionListener numberListener = new NumberListener();
String buttonOrder = "123456789.0-";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
buttonPanel.setBackground(Color.decode("#353C51"));
//buttonPanel.setForeground(Color.WHITE);
buttonPanel.setOpaque(true);
}
else
if (e.getActionCommand().equals("cos"))
{
textfield.setText("" +
Math.cos(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("log"))
{
textfield.setText("" +
Math.log(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("√"))
{
textfield.setText("" +
Math.sqrt(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("⌈x⌉"))
{
textfield.setText("" +
Math.ceil(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("⌊x⌋"))
{
textfield.setText("" +
Math.floor(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("abs"))
{
textfield.setText("" +
Math.abs(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("³√"))
{
textfield.setText("" +
Math.cbrt(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("rnd"))
{
textfield.setText("" +
Math.round(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("exp"))
{
textfield.setText("" +
Math.exp(Double.valueOf(displayText).doubleValue()));
}
else
if (e.getActionCommand().equals("x^2"))
{
textfield.setText("" +
Math.pow(Double.valueOf(displayText).doubleValue(), 2));
}
else
if (e.getActionCommand().equals("x^3"))
{
textfield.setText("" +
Math.pow(Double.valueOf(displayText).doubleValue(), 3));
else
if (e.getActionCommand().equals("N!"))
{
String input= displayText;
long factorial=1;
for(long j=Long.parseLong(input);j>=1;j--)
{
factorial=factorial*j;
}
textfield.setText("" + factorial);
else
if (e.getActionCommand().equals("clr"))
{
textfield.setText("");
}
else
{
if (number)
{
action();
textfield.setText("");
}
else
{
number = true;
if (equalOp.equals("="))
{
op.setTotal(displayText);
}else if (equalOp.equals("+"))
{
op.add(displayText);
}
else if (equalOp.equals("-"))
{
op.subtract(displayText);
}
else if (equalOp.equals("*"))
{
op.multiply(displayText);
}
else if (equalOp.equals("/"))
{
op.divide(displayText);
}
else if (equalOp.equals("%"))
{
op.mod(displayText);
}
textfield.setText("" + op.getTotalString());
equalOp = e.getActionCommand();
}
}
}
}
class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String digit = event.getActionCommand();
if (number) {
textfield.setText(digit);
number = false;
} else {
textfield.setText(textfield.getText() + digit);
}
}
}
public class CalculatorOp {
private int total;
public CalculatorOp() {
total = 0;
}
public String getTotalString() {
return ""+total;
}
public void setTotal(String n) {
total = convertToNumber(n);
}
public void add(String n) {
total += convertToNumber(n);
}
public void subtract(String n) {
total -= convertToNumber(n);
}
public void multiply(String n) {
total *= convertToNumber(n);
}
public void divide(String n) {
total /= convertToNumber(n);
}
public void mod(String n) {
total %= convertToNumber(n);
}