|
| 1 | +/** |
| 2 | + * This program is used to simulate hypothetical COVID-19 transmission |
| 3 | + * simulations as well as collect data regarding how the virus would |
| 4 | + * spread if all students went about their lives without taking any |
| 5 | + * precautionary measures. This simulation includes the following |
| 6 | + * factors: populating lists of who is infected, their names, |
| 7 | + * their movement, the world size, how many people they infected, |
| 8 | + * the average number of infections, as well as the name of the |
| 9 | + * individual with the most infections after a certain number of days |
| 10 | +**/ |
| 11 | +import java.util.*; |
| 12 | +import java.io.File; |
| 13 | +import java.io.FileNotFoundException; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.FileInputStream; |
| 16 | +/** |
| 17 | +* This class contains all of the methods to simulate the hypothetical |
| 18 | +* COVID-19 transmission simulation. These methods include a couple |
| 19 | +* isValid methods, populateArrays method, updateLocations, updateInfections, |
| 20 | +* countInfectionsByStudent, findRNaught, and findSuperSpreader. |
| 21 | +**/ |
| 22 | +public class InfectionTracking{ |
| 23 | +/** |
| 24 | +* This method checks the validity of each value in the different arrays. |
| 25 | +* The method checks to see if the inputs for each array are valid or not |
| 26 | +* and returns a boolean statement to ensure validity or not. |
| 27 | +* The method checks to see if the lengths of each array match each other, |
| 28 | +* if any of the input arrays is null, and if the infection array values |
| 29 | +* are either a 1 or 0. |
| 30 | +* |
| 31 | +* @param names - array for student names |
| 32 | +* @param locations - array for locations |
| 33 | +* @param movements - array for movement between locations |
| 34 | +* @param infections - array for infections |
| 35 | +* @param worldSize - size of 1-dimensional world |
| 36 | +* @param pathToFile - file path |
| 37 | +* |
| 38 | +**/ |
| 39 | + public static boolean isValid(String[] names, int[] locations, |
| 40 | + int[] movements, int[] infections){ |
| 41 | + if(names==null||locations==null||movements==null||infections==null){ |
| 42 | + return false; |
| 43 | + } |
| 44 | + for(int i=0;i<locations.length;i++){ |
| 45 | + if(locations[i]<0){ |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + if(names.length!=locations.length||locations.length!=movements.length|| |
| 50 | + movements.length!=infections.length||infections.length!=names.length){ |
| 51 | + return false; |
| 52 | + } |
| 53 | + boolean infectionBool = false; |
| 54 | + for(int j=0;j<infections.length;j++){ |
| 55 | + if(infections[j]==0||infections[j]==1){ |
| 56 | + infectionBool = true; |
| 57 | + }else{ |
| 58 | + infectionBool=false; |
| 59 | + } |
| 60 | + } |
| 61 | + return infectionBool; |
| 62 | + } |
| 63 | +/** |
| 64 | +* This method checks the validity with the introduciton of a new |
| 65 | +* parameter: worldSize |
| 66 | +* This method makes sure none of the arrays are null, the locations |
| 67 | +* array and the movements array have the same length, the worldSize |
| 68 | +* is between 0 and worldSize-1 |
| 69 | +* @param locations - array of locations as integers |
| 70 | +* @param movements - array of movements between locations as integers |
| 71 | +* @param worldSize - int indicating size of world |
| 72 | +* returns boolean checking validity of these tests |
| 73 | +**/ |
| 74 | + public static boolean isValid(int[] locations,int[] movements, |
| 75 | + int worldSize){ |
| 76 | + if(locations==null||movements==null|| |
| 77 | + locations.length!=movements.length||worldSize<0){ |
| 78 | + return false; |
| 79 | + } |
| 80 | + for(int i=0;i<locations.length;i++){ |
| 81 | + if(locations[i]<0||locations[i]>(worldSize-1)){ |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | + /** |
| 88 | + * This method checks the validity with respect to different |
| 89 | + * parameters: locations, worldSize, and infecttions |
| 90 | + * This method makes sure none of the arrays are null, the locations |
| 91 | + * array and the infections array have the same length, the worldSize |
| 92 | + * is between 0 and worldSize-1 |
| 93 | + * @param locations - array of locations as integers |
| 94 | + * @param infections - array of infections either 1 or 0 |
| 95 | + * @param worldSize - int indicating size of world |
| 96 | + * returns boolean checking validity of these tests |
| 97 | + **/ |
| 98 | + public static boolean isValid(int[] locations, |
| 99 | + int worldSize,int[] infections){ |
| 100 | + if(locations==null||infections==null){ |
| 101 | + return false; |
| 102 | + } |
| 103 | + if(worldSize<0){ |
| 104 | + return false; |
| 105 | + } |
| 106 | + for(int i=0;i<locations.length;i++){ |
| 107 | + if(locations[i]<0||locations[i]>(worldSize-1)){ |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + boolean sameLength = locations.length==infections.length; |
| 112 | + for(int j=0;j<infections.length;j++){ |
| 113 | + if(infections[j]!=0&&infections[j]!=1){ |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + return sameLength; |
| 118 | + } |
| 119 | + /** |
| 120 | + * This method is used to populate empty arrays from the text file |
| 121 | + * that is imported by this method also. The method imports a file |
| 122 | + * and scans through it getting the individual values from it |
| 123 | + * and populating arrays according to those values. These arrays |
| 124 | + * include names, locations, movements, infections |
| 125 | + * The method returns -1 if the arrays are not properly populated |
| 126 | + * and returns the max locations value+1 if properly populated |
| 127 | + * |
| 128 | + * @param pathToFile - file path |
| 129 | + * @param names - array of names |
| 130 | + * @param locations - array of locations as integers |
| 131 | + * @param infections - array of infections either 1 or 0 |
| 132 | + * @param worldSize - int indicating size of world |
| 133 | + * @param movements - array of movements between locations as integers |
| 134 | + * returns max locations value+1 if populated correctly |
| 135 | + * returns -1 if populated incorrectly |
| 136 | + **/ |
| 137 | + public static int populateArrays(String pathToFile, String[] names, |
| 138 | + int[] locations, int[] movements, int[] infections) throws IOException{ |
| 139 | + if(pathToFile!=null&&isValid(names,locations,movements,infections)){ |
| 140 | + FileInputStream filePath = new FileInputStream(pathToFile); |
| 141 | + Scanner sc = new Scanner(filePath); |
| 142 | + int i =0; |
| 143 | + while(sc.hasNextLine()){ |
| 144 | + // goes thru each line in text file splitting strings by the "," |
| 145 | + // this gets the individual values to go in each array |
| 146 | + String str[]=sc.nextLine().split(","); |
| 147 | + String name = str[0]; |
| 148 | + int location = Integer.parseInt(str[1]); |
| 149 | + int movement = Integer.parseInt(str[2]); |
| 150 | + int infection = Integer.parseInt(str[3]); |
| 151 | + names[i]=name; |
| 152 | + locations[i] = location; |
| 153 | + movements[i] = movement; |
| 154 | + infections[i] = infection; |
| 155 | + i=i+1; |
| 156 | + } |
| 157 | + sc.close(); |
| 158 | + // loops thru finding max value |
| 159 | + int maxVal = locations[0]; |
| 160 | + for(int j=0;j<locations.length;j++){ |
| 161 | + if(maxVal<locations[j]){ |
| 162 | + maxVal = locations[j]; |
| 163 | + } |
| 164 | + } |
| 165 | + int max = maxVal+1; |
| 166 | + return max; |
| 167 | + }else{ |
| 168 | + return -1; |
| 169 | + } |
| 170 | + } |
| 171 | + /** |
| 172 | + * This method updates the locations of the individuals based on their |
| 173 | + * movements and updates the current location after the movement. It |
| 174 | + * also checks that the movements stay within the worldsize and wraps |
| 175 | + * back the location using the modulus operator to ensure the movement |
| 176 | + * stays within the worldSize |
| 177 | + * |
| 178 | + * @param modulo - modulus of the new location divided by the worldSize |
| 179 | + * @param worldSize - int indicating size of world |
| 180 | + * @param movements - array of movements between locations as integers |
| 181 | + * @param locations - array of locations as integers |
| 182 | + * |
| 183 | + **/ |
| 184 | + public static void updateLocations(int worldSize, int[] locations, |
| 185 | + int[] movements){ |
| 186 | + if(isValid(locations,movements,worldSize)==true){ |
| 187 | + for(int i=0;i<locations.length;i++){ |
| 188 | + // check validity of input |
| 189 | + if(locations[i]+movements[i]<0||locations[i]+movements[i]>=worldSize){ |
| 190 | + int modulo = (locations[i]+movements[i])%worldSize; |
| 191 | + // if new location is negative |
| 192 | + if(modulo<0){ |
| 193 | + locations[i]=modulo+worldSize; |
| 194 | + // if new location is not neg and > worldSize |
| 195 | + }else{ |
| 196 | + locations[i]=modulo; |
| 197 | + } |
| 198 | + // if new location is not negative and < worldSize |
| 199 | + }else{ |
| 200 | + locations[i] = locations[i]+movements[i]; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + /** |
| 206 | + * This method updates the infections of students who come into contact |
| 207 | + * with each other or are in the same location at one point. The method |
| 208 | + * takes the locations and worldSize ensureing not to go out of bounds, |
| 209 | + * and counds how many individuals a student has infected based upon |
| 210 | + * their contact at their shared location. The method creates a new array |
| 211 | + * keeping track of all the students and the number of other students |
| 212 | + * they infect. |
| 213 | + * |
| 214 | + * @param modulo - modulus of the new location divided by the worldSize |
| 215 | + * @param worldSize - int indicating size of world |
| 216 | + * @param infections - array of infections either 1 or 0 |
| 217 | + * @param locations - array of locations as integers |
| 218 | + * returns the newly created array that keeps track of the number of |
| 219 | + * students that were infected by the student at the index |
| 220 | + * |
| 221 | + **/ |
| 222 | + public static int[] updateInfections(int worldSize, int[] locations, |
| 223 | + int[] infections){ |
| 224 | + if(isValid(locations,worldSize,infections)==true){ |
| 225 | + int[] numStudentsInfected= new int[infections.length]; |
| 226 | + // loops thru adding to numStudentsInfected for each student infected |
| 227 | + for(int i=0;i<infections.length;i++){ |
| 228 | + if(infections[i]==1){ |
| 229 | + for(int j=0;j<locations.length;j++){ |
| 230 | + if(locations[i]==locations[j]&&infections[j]==0){ |
| 231 | + numStudentsInfected[i] = numStudentsInfected[i]+1; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + // loops through changing all the infected to 1's |
| 237 | + for(int i=0;i<infections.length;i++){ |
| 238 | + if(infections[i]==1){ |
| 239 | + for(int j=0;j<infections.length;j++){ |
| 240 | + if(locations[i]==locations[j]&&infections[j]==0){ |
| 241 | + infections[j]=1; |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + return numStudentsInfected; |
| 247 | + } |
| 248 | + return null; |
| 249 | + } |
| 250 | + /** |
| 251 | + * This method creates a new array called infectionRecord and updates |
| 252 | + * the locations of students after their movements each day and |
| 253 | + * records the number of people they have infected or got infected in |
| 254 | + * the shared location |
| 255 | + * |
| 256 | + * @param worldSize - int indicating size of world |
| 257 | + * @param infections - array of infections either 1 or 0 |
| 258 | + * @param locations - array of locations as integers |
| 259 | + * @param movements - array of movements between locations as integers |
| 260 | + * @param infectionRecord - array of infections over a number of days |
| 261 | + * returns the newly created array that keeps track of the number of |
| 262 | + * students that were infected by the student at the index after |
| 263 | + * a certain number of days |
| 264 | + * |
| 265 | + **/ |
| 266 | + public static int[] countInfectionsByStudent(int days, int worldSize, |
| 267 | + int[] locations, int[] movements, int[]infections){ |
| 268 | + // checks validity of inputs |
| 269 | + if(isValid(locations,worldSize,infections)==true&& |
| 270 | + isValid(locations, movements, worldSize)==true&&days>=0){ |
| 271 | + int[] infectionRecord = new int[locations.length]; |
| 272 | + for(int i=0;i<days;i++){ |
| 273 | + updateLocations(worldSize,locations,movements); |
| 274 | + // create new array to store new values to add infectionRecord |
| 275 | + int[] parallelArr = updateInfections(worldSize, locations, infections); |
| 276 | + for(int j=0;j<infections.length;j++){ |
| 277 | + infectionRecord[j] = infectionRecord[j]+parallelArr[j]; |
| 278 | + } |
| 279 | + } |
| 280 | + return infectionRecord; |
| 281 | + }else{ |
| 282 | + return null; |
| 283 | + } |
| 284 | + } |
| 285 | + /** |
| 286 | + * This method takes the newly created array from countInfectionsByStudent |
| 287 | + * and checks to see its validity if it is not null and not equal to 0. |
| 288 | + * It also checks to see none of the values in the array are negative and |
| 289 | + * takes the values in the array and adds them up to find the average |
| 290 | + * number of people infected by a single individual student |
| 291 | + * |
| 292 | + * @param infectionRecord - array of infections over a number of days |
| 293 | + * returns -1 if input is invalid |
| 294 | + * returns average number of infections by a single student |
| 295 | + **/ |
| 296 | + public static int findRNaught(int[] infectionRecord){ |
| 297 | + // checks validity of input |
| 298 | + if(infectionRecord==null||infectionRecord.length==0){ |
| 299 | + return -1; |
| 300 | + } |
| 301 | + for(int i=0;i<infectionRecord.length;i++){ |
| 302 | + if(infectionRecord[i]<0){ |
| 303 | + return -1; |
| 304 | + } |
| 305 | + } |
| 306 | + int sum=0; |
| 307 | + for(int j=0; j<infectionRecord.length;j++){ |
| 308 | + sum = sum+infectionRecord[j]; |
| 309 | + } |
| 310 | + // math ceiling allows the value to always be rounded up |
| 311 | + int avg = (int)Math.ceil(((double)sum)/infectionRecord.length); |
| 312 | + return avg; |
| 313 | + } |
| 314 | + /** |
| 315 | + * This method takes the array from countInfectionsByStudent |
| 316 | + * and checks to see its validity if it is not null and not equal to 0. |
| 317 | + * It also checks to see none of the values in the array are negative and |
| 318 | + * also makes sure the names array and infectionRecord array are equal |
| 319 | + * in length. It also loops through the infectionRecord array |
| 320 | + * and finds the highest infection number and uses that index to |
| 321 | + * locate the name of the individual responsible for the highest |
| 322 | + * spread of COVID-19 |
| 323 | + * |
| 324 | + * @param infectionRecord - array of infections over a number of days |
| 325 | + * returns null if input is invalid |
| 326 | + * returns name of SuperSpreader |
| 327 | + **/ |
| 328 | + public static String findSuperSpreader(int[] infectionRecord, |
| 329 | + String[] names){ |
| 330 | + // checks validity of input |
| 331 | + if(infectionRecord==null||infectionRecord.length==0|| |
| 332 | + names==null||names.length==0){ |
| 333 | + return null; |
| 334 | + } |
| 335 | + if(infectionRecord.length!=names.length){ |
| 336 | + return null; |
| 337 | + } |
| 338 | + for(int i=0;i<infectionRecord.length;i++){ |
| 339 | + if(infectionRecord[i]<0){ |
| 340 | + return null; |
| 341 | + } |
| 342 | + } |
| 343 | + // loops thru to find max infeciton value |
| 344 | + int maxInfect=infectionRecord[0]; |
| 345 | + int index = 0; |
| 346 | + for(int j=0;j<infectionRecord.length;j++){ |
| 347 | + if(maxInfect<infectionRecord[j]){ |
| 348 | + maxInfect = infectionRecord[j]; |
| 349 | + index = j; |
| 350 | + } |
| 351 | + } |
| 352 | + // uses max infection value index to find name |
| 353 | + String namesIndex=names[index]; |
| 354 | + return namesIndex; |
| 355 | + } |
| 356 | +} |
0 commit comments