Im trying to make a calculator and I thought of using the ScriptEngine class to evaluate expressions. So I designed the following code:
CalculatorFrame.java :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class CalculatorFrame extends JFrame {
public CalculatorFrame()
{
setTitle("My Scientific Calculator");
CalculatorPanel calcPanel=new CalculatorPanel();
add(calcPanel);
pack();
}
}
class CalculatorPanel extends JPanel
{
private JTextField display1;
private JTextField display2;
private JPanel buttonPanel;
private JPanel displayPanel;
public CalculatorPanel()
{
ActionListener listener=new CalculatorListener();
ActionListener displayListener=new DisplayListener();
setLayout(new BorderLayout());
//Display
displayPanel=new JPanel();
displayPanel.setLayout(new GridLayout(2,1));
display1=new JTextField("");
displayPanel.add(display1);
display2=new JTextField("");
displayPanel.add(display2);
add(displayPanel,BorderLayout.NORTH);
buttonPanel=new JPanel();
//ButtonLayout
buttonPanel.setLayout(new GridLayout(7,4));
//Button Creation
JLabel empty=new JLabel("");
JLabel empty1=new JLabel("");
buttonPanel.add(empty);
buttonPanel.add(empty1);
JButton AC=new JButton("AC");
AC.addActionListener(displayListener);
buttonPanel.add(AC);
JButton OFF=new JButton("OFF");
OFF.addActionListener(displayListener);
buttonPanel.add(OFF);
JButton log=new JButton("log");
log.addActionListener(listener);
buttonPanel.add(log);
JButton sin=new JButton("sin");
sin.addActionListener(listener);
buttonPanel.add(sin);
JButton tan=new JButton("tan");
tan.addActionListener(listener);
buttonPanel.add(tan);
JButton cos=new JButton("cos");
cos.addActionListener(listener);
buttonPanel.add(cos);
JButton e=new JButton("e");
e.addActionListener(listener);
buttonPanel.add(e);
JButton x_2=new JButton("x^2");
x_2.addActionListener(listener);
buttonPanel.add(x_2);
JButton x_3=new JButton("x^3");
x_3.addActionListener(listener);
buttonPanel.add(x_3);
JButton sqrt=new JButton("sqrt");
sqrt.addActionListener(listener);
buttonPanel.add(sqrt);
JButton seven=new JButton("7");
seven.addActionListener(listener);
buttonPanel.add(seven);
JButton eight=new JButton("8");
eight.addActionListener(listener);
buttonPanel.add(eight);
JButton nine=new JButton("9");
nine.addActionListener(listener);
buttonPanel.add(nine);
JButton add=new JButton("+");
add.addActionListener(listener);
buttonPanel.add(add);
JButton four=new JButton("4");
four.addActionListener(listener);
buttonPanel.add(four);
JButton five=new JButton("5");
five.addActionListener(listener);
buttonPanel.add(five);
JButton six=new JButton("6");
six.addActionListener(listener);
buttonPanel.add(six);
JButton minus=new JButton("-");
minus.addActionListener(listener);
buttonPanel.add(minus);
JButton one=new JButton("1");
one.addActionListener(listener);
buttonPanel.add(one);
JButton two=new JButton("2");
two.addActionListener(listener);
buttonPanel.add(two);
JButton three=new JButton("3");
three.addActionListener(listener);
buttonPanel.add(three);
JButton divide=new JButton("/");
divide.addActionListener(listener);
buttonPanel.add(divide);
JButton zero=new JButton("0");
zero.addActionListener(listener);
buttonPanel.add(zero);
JButton decimal=new JButton(".");
decimal.addActionListener(listener);
buttonPanel.add(decimal);
JButton equal=new JButton("=");
equal.addActionListener(listener);
buttonPanel.add(equal);
JButton multiply=new JButton("*");
multiply.addActionListener(listener);
buttonPanel.add(multiply);
add(buttonPanel,BorderLayout.CENTER);
}
ScriptEngineManager manager=new ScriptEngineManager();
ScriptEngine engine=manager.getEngineByName("Java Script");
private class DisplayListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand();
if(command.equals("AC"))
{
display1.setText("");
display2.setText("");
}
else
{
System.exit(0);
}
}
}
private class CalculatorListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String command=event.getActionCommand();
//double oprOneVal = 0;
//double oprTwoVal=0;
//String operator="";
//boolean operand1=true;
//boolean operand2=false;
if(command.equals("log"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(Math.log(input)));
}
else if(command.equals("sin"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(Math.sin(input)));
}
else if(command.equals("tan"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(Math.tan(input)));
}
else if(command.equals("cos"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(Math.cos(input)));
}
else if(command.equals("e"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(Math.exp((input))));
}
else if(command.equals("x^2"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(input*input));
}
else if(command.equals("x^3"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(input*input*input));
}
else if(command.equals("sqrt"))
{
double input=Double.parseDouble(display1.getText());
display2.setText(Double.toString(Math.sqrt((input))));
}
else if(command.equals("="))
{
String input=display1.getText();
System.out.println(input); //debugging purpose
try {
display2.setText(Double.toString(evaluateExpression(input)));
} catch(Exception e)
{e.printStackTrace();
}
}
else if(command.equals("ANS"))
{
display1.setText(display1.getText()+display2.getText());
}
else
{
display1.setText(display1.getText()+command);
}
}
Double evaluateExpression (String expression) throws Exception
{
System.out.println(expression); //debugging purpose
return (Double) engine.eval(expression); //Throwing Exception
}
}
}
Calculator.java
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Calculator {
public static void main(String arg[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CalculatorFrame calculator=new CalculatorFrame();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculator.setVisible(true);
}
});
}
}
But When I execute it and when I press "8" + "9" and then "="
The string from display1 is getting stored in the String input.
And it is being passed on to evaluateExpression method. But still engine.eval() throws NullPointerException. But Javadoc clearly states that this method throws nullexception iff the argument is null.
Please help me. Am I missing something.
Here is the GUI.
[IMG]http://lulzimg.com/i23/3de3b0.jpg[/IMG]
Please help me. BTW please ignore the operations for log, sin, tan etc. I haven't worked on them yet. Current implementation is logically wrong.