-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.java
122 lines (119 loc) · 2.55 KB
/
Stats.java
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
import java.util.Random;
public class Stats extends Main
{
int sampleSize;
double count;
double ave;
double sum;
int min;
int max;
int mode;
int evenCount;
int oddCount;
int countMatching;
int counter;
int match;
//Method: return the sample set's max value
public int getMax(int sampleSize, int[] data)
{
max = data[0];
for(int i = 1; i < sampleSize; i++)
{
if (data[i] > max)
max = data[i];
}
return max;
}
//Method: return the min value
public int getMin(int sampleSize, int[] data)
{
min = data[0];
for(int i = 1; i < sampleSize; i++)
{
if (data[i] < min)
min = data[i];
}
return min;
}
//Method: return the average value
public double getAve(int sampleSize, int[] data)
{
count = sampleSize;
sum = 0;
for(int i = 0; i < sampleSize; i++)
{
sum = sum + data[i];
}
ave = sum / count;
return ave;
}
//Method: return the mode; in case of a tie, choose the smallest value
public int getMode(int sampleSize, int[] data)
{
int popularity1 = 0;
int popularity2 = 0;
int array_item;
for(int i = 0; i < sampleSize; i++)
{
array_item = data[i];
for(int j = 0; j < sampleSize; j++)
{
if(array_item == data[j])
popularity1++;
}
if(popularity1 >= popularity2)
{
mode = array_item;
popularity2 = popularity1;
}
}
return mode;
}
//Method: return the count of even numbers
public int getEven(int sampleSize, int[] data)
{
int evenCount = 0;
for (int i = 0; i < sampleSize; i++)
{
if (data[i] % 2 == 0)
evenCount++;
}
return evenCount;
}
//Method: return the count of odd numbers
public int getOdd(int sampleSize, int[] data)
{
int oddCount = 0;
for (int i = 0; i < sampleSize; i++)
{
if (data[i] % 2 != 0)
oddCount++;
}
return oddCount;
}
//Display all numbers, formatted in columns (hint: pg. 158)
public void displaySampleSet(int sampleSize, int[] data)
{
int counter = 0;
for(int i = 0; i < sampleSize; i++)
{
System.out.print(" ");
System.out.print(data[i]);
counter++;
if (counter % 20 == 0)
System.out.println();
}
}
//Return the count of numbers in the sample set that match the input parameter
public int countMatching(int match, int sampleSize, int []data)
{
for(int i = 0; i < sampleSize; i++)
{
if (match == data[i])
counter++;
}
return counter;
}
//Create a list of private variable(s) that belong to the Stats class
private int[] data;
}