-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeanAndVariance.java
More file actions
116 lines (74 loc) · 2.79 KB
/
MeanAndVariance.java
File metadata and controls
116 lines (74 loc) · 2.79 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
import java.util.*;
public class MeanAndVariance{
public static double calculateMean(double[] arguments){
double sum = 0;
for(int p = 0; p < arguments.length; p++){ //For the amount of numbers entered used
sum = sum + arguments[p]; //Keep adding each number to the previous sum
}
double average = sum / arguments.length;
return average;
}
public static double calculateVariance(double[] args){
int length = args.length;
double variance = 0;
double varianceSum = 0;
for (double s: args){ //Enhanced for loop to count to the amount of numbers entered
variance += (s - calculateMean(args)) * (s - calculateMean(args)); //New variance is calculated for each number entered using standard formulae
}
varianceSum = variance / length;
return varianceSum;
}
public static double[] getArguments(int nargs, Scanner inputStream){
inputStream = new Scanner(System.in); //inputStream is the reference to the Scanner object
double[] arguments = new double[nargs];
System.out.println("Enter the " + nargs + " numbers now");
for(int i = 0; i < arguments.length; i++){
arguments[i] = inputStream.nextDouble();
}
return arguments;
}
public static void main(String args[]){
Scanner input = new Scanner(System.in);
try{
System.out.println("How many numbers would you like to use?");
int numbersUsed = input.nextInt();
if(numbersUsed >= 2){
double[] result = getArguments(numbersUsed,input); //Store the arguments they enter in an array
System.out.println("Numbers entered are:");
for(int k = 0; k < result.length; k++){
System.out.print(result[k] + "\n");
}
System.out.println("Mean value of numbers is: " + calculateMean(result));
System.out.println("Variance of numbers is: " + calculateVariance(result));
}
else{
System.out.println("You must enter two or more numbers");
}
/*System.out.println("Would you like to calculate the mean [1] or the variance [2]? ");
int answer = input.nextInt();
switch(answer){
case(1):
System.out.println("Numbers entered are:");
for(int k = 0; k < result.length; k++){
System.out.print(result[k] + "\n");
}
System.out.println("Mean value of numbers is: " + calculateMean(result));
break;
case(2):
System.out.println("Numbers entered are:");
for(int k = 0; k < result.length; k++){
System.out.print(result[k] + "\n");
}
System.out.println("Variance of numbers is: " + calculateVariance(result));
break;
default:
System.out.println("That is not possible");
}
*/
}
catch(Exception e){
System.err.printf("Exception: %s\n", e);
System.err.printf("That is not possible");
}
}
}