-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStatistics2.java
More file actions
163 lines (136 loc) · 3.74 KB
/
Copy pathTestStatistics2.java
File metadata and controls
163 lines (136 loc) · 3.74 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// TestStatistics.java
/*
* This program inputs decimal numbers as a String separated by commas/spaces,
* then it separates them into tokens one at a time.
* It converts each token received (as String) into a double
* and stores it in the corresponding array data element
**/
import java.text.DecimalFormat;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class TestStatistics {
public static void main(String[] args) {
String in, out;
in = JOptionPane.showInputDialog("Enter data seprated by comma");
String delim = ",";
StringTokenizer stk = new StringTokenizer(in, delim);
int itemCount = stk.countTokens();
double [] x = new double [itemCount];
for(int i=0; i<x.length; i++) {
String token= stk.nextToken().trim();
x[i]=Double.parseDouble(token);
}
Statistics stat = new Statistics (x);
double []origData = stat.getOrigData();
double []sortedData = stat.getSortedData();
double min = stat.findMin();
double max = stat.findMax();
double mean = stat.findMean();
double median = stat.findMedian();
double min2 = Statistics.computeMin(x);
double max2 = Statistics.computeMax(x);
double mean2 = Statistics.computeMean(x);
double [] sortedData2 = Statistics.computeSortedData(x);
double objectCount = Statistics.count;
String pattern = ".0000";
// Build output
DecimalFormat df = new DecimalFormat(pattern);
out="";
out+="Original Data:"+"\n";
out+=origData[0];
for(int i=1; i<origData.length;i++) {
out+=", "+origData[i];
}
out+="\n";
out+="Sorted Data:"+"\n";
out+=sortedData[0];
for(int i=1; i<sortedData.length;i++) {
out+=", "+sortedData[i];
}
out+="\n";
out+="Min:"+df.format(min2)+"\n";
out+="Max:"+df.format(max2)+"\n";
out+="Mean:"+df.format(mean2)+"\n";
out+="Median:"+df.format(median)+"\n";
out+="The Total Number of Statistics objects created during execution: " + objectCount;
JOptionPane.showMessageDialog(null, out);
// display objectCount
}
}
// Statistics
import java.util.Arrays;
public class Statistics {
private double [] data;
private static double [] sdata;
public static int count = 0;
public Statistics(double[] d) {
super();
data = d.clone();
sdata = d.clone();
Arrays.sort(sdata);
Statistics.count++;
}
public double findMin() {
double min;
min = sdata[0];
return min;
}
public double findMax() {
double max;
max = sdata[sdata.length-1];
return max;
}
public static double findMean() {
double mean, sum = 0;
for(int i=0; i<sdata.length; i++) {
sum = sum+sdata[i];
}
mean = sum/sdata.length;
return mean;
}
public double findMedian() {
int loIndexMid, hiIndexMid, indexMid;
double median;
if ((sdata.length%2) == 0) { // Even case
hiIndexMid=sdata.length/2;
loIndexMid=hiIndexMid-1;
median = (sdata[loIndexMid]+sdata[hiIndexMid])/2.0;
}
else {
indexMid = sdata.length/2;
median = sdata[indexMid];
}
return median;
}
public double [] getOrigData() {
return data.clone();
}
public double [] getSortedData() {
return sdata.clone();
}
public static double computeMin(double []d) {
Statistics st = new Statistics(d);
double min = st.findMin();
return min;
}
public static double computeMax(double []d) {
Statistics st = new Statistics(d);
double max = st.findMax();
return max;
}
public static double computeMean(double []d) {
Statistics st = new Statistics(d);
double mean = st.findMean();
return mean;
}
public static double [] computeSortedData(double []d) {
Statistics st = new Statistics(d);
double [] sortedData= st.getSortedData();
return sortedData;
}
public static double [] computeOrigData(double []d) {
Statistics st = new Statistics(d);
double [] origData= st.getOrigData();
return origData;
}
}