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

Context: Data collection is ubiquitous in today’s modern world. Data is being co

ID: 3813187 • Letter: C

Question

Context: Data collection is ubiquitous in today’s modern world. Data is being
collected through a many cameras and embedded devices. This data along
with tracking algorithms can be ‘mined’ to improve quality of life.
Many heavily traversed intersections are now
equipped with cameras. Simple image
processing algorithms can be used to count the
number of vehicles that pass thru the
intersection as well as any accidents. This
data could be used to identify intersections
that have high traffic volumes and are more
prone to accidents. These intersections could
be selected for study and improvement.

Problem Description:
Write a Java program to do the following:
1. Read in data from a text file that contains traffic data. Each line in the
program will contain an intersection ID (integer value) and the time it took a
particular vehicle to cross the intersection (in seconds, integer value). YOUR
PROGRAM SHOULD NOT CONTAIN AN ABSOLUTE PATH TO THE FILE,
ONLY THE FILE NAME (i.e., ‘traffic.txt’)
2. Read each line in the file and keep track of the number of vehicles that pass
through an intersection and the number of accidents at an intersection. An
accident occurs when it takes a car more than 500 seconds to cross the
intersection.
3. Calculate and display the relative frequency of an accident per total number
of vehicles that pass thru each intersection and the most dangerous
intersections (in terms of most accidents and highest accident rate).

Requirements:
In addition to fulfilling the interaction described above your program must also

1. Have the main class named ‘TrafficDataProcessor’
2. Have a constant value, NUM_INTERSECTIONS, for the size of the array and
set it to 12, the number of intersections.
3. Read from the file ‘traffic.txt’. This file as the following format with the first
column containing the intersection ID and the second column containing the
number of seconds a vehicle was in the intersection.

4. Calculate the number of accidents (i.e., if time in intersection is > 500) and accident rate of each intersection and save the result in an array. 5. Display the output in a manner consistent with sample interaction described below. You should display the number of vehicles, number of accidents and accident rate for each intersection. You should also determine the highest number of accidents and highest accident rate among all intersections.

Do not worry if you show more or fewer decimal places with the results or if the
alignment is off.
Optional: To format your output, consider using System.out.printf [with the format
specifier
“%2d %13d %20d %24.3f ”

Remember: Use good programming practices such as proper use of white
space, sufficient in-line comments to explain your code.

Create and use user-defined methods as you warranted. Make sure that all
methods have headers and your program clearly identifies yours algorithm as
comments.

this is the traffic.txt:

11 600 514 318 00 8 60 4 8 113 857 111 24718561573

Explanation / Answer

import com.sun.tools.doclets.formats.html.SourceToHTMLConverter; import java.util.HashMap; import java.util.IntSummaryStatistics; import java.util.Map; import java.io.*; /** * Created by gchadha on 4/8/17. */ public class TrafficDataProcessor { private static Map accidentOccursCount = new HashMap(); private static Map noOfCarsPassed = new HashMap(); private static int hightestAccidentRate; private static int highestAccidentCount; public static void main(String z[]) { //reads the file try{ FileInputStream fstream = new FileInputStream("traffic.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { String[] tokens = strLine.split(" "); String id = tokens[0]; String time = tokens[1]; dataSaved(Integer.parseInt(id), Integer.parseInt(time)); } in.close(); }catch (Exception e){ System.err.println("Error: " + e.getMessage()); } calculation(); } //this method saves the accident occurs count and No of cars passed count for each intersection private static void dataSaved(int intersectionId, int timeTaken) { if (timeTaken>500) { if(accidentOccursCount.containsKey(intersectionId)) { Integer count = accidentOccursCount.get(intersectionId); count++; accidentOccursCount.put(intersectionId,count); } else { accidentOccursCount.put(intersectionId,1); } } if(noOfCarsPassed.containsKey(intersectionId)) { Integer count1 = noOfCarsPassed.get(intersectionId); count1++; noOfCarsPassed.put(intersectionId,count1); } else { noOfCarsPassed.put(intersectionId,1); } } // This method calculates and displays the avg. accident rate. private static void calculation() { for (int i=0; i highestAccidentCount) { highestAccidentCount = accidentCount; } System.out.print("Number Of Accidents >> "+ accidentCount); } if(noOfVehicles !=0) { accidentRate = accidentCount/noOfVehicles; if(accidentRate > hightestAccidentRate) { hightestAccidentRate = accidentRate; } System.out.print("Accident Rate >> " + accidentRate); } } System.out.print("Most accidents at any interaction: "+ highestAccidentCount); System.out.print("Hightest accident rate at any interaction: "+ hightestAccidentRate); } }
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