Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java. accident.txt file: [http://files.fm/u/eyknffr] The National Highway Tra

ID: 3762229 • Letter: I

Question

In Java.

accident.txt file: [http://files.fm/u/eyknffr]

The National Highway Traffic Safety Administration has a database containing information on all of the fatal accidents in the United States, the Fatality Analysis Reporting System (FARS). You are to write a program that reads a file containing portions of that data to determine which hours of the day are most likely to have fatal accidents involving drunken drivers. Your instructor has provided a file, where each line contains the following data for the year 2013 in which there were over 30,000 accidents. The data in each line of the file is: Day Of Week The day of the week of the accident where 1 = Sunday, 2 = Monday, ..., 7 = Saturday hour The hour 0-23, the accident occurred. A value of 99 is used when the hour is unknown. minute The minute of the hour 0 - 59, the accident occurred. A value of 99 is used when the time is unknown. fatals Number of fatalities in this accident drunkDrv non-zero if this accident involved a drunken driver and 0 otherwise Your program only needs to use the hour and drunkDrv data values although the program must read all of the numbers in the file. Ignore any line of data where the hour is greater than 23. Your program must compute the percentage of fatal accidents for each hour that involve drunken drivers. The program must also display the hours with the least and the most drunk related accidents. You will need two arrays of 24 integers that will be indexed by the hour. One array should count the total number of accidents in that hour and the other should count the number of accidents that involved a drunken driver. After reading in all of the data, for each hour print the percentage of drunken driver accidents. When you calculate the percentage of drunk driver accidents for an hour, compare it to the best and worst hour, saving the best and worst.

Explanation / Answer

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

public class FatalAccPercentage {

   /**
   * @param args
   */
   /**
   * @param args
   */
   public static void main(String args[]) {
       Scanner scanner = null;
       try {
           File file = new File("accident.txt");
           scanner = new Scanner(file);
           int totalNoAcc[] = new int[24];
           int totalDrunkDrv[] = new int[24];
           // List<Result> results = new ArrayList<Result>();
           Arrays.fill(totalNoAcc, new Integer(0));
           Arrays.fill(totalDrunkDrv, new Integer(0));
           while (scanner.hasNextLine()) {
               String currentLine = scanner.nextLine();
               String[] resultArray = currentLine.split("   ");
               int dayOfWeek = Integer.parseInt(resultArray[0]);
               int hour = Integer.parseInt(resultArray[1]);
               int minute = Integer.parseInt(resultArray[2]);
               int fatals = Integer.parseInt(resultArray[3]);
               int drunkDrv = Integer.parseInt(resultArray[4]);
               if (hour < 24 && hour >= 0) {

                   totalNoAcc[hour] += fatals;
                   totalDrunkDrv[hour] += drunkDrv;
               }

           }
           System.out.println("Hour % of Drunk and Drive Acidents");
           double accdentPercent = 0;
           int safestHour = 0, dangeHour = 0;
           double highAccdentPercent = 0, lowAccdentPercent = 0;
           highAccdentPercent = lowAccdentPercent = (double) ((double) totalDrunkDrv[0] / ((double) totalNoAcc[0])) * 100.00;
           for (int i = 0; i < 24; i++) {
               accdentPercent = (double) ((double) totalDrunkDrv[i] / ((double) totalNoAcc[i])) * 100.00;
               System.out.println(((i <= 9) ? "0" + i : i) + ":00 "
                       + accdentPercent);
               if (lowAccdentPercent >= accdentPercent) {
                   lowAccdentPercent = accdentPercent;
                   safestHour = i;

               }
               if (highAccdentPercent <= accdentPercent) {
                   highAccdentPercent = accdentPercent;
                   dangeHour = i;

               }

           }
           System.out.println("safest hour to drive is :"
                   + ((safestHour <= 9) ? "0" + safestHour : safestHour)
                   + ":00");
           System.out.println("the most dangerous hour to drive is :"
                   + ((dangeHour <= 9) ? "0" + dangeHour : dangeHour) + ":00");
       } catch (Exception e) {
           e.printStackTrace();

       } finally {
           scanner.close();
       }

   }

}

output :

Hour       % of Drunk and Drive Acident
00:00       54.934965570007655
01:00       59.23460898502496
02:00       61.49963950973324
03:00       58.55397148676171
04:00       39.603960396039604
05:00       28.632938643702904
06:00       17.81756180733163
07:00       14.623467600700527
08:00       11.11111111111111
09:00       8.611410118406889
10:00       6.950880444856349
11:00       8.710217755443885
12:00       9.420289855072465
13:00       10.258199581297976
14:00       13.251454427925017
15:00       16.51108518086348
16:00       17.906008328375968
17:00       21.023046655424395
18:00       23.97107897664071
19:00       32.39601640304628
20:00       32.82887077997672
21:00       35.30751708428246
22:00       42.56120527306968
23:00       47.277739490006894
safest hour to drive is :10:00
the most dangerous hour to drive is :02:00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote