-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
executable file
·77 lines (75 loc) · 2.04 KB
/
Calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
<applet code="Calculator.class" width=240 height=200>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class Calculator extends Applet implements ActionListener {
Button numbers[],functions[];
TextField txt;
boolean to_clear=false;
public void init(){
Panel input = new Panel();
Panel numberkeys = new Panel();
Panel functionkeys = new Panel();
setLayout (new BorderLayout ());
numbers=new Button[10];
functions=new Button[6];
numberkeys.setLayout(new GridLayout(4,3));
functionkeys.setLayout(new GridLayout(3,2,3,3));
for(int i=1;i<10;i++){
numbers[i]=new Button(""+i);
numberkeys.add(numbers[i]);
numbers[i].addActionListener(this);
}
Dimension keypadsize = new Dimension(150,150);
numberkeys.add(new Button(""));
numberkeys.setPreferredSize(keypadsize);
numbers[0]=new Button("0");
numbers[0].addActionListener(this);
numberkeys.add(numbers[0]);
numberkeys.add(new Button(""));
txt = new TextField(20);
input.add(txt);
String[] fns = {" + "," - "," / "," * "," % "," = "};
for (int i=0; i<6;i++) {
functions[i] = new Button(fns[i]);
functionkeys.add(functions[i]);
functions[i].addActionListener(this);
}
add(input,BorderLayout.PAGE_START);
add(numberkeys,BorderLayout.LINE_START);
add(functionkeys,BorderLayout.LINE_END);
}
public void actionPerformed(ActionEvent evt) {
Button b = (Button)evt.getSource();
if(b==functions[5])
{
String expr = txt.getText();
txt.setText(this.evaluate(expr));
}
else{
if(to_clear){
txt.setText("");
to_clear=false;
}
txt.setText(txt.getText()+b.getLabel());
}
}
private String evaluate(String expr){
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String res="";
try{
res=(engine.eval(expr)).toString();
}
catch(Exception e){
res="Invalid Expression";
}
to_clear=true;
return res;
}
}