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

Problem: Analyzing Data from a Road Traffic Meter To analyze how much traffic is

ID: 3875258 • Letter: P

Question

Problem: Analyzing Data from a Road Traffic Meter To analyze how much traffic is on the street, city planners use a Road Traffic Meter, as shown in the figure below: Figure 1: Retrieved December 4, 2017, http://diamondtraffic.com. The traffic meter consists of two air hoses lying across the lane of traffic. Every time the wheels of a vehicle rol over both hoses in quick succession, the meter logs the event as one axle of a vehicle. Two pieces of information are logged for each event: the time (in milliseconds) and the speed the axle was traveling at (in kilometers per hour). The traffic meter produces a log of axle events that look like this 97205795 51 97205995 52 97207123 45 97207347 44

Explanation / Answer

package com.test;

import java.util.List;

import java.util.ArrayList;

import java.util.Scanner;

interface TrafficReporter {

   public List<String> computeTraffic(Scanner input);

}

public class VehicleCounter implements TrafficReporter{

   @Override

   public List<String> computeTraffic(Scanner input) {

       String car = ": Car or light truck";

       String truck = ": Heavy truck, ";

       String result = "";

       List<String> results = new ArrayList<String>();

       List<Long> timeList = new ArrayList<Long>();

       List<Integer> speedList = new ArrayList<Integer>();

       List<Float> distanceList = new ArrayList<Float>();

       while(input.hasNextLong()) {

           timeList.add(input.nextLong());

           speedList.add(input.nextInt());

       }

       for (int i = 0; i < timeList.size() - 1;i++) {

           long timeTaken = timeList.get(i+1) - timeList.get(i);

           float avgSpeed = getAvgSpeedInCmPerMillies(speedList.get(i), speedList.get(i+1));

           float distance = timeTaken * avgSpeed;

           distanceList.add(distance);

       }

       for (int i = 0; i < distanceList.size() - 1; i++) {

           result = ""+timeList.get(i) / 1000;

           int front,rear;

           if (distanceList.get(i) > 186) {

               if (distanceList.get(i+1) > 186) {

                   result += car;

                   results.add(result);  

               } else {

                   front = 1;

                   rear = 2;

                   ++i;

                   while(distanceList.get(i+1) > 186) {

                       ++rear;

                       ++i;

                   }

                   result += truck +front +" "+ rear;

               }

           } else {

               front = 2;

               rear = 1;

               ++i;

               while(distanceList.get(i+1) < 186) {

                   ++front;

                   ++i;

               }

               result += truck +front +" "+ rear;

           }

       }

       return results;

   }

  

   private float getAvgSpeedInCmPerMillies(int speed1, int speed2) {

       float firstSpeed = speed1/36; // km/hr to cm/millisec

       float secondSpeed = speed2/36;

       return (firstSpeed + secondSpeed) / 2;

   }

   public static void main(String[] args) {

       VehicleCounter vehicleCounter = new VehicleCounter();

       List<String> results = vehicleCounter.computeTraffic(new Scanner(System.in));

       for (String result : results) {

           System.out.println(result);

       }

   }

}

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