-
Notifications
You must be signed in to change notification settings - Fork 6
/
CalculatorFunctions.java
193 lines (149 loc) · 4.56 KB
/
CalculatorFunctions.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
public class CalculatorFunctions extends JPanel
{
private JTextField number1TextField;
private JTextField number2TextField;
private JLabel solutionLabel;
public CalculatorFunctions()
{
super(new GridLayout(3,2));
number1TextField = new JTextField(10);
number2TextField = new JTextField(10);
solutionLabel = new JLabel();
add(new JLabel("Number 1:"));
add(number1TextField);
add(new JLabel("Number 2:"));
add(number2TextField);
add(new JLabel("Solution:"));
add(solutionLabel);
}
public void add()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
int solution = number1 + number2;
solutionLabel.setText("" + solution);
}
public void subtract()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
int solution = number1 - number2;
solutionLabel.setText("" + solution);
}
public void multiply()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
int solution = number1 * number2;
solutionLabel.setText("" + solution);
}
public void divide()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
double solution = (double) number1 / number2;
solutionLabel.setText("" + solution);
}
public void mod()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
int solution = number1%number2;
solutionLabel.setText("" + solution);
}
public void power()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
double solution = Math.pow(number1, number2);
solutionLabel.setText("" + solution);
}
public void squareRoot()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = Math.sqrt(number1);
solutionLabel.setText("" + solution);
}
public void log()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
double solution = Math.log(number1)/Math.log(number2);
solutionLabel.setText("" + solution);
}
public void ln()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = Math.log(number1);
solutionLabel.setText("" + solution);
}
public void sin()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = (double) Math.sin(number1);
solutionLabel.setText("" + solution);
}
public void cos()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = (double) Math.cos(number1);
solutionLabel.setText("" + solution);
}
public void tan()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = (double) Math.tan(number1);
solutionLabel.setText("" + solution);
}
public void cot()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = (double) 1/Math.tan(number1);
solutionLabel.setText("" + solution);
}
public void sec()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = (double) 1/Math.cos(number1);
solutionLabel.setText("" + solution);
}
public void csc()
{
int number1 = Integer.parseInt(number1TextField.getText());
double solution = (double) 1/Math.sin(number1);
solutionLabel.setText("" + solution);
}
public static long factorialCalculation(int n)
{
if(n < 0)
return (Long) null;
if(n == 0)
return (long) 1;
if(n > 0)
return n*factorialCalculation(n-1);
return 1;
}
public void factorial()
{
int number1 = Integer.parseInt(number1TextField.getText());
long solution = factorialCalculation(number1);
solutionLabel.setText("" + solution);
}
public void nPr()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
long solution = factorialCalculation(number1)/factorialCalculation(number1 - number2);
solutionLabel.setText("" + solution);
}
public void nCr()
{
int number1 = Integer.parseInt(number1TextField.getText());
int number2 = Integer.parseInt(number2TextField.getText());
long solution = factorialCalculation(number1)/(factorialCalculation(number1 - number2)*factorialCalculation(number2));
solutionLabel.setText("" + solution);
}
}