-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStats.java
More file actions
46 lines (40 loc) · 974 Bytes
/
Stats.java
File metadata and controls
46 lines (40 loc) · 974 Bytes
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
/**
* Homework 4
* Maricel Vicente bvicente@syr.edu
*/
import java.util.ArrayList;
public class Stats {
public static double mean(ArrayList<Double> data)
{
// check if ArrayList is empty, if not compute the average
if (!(data.isEmpty() && data.size() > 0)) {
double total = 0;
for(int i=0; i< data.size(); i++)
{
total = total + data.get(i);
}
double average = total / data.size();
return average;
}
// Return NAN if array is empty
else {
return Double.NaN;
}
}
public static double stdDev(ArrayList<Double> data)
{
// check if array is empty and has more than 1 element
if (!(data.isEmpty() && data.size() > 0)) {
double std = 0;
for (int i = 0; i < data.size(); i++)
{
std = std + Math.pow((data.get(i) - mean(data)), 2) / data.size();
}
double stDev = Math.sqrt(std);
return stDev;
}
else {
return Double.NaN;
}
}
}