-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.java
More file actions
125 lines (116 loc) · 5.7 KB
/
Menu.java
File metadata and controls
125 lines (116 loc) · 5.7 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import javax.swing.*;
import java.awt.event.ActionEvent;
public class Menu extends JFrame {
private JPanel panel1;
private JTextField input1;
private JTextField input2;
private JButton ENTERButton;
private JComboBox functionBox;
private JLabel output;
private double equals = 0;
public Menu() {
setContentPane(panel1);
setBounds(100,100,500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Zach's Calculator");
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
System.out.println("Nimbus look and feel unavailable");
}
functionBox.addActionListener((ActionEvent e) -> {
switch(functionBox.getSelectedIndex()) {
case 0: case 1: case 2: case 3: case 4:
input2.setEditable(true);
break;
default:
input2.setEditable(false);
break;
}
});
ENTERButton.addActionListener((ActionEvent e) -> {
try {
Double.parseDouble(input1.getText()); // Check if input is numerical
output.setText(output.getText() + input1.getText());
switch(functionBox.getSelectedIndex()) {
case 0:
Double.parseDouble(input2.getText()); // Check if input is numerical
output.setText(input1.getText() + " + " + input2.getText() + " = ");
equals = Double.parseDouble(input1.getText()) + Double.parseDouble(input2.getText());
break;
case 1:
Double.parseDouble(input2.getText()); // Check if input is numerical
output.setText(input1.getText() + " - " + input2.getText() + " = ");
equals = Double.parseDouble(input1.getText()) - Double.parseDouble(input2.getText());
break;
case 2:
Double.parseDouble(input2.getText()); // Check if input is numerical
output.setText(input1.getText() + " × " + input2.getText() + " = ");
equals = Double.parseDouble(input1.getText()) * Double.parseDouble(input2.getText());
break;
case 3:
Double.parseDouble(input2.getText()); // Check if input is numerical
output.setText(input1.getText() + " ÷ " + input2.getText() + " = ");
equals = Double.parseDouble(input1.getText()) / Double.parseDouble(input2.getText());
break;
case 4:
Double.parseDouble(input2.getText()); // Check if input is numerical
output.setText(input1.getText() + "^" + input2.getText() + " = ");
equals = Math.pow(Double.parseDouble(input1.getText()), Double.parseDouble(input2.getText()));
break;
case 5:
output.setText("sin(" + input1.getText() + ") = ");
equals = Math.sin(Double.parseDouble(input1.getText()) * Math.PI/180);
break;
case 6:
output.setText("cos(" + input1.getText() + ") = ");
equals = Math.cos(Double.parseDouble(input1.getText()) * Math.PI/180);
break;
case 7:
output.setText("tan(" + input1.getText() + ") = ");
equals = Math.tan(Double.parseDouble(input1.getText()) * Math.PI/180);
break;
case 8:
output.setText("arcsin(" + input1.getText() + ") = ");
equals = Math.asin(Double.parseDouble(input1.getText())) * 180/Math.PI;
break;
case 9:
output.setText("arccos(" + input1.getText() + ") = ");
equals = Math.acos(Double.parseDouble(input1.getText())) * 180/Math.PI;
break;
case 10:
output.setText("arctan(" + input1.getText() + ") = ");
equals = Math.atan(Double.parseDouble(input1.getText())) * 180/Math.PI;
break;
case 11:
output.setText("log(" + input1.getText() + ") = ");
equals = Math.log10(Double.parseDouble(input1.getText()));
break;
case 12:
output.setText("ln(" + input1.getText() + ") = ");
equals = Math.log(Double.parseDouble(input1.getText()));
break;
default:
output.setText("Error");
break;
}
// Round to ten decimal points
equals *= Math.pow(10, 10);
equals = Math.round(equals);
equals /= Math.pow(10, 10);
// Output final calculation
output.setText(output.getText() + equals);
input1.setText(String.valueOf(equals));
input2.setText("");
} catch(NumberFormatException f) {
output.setText("Error: Only numbers allowed!");
}
});
setVisible(false);
}
}