-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatDriver.java
More file actions
61 lines (42 loc) · 1.39 KB
/
StatDriver.java
File metadata and controls
61 lines (42 loc) · 1.39 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
/**
* Homework 4
* Maricel Vicente bvicente@syr.edu
*/
import java.util.Scanner;
import java.util.ArrayList;
public class StatDriver {
public static void main(String[] args) {
// Read a series of numbers from System.in, create an Arraylist containing those numbers, call the mean and
// standard deviation functions of the Stats class, and then report the results
Scanner scan = new Scanner(System.in);
// Create an ArrayList of doubles
ArrayList<Double> data = new ArrayList<Double>();
// loop will read input values as doubles and store them in data Arraylist
while (scan.hasNextDouble())
{
data.add(scan.nextDouble());
}
// check if user asked for mean or standard deviation in the command line
if (args.length > 0 && args != null)
{
if (args[0].equals("std"))
{
String stdStr;
stdStr = String.format("StdDev: %.2f\n", Stats.stdDev(data));
System.out.println(stdStr);
}
else {
String meanStr;
meanStr = String.format("Mean: %.2f\n", Stats.mean(data));
System.out.println(meanStr);
}
}
// if user does not indicate mean or std, then calculate mean
else
{
String meanStr;
meanStr = String.format("Mean: %.2f\n", Stats.mean(data));
System.out.println(meanStr);
}
}
}