-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfectionTracking.java
More file actions
356 lines (356 loc) · 13.5 KB
/
InfectionTracking.java
File metadata and controls
356 lines (356 loc) · 13.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* This program is used to simulate hypothetical COVID-19 transmission
* simulations as well as collect data regarding how the virus would
* spread if all students went about their lives without taking any
* precautionary measures. This simulation includes the following
* factors: populating lists of who is infected, their names,
* their movement, the world size, how many people they infected,
* the average number of infections, as well as the name of the
* individual with the most infections after a certain number of days
**/
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
/**
* This class contains all of the methods to simulate the hypothetical
* COVID-19 transmission simulation. These methods include a couple
* isValid methods, populateArrays method, updateLocations, updateInfections,
* countInfectionsByStudent, findRNaught, and findSuperSpreader.
**/
public class InfectionTracking{
/**
* This method checks the validity of each value in the different arrays.
* The method checks to see if the inputs for each array are valid or not
* and returns a boolean statement to ensure validity or not.
* The method checks to see if the lengths of each array match each other,
* if any of the input arrays is null, and if the infection array values
* are either a 1 or 0.
*
* @param names - array for student names
* @param locations - array for locations
* @param movements - array for movement between locations
* @param infections - array for infections
* @param worldSize - size of 1-dimensional world
* @param pathToFile - file path
*
**/
public static boolean isValid(String[] names, int[] locations,
int[] movements, int[] infections){
if(names==null||locations==null||movements==null||infections==null){
return false;
}
for(int i=0;i<locations.length;i++){
if(locations[i]<0){
return false;
}
}
if(names.length!=locations.length||locations.length!=movements.length||
movements.length!=infections.length||infections.length!=names.length){
return false;
}
boolean infectionBool = false;
for(int j=0;j<infections.length;j++){
if(infections[j]==0||infections[j]==1){
infectionBool = true;
}else{
infectionBool=false;
}
}
return infectionBool;
}
/**
* This method checks the validity with the introduciton of a new
* parameter: worldSize
* This method makes sure none of the arrays are null, the locations
* array and the movements array have the same length, the worldSize
* is between 0 and worldSize-1
* @param locations - array of locations as integers
* @param movements - array of movements between locations as integers
* @param worldSize - int indicating size of world
* returns boolean checking validity of these tests
**/
public static boolean isValid(int[] locations,int[] movements,
int worldSize){
if(locations==null||movements==null||
locations.length!=movements.length||worldSize<0){
return false;
}
for(int i=0;i<locations.length;i++){
if(locations[i]<0||locations[i]>(worldSize-1)){
return false;
}
}
return true;
}
/**
* This method checks the validity with respect to different
* parameters: locations, worldSize, and infecttions
* This method makes sure none of the arrays are null, the locations
* array and the infections array have the same length, the worldSize
* is between 0 and worldSize-1
* @param locations - array of locations as integers
* @param infections - array of infections either 1 or 0
* @param worldSize - int indicating size of world
* returns boolean checking validity of these tests
**/
public static boolean isValid(int[] locations,
int worldSize,int[] infections){
if(locations==null||infections==null){
return false;
}
if(worldSize<0){
return false;
}
for(int i=0;i<locations.length;i++){
if(locations[i]<0||locations[i]>(worldSize-1)){
return false;
}
}
boolean sameLength = locations.length==infections.length;
for(int j=0;j<infections.length;j++){
if(infections[j]!=0&&infections[j]!=1){
return false;
}
}
return sameLength;
}
/**
* This method is used to populate empty arrays from the text file
* that is imported by this method also. The method imports a file
* and scans through it getting the individual values from it
* and populating arrays according to those values. These arrays
* include names, locations, movements, infections
* The method returns -1 if the arrays are not properly populated
* and returns the max locations value+1 if properly populated
*
* @param pathToFile - file path
* @param names - array of names
* @param locations - array of locations as integers
* @param infections - array of infections either 1 or 0
* @param worldSize - int indicating size of world
* @param movements - array of movements between locations as integers
* returns max locations value+1 if populated correctly
* returns -1 if populated incorrectly
**/
public static int populateArrays(String pathToFile, String[] names,
int[] locations, int[] movements, int[] infections) throws IOException{
if(pathToFile!=null&&isValid(names,locations,movements,infections)){
FileInputStream filePath = new FileInputStream(pathToFile);
Scanner sc = new Scanner(filePath);
int i =0;
while(sc.hasNextLine()){
// goes thru each line in text file splitting strings by the ","
// this gets the individual values to go in each array
String str[]=sc.nextLine().split(",");
String name = str[0];
int location = Integer.parseInt(str[1]);
int movement = Integer.parseInt(str[2]);
int infection = Integer.parseInt(str[3]);
names[i]=name;
locations[i] = location;
movements[i] = movement;
infections[i] = infection;
i=i+1;
}
sc.close();
// loops thru finding max value
int maxVal = locations[0];
for(int j=0;j<locations.length;j++){
if(maxVal<locations[j]){
maxVal = locations[j];
}
}
int max = maxVal+1;
return max;
}else{
return -1;
}
}
/**
* This method updates the locations of the individuals based on their
* movements and updates the current location after the movement. It
* also checks that the movements stay within the worldsize and wraps
* back the location using the modulus operator to ensure the movement
* stays within the worldSize
*
* @param modulo - modulus of the new location divided by the worldSize
* @param worldSize - int indicating size of world
* @param movements - array of movements between locations as integers
* @param locations - array of locations as integers
*
**/
public static void updateLocations(int worldSize, int[] locations,
int[] movements){
if(isValid(locations,movements,worldSize)==true){
for(int i=0;i<locations.length;i++){
// check validity of input
if(locations[i]+movements[i]<0||locations[i]+movements[i]>=worldSize){
int modulo = (locations[i]+movements[i])%worldSize;
// if new location is negative
if(modulo<0){
locations[i]=modulo+worldSize;
// if new location is not neg and > worldSize
}else{
locations[i]=modulo;
}
// if new location is not negative and < worldSize
}else{
locations[i] = locations[i]+movements[i];
}
}
}
}
/**
* This method updates the infections of students who come into contact
* with each other or are in the same location at one point. The method
* takes the locations and worldSize ensureing not to go out of bounds,
* and counds how many individuals a student has infected based upon
* their contact at their shared location. The method creates a new array
* keeping track of all the students and the number of other students
* they infect.
*
* @param modulo - modulus of the new location divided by the worldSize
* @param worldSize - int indicating size of world
* @param infections - array of infections either 1 or 0
* @param locations - array of locations as integers
* returns the newly created array that keeps track of the number of
* students that were infected by the student at the index
*
**/
public static int[] updateInfections(int worldSize, int[] locations,
int[] infections){
if(isValid(locations,worldSize,infections)==true){
int[] numStudentsInfected= new int[infections.length];
// loops thru adding to numStudentsInfected for each student infected
for(int i=0;i<infections.length;i++){
if(infections[i]==1){
for(int j=0;j<locations.length;j++){
if(locations[i]==locations[j]&&infections[j]==0){
numStudentsInfected[i] = numStudentsInfected[i]+1;
}
}
}
}
// loops through changing all the infected to 1's
for(int i=0;i<infections.length;i++){
if(infections[i]==1){
for(int j=0;j<infections.length;j++){
if(locations[i]==locations[j]&&infections[j]==0){
infections[j]=1;
}
}
}
}
return numStudentsInfected;
}
return null;
}
/**
* This method creates a new array called infectionRecord and updates
* the locations of students after their movements each day and
* records the number of people they have infected or got infected in
* the shared location
*
* @param worldSize - int indicating size of world
* @param infections - array of infections either 1 or 0
* @param locations - array of locations as integers
* @param movements - array of movements between locations as integers
* @param infectionRecord - array of infections over a number of days
* returns the newly created array that keeps track of the number of
* students that were infected by the student at the index after
* a certain number of days
*
**/
public static int[] countInfectionsByStudent(int days, int worldSize,
int[] locations, int[] movements, int[]infections){
// checks validity of inputs
if(isValid(locations,worldSize,infections)==true&&
isValid(locations, movements, worldSize)==true&&days>=0){
int[] infectionRecord = new int[locations.length];
for(int i=0;i<days;i++){
updateLocations(worldSize,locations,movements);
// create new array to store new values to add infectionRecord
int[] parallelArr = updateInfections(worldSize, locations, infections);
for(int j=0;j<infections.length;j++){
infectionRecord[j] = infectionRecord[j]+parallelArr[j];
}
}
return infectionRecord;
}else{
return null;
}
}
/**
* This method takes the newly created array from countInfectionsByStudent
* and checks to see its validity if it is not null and not equal to 0.
* It also checks to see none of the values in the array are negative and
* takes the values in the array and adds them up to find the average
* number of people infected by a single individual student
*
* @param infectionRecord - array of infections over a number of days
* returns -1 if input is invalid
* returns average number of infections by a single student
**/
public static int findRNaught(int[] infectionRecord){
// checks validity of input
if(infectionRecord==null||infectionRecord.length==0){
return -1;
}
for(int i=0;i<infectionRecord.length;i++){
if(infectionRecord[i]<0){
return -1;
}
}
int sum=0;
for(int j=0; j<infectionRecord.length;j++){
sum = sum+infectionRecord[j];
}
// math ceiling allows the value to always be rounded up
int avg = (int)Math.ceil(((double)sum)/infectionRecord.length);
return avg;
}
/**
* This method takes the array from countInfectionsByStudent
* and checks to see its validity if it is not null and not equal to 0.
* It also checks to see none of the values in the array are negative and
* also makes sure the names array and infectionRecord array are equal
* in length. It also loops through the infectionRecord array
* and finds the highest infection number and uses that index to
* locate the name of the individual responsible for the highest
* spread of COVID-19
*
* @param infectionRecord - array of infections over a number of days
* returns null if input is invalid
* returns name of SuperSpreader
**/
public static String findSuperSpreader(int[] infectionRecord,
String[] names){
// checks validity of input
if(infectionRecord==null||infectionRecord.length==0||
names==null||names.length==0){
return null;
}
if(infectionRecord.length!=names.length){
return null;
}
for(int i=0;i<infectionRecord.length;i++){
if(infectionRecord[i]<0){
return null;
}
}
// loops thru to find max infeciton value
int maxInfect=infectionRecord[0];
int index = 0;
for(int j=0;j<infectionRecord.length;j++){
if(maxInfect<infectionRecord[j]){
maxInfect = infectionRecord[j];
index = j;
}
}
// uses max infection value index to find name
String namesIndex=names[index];
return namesIndex;
}
}