-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStat.java
More file actions
104 lines (95 loc) · 2.86 KB
/
Copy pathStat.java
File metadata and controls
104 lines (95 loc) · 2.86 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
import java.util.Scanner;
class StatCalc {
int n;
int[] ar;
int i = 0;
StatCalc(int nn) {
n = nn;
ar = new int[n];
}
void enter(int num) {
if (i >= n) {
System.out.println("Out of bounds, cannot add more items.");
return;
}
else
{ar[i] = num;}
i++;
}
int getcount() {
return i;
}
int getsum() {
int sum = 0;
for (int j = 0; j <i; j++) {
sum += ar[j];
}
return sum;
}
float getmean() {
int mean = 0;
for (int j = 0; j <i; j++) {
mean += ar[j];
}
return (float) mean / (i + 1);
}
float getStandardDeviation() {
double sum = 0.0;
int array_length = i + 1;
for (int j = 0; j <i; j++) {
sum += ar[j];
}
double mean = sum / array_length;
double standard_deviation = 0.0;
for (int j = 0; j <i; j++) {
standard_deviation += Math.pow(ar[j] - mean, 2);
}
double result = Math.sqrt(standard_deviation / array_length);
return (float) result;
}
}
class Stat {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int len, ch, item;
System.out.println("Enter array size");
len = in.nextInt();
StatCalc ob = new StatCalc(len);
boolean exit = false;
while (!exit) {
System.out.println("\nMenu");
System.out.println("1. Enter an item");
System.out.println("2. Get count");
System.out.println("3. Get sum");
System.out.println("4. Get mean");
System.out.println("5. Get standard deviation");
System.out.println("6. Exit");
System.out.println("Enter your choice:");
ch = in.nextInt();
switch (ch) {
case 1:
System.out.println("Enter an item:");
item = in.nextInt();
ob.enter(item);
break;
case 2:
System.out.println("Length of the array: " + ob.getcount());
break;
case 3:
System.out.println("Sum = " + ob.getsum());
break;
case 4:
System.out.println("Mean = " + ob.getmean());
break;
case 5:
System.out.println("Standard deviation = " + ob.getStandardDeviation());
break;
case 6:
exit = true;
break;
default:
System.out.println("INVALID INPUT");
}
}
}
}