-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
51 lines (43 loc) · 1.17 KB
/
Calculator.java
File metadata and controls
51 lines (43 loc) · 1.17 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
// Calculator.java
public class Calculator {
// Basic Arithmetic Operations
public double add(double a, double b) {
return a + b;
}
public double subtract(double a, double b) {
return a - b;
}
public double multiply(double a, double b) {
return a * b;
}
public double divide(double a, double b) {
return (b != 0) ? a / b : Double.NaN;
}
// Fibonacci Calculation (Recursive)
public int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
// Array Operations
public double sumArray(double[] array) {
double sum = 0;
for (double num : array) {
sum += num;
}
return sum;
}
public double mean(double[] array) {
return sumArray(array) / array.length;
}
public double variance(double[] array) {
double mean = mean(array);
double sum = 0;
for (double num : array) {
sum += Math.pow(num - mean, 2);
}
return sum / array.length;
}
public double standardDeviation(double[] array) {
return Math.sqrt(variance(array));
}
}