Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program called VehicleCounter.java that reads in a log of axles (as exemplified above) and outputs the corresponding list of cars and heavy trucks.

Write a program called VehicleCounter.java that reads in a log of axles (as exemplified above) and outputs the corresponding list of cars and heavy trucks. Your VehicleCounter class must implement the provided TrafficReporter 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 traffic meter. Each line denotes a single event of an axle crossing the traffic 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 traffic:

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 vehicles first 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

503612 33

503775 34

560933 37

561479 36

595629 41

596044 42

650172 56

650780 55

650878 54

650978 53

502: Heavy truck, 1 2

560: Car or light truck

595: Car or light truck

650: Heavy truck, 1 3

VechicleCounterTest.java

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList; import java.util.Scanner; import org.junit.jupiter.api.Test; class VehicleCounterTest { private static Scanner mkTest( long [] input ) { String s = ""; for( int i = 0; i < input.length; i += 2 ) { s += String.format( "%d %d ", input[i], (int)input[i+1] ); } return new Scanner( s ); } private static ArrayList mkOutput( int [] output ) { ArrayList al = new ArrayList(); for( int i = 0; i < output.length; i += 3 ) { if( output[i+1] == 0 ) { al.add( String.format( "%d: Car or light truck", output[i] ) ); } else { al.add( String.format( "%d: Heavy truck, %d %d", output[i], output[i+1], output[i+2] ) ); } } return al; } private static boolean doTest( long [] input, int [] output ) { ArrayList expected = mkOutput( output ); VehicleCounter vc = new VehicleCounter(); ArrayList al = vc.computeTraffic( mkTest( input ) ); return al != null && al.equals( mkOutput( output ) ); } @Test void testEmpty() { VehicleCounter vc = new VehicleCounter(); ArrayList al = vc.computeTraffic( new Scanner( "" ) ); assertTrue( al != null && al.size() == 0, "Empty Test" ); } @Test void test1Car() { long [] input = { 595629, 41, 596044, 42 }; int [] output = { 595, 0, 0 }; assertTrue( doTest( input, output), "1 Car Test" ); } @Test void test1Truck() { long [] input = { 502788, 32, 503612, 33, 503775, 34 }; int [] output = { 502, 1, 2 }; assertTrue( doTest( input, output), "1 Truck Test" ); } @Test void test1Car1Truck() { long [] input = { 595629, 41, 596044, 42, 650172, 56, 650780, 55, 650878, 54, 650978, 53 }; int [] output = { 595, 0, 0, 650, 1, 3 }; assertTrue( doTest( input, output), "1 Car 1 Truck Test" ); } @Test void test1Truck1Car() { long [] input = { 502788, 32, 503612, 33, 503775, 34, 560933, 37, 561479, 36 }; int [] output = { 502, 1, 2, 560, 0, 0 }; assertTrue( doTest( input, output), "1 Car 1 Truck Test" ); } @Test void testExample() { long [] input = { 502788, 32, 503612, 33, 503775, 34, 560933, 37, 561479, 36, 595629, 41, 596044, 42, 650172, 56, 650780, 55, 650878, 54, 650978, 53 }; int [] output = { 502, 1, 2, 560, 0, 0, 595, 0, 0, 650, 1, 3 }; assertTrue( doTest( input, output), "Example Test" ); } }

TrafficReporter.java

import java.util.ArrayList; import java.util.Scanner; public interface TrafficReporter { public static int MIN_WHEELBASE = 186; // centimeters // This method takes a string containing the log of a car meter // and returns a log of vehicles that passed the car meter. // // Parameters: // Scanner input: is a Scanner object that is a stream of text // containing the log extracted from a traffic meter. // Each entry in the log (one per line) denotes // a vehicle axel triggering the meter. Each // entry consists of 2 integers: time (in ms) // and speed (in km/h) // e.g. 97205795 51 // // Returns: an ArrayList of Strings. Each string is of the form: // "T: Light car or truck" // or // "T: Heavy truck, F R" // where T is the time (in seconds) where the front axle was sensed // F is the number of axles in the front of the truck (>=1) // R is the number of axles in the rear of the truck (>= 1) // Examples: // "36736: Light car or truck" // "74231: Heavy truck, 1 2" // Semantics: // A light car or truck has only one axle in th back and one in // the front, separated by at least 186cm (MIN_WHEELBASE). // A heavy truck has 3 or more axles such that 3 <= F + R // where F >= 1 is the number of front axles, such that // consecutive axles are less than 186cm apart. // There are also R >= 1 rear axles, such that consecutive // axles are less than 186cm apart. public ArrayList computeTraffic( Scanner log ); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions