QuestionQuestion

Transcribed TextTranscribed Text

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: The traffic meter consists of two air hoses lying across the lane of tra c. Every time the wheels of a vehicle roll 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 ... where each line corresponds to a single axle event and is denoted by the time of crossing and speed. However, tra c planners need to know how many cars and heavy trucks are using the street. Your task is to write a program that processes the log and converts the log of axles to a log of cars and trucks that have crossed the tra c meter. Write a program called VehicleCounter.java that reads in a log of axles (as exempli ed above) and outputs the corresponding list of cars and heavy trucks. Your VehicleCounter class must implement the provided TrafficReporter interface. This is because your program will be tested via this interface. The interface contains a single method: public ArrayList computeTraffic( Scanner input ); This method must perform the required computation. Input The method takes a Scanner object, which contains 0 or more lines of text from the log produced by a tra c meter. Each line denotes a single event of an axle crossing the tra c meter. Each line consists of an integer of type long, denoting the time in milliseconds; and an integer of type int, denoting the speed of the axle in kilometers per hour. You may assume that there are no errors in the input. Hint: Use the Scanner object to easily parse the input by using the methods such as hasNextLong(), nextLong(), and nextInt(), on the Scanner object. Semantics The input log will contain a sequence of axles, corresponding to zero or more vehicles driving down the street. Your vehicle counter must distinguish between two classes of tra c: Cars and light trucks are vehicles that have exactly two axles, which are at least 186cm apart. Heavy trucks are vehicles that have at least three axles, with one or more close consecutive axles at the front and one or more close consecutive axles on the back. Two axles are close if they are less than 186cm apart. The distance between axles can be computed using the general formula distance = speed * time where speed is the average of the two axle speeds and time is the positive difference between the arrival times of the two axles. Note: The speed of the axles is given in km/h, the time of arrival is given in milliseconds, and the minimum distance is in centimeters. You will need to convert the values to common units in order to get correct results. Output The method should return an ArrayList of Strings denoting the class of vehicle for each set of axles in the input log. If the axles correspond to the car or light truck, the corresponding String is T: Car or light truck and, if the axles in the input log correspond to a heavy truck, the corresponding String is T: Heavy truck, F R where T is the time, rounded down to the nearest second, of the vehicle's rst axle crossing the traffic meter. F is the number of axles at the front of a heavy truck R is the number of axles at the rear of a heavy truck Examples Below are a sequence of examples and the corresponding output. Sample Input Sample Output 502788 32 502: Heavy truck, 1 2 503612 33 560: Car or light truck 503775 34 595: Car or light truck 560933 37 650: Heavy truck, 1 3 561479 36 595629 41 596044 42 650172 56 650780 55 650878 54 650978 53

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.ArrayList;
import java.util.Scanner;

/*
* VehicleCounter class
*/
public class VehicleCounter implements TrafficReporter {
   
    // Method to compute traffic from Scanner
    public ArrayList computeTraffic(Scanner log) {
       // Initialize result array
       ArrayList result = new ArrayList();
       // Initialize axles array
       ArrayList axles = new ArrayList();
       // Run in a loop, while the Scanner has remaining lines
       while (log.hasNextLine()) {
            // Obtain line from Scanner
            String line = log.nextLine();
            // Split the line by whitespace
            String[] params = line.split(" ");
            if (params.length == 2) {
                // Obtain the time from the first parameter
                double time = Integer.parseInt(params[0]) / 1000.0;
                // Obtain the speed from the second parameter
                double speed = Integer.parseInt(params[1]) * 1000.0 / 60.0;
                // Add axle to the axles array
                axles.add(new Axle(time, speed));
            }
       }
       // Initialize array of front axles
       ArrayList frontAxles = new ArrayList();
       // Initialize array of rear axles
       ArrayList rearAxles = new ArrayList();
       // Execute in a loop while the axles array is NOT empty
       while (!axles.isEmpty()) {
            // Remove the first axle
            Axle axle = (Axle) axles.remove(0);...
$28.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats