Question: Can some one help me write a JAVA test class for this code import java.util.*; import java.io.*; //JAVA VERSION 8 HAS ERRORS WITH FOR-EACH LOOPS
Can some one help me write a JAVA test class for this code
import java.util.*; import java.io.*; //JAVA VERSION 8 HAS ERRORS WITH FOR-EACH LOOPS IN FILE-SCANNING. EVERY LOOP IS A FOR LOOP public class Simulator { //Constants private static final int MAX_LINE_LENGTH = 25; private static final int MAX_LINE_INDEX = 5; private static final int MAX_NUMOF_VEHICLES = 50000; public static void main(String[] args) throws IOException { //will break if file not found //IO Variables Vehicle [] vehicles = new Vehicle[1]; //array must be initialized to build program. errors are handled later. Scanner scan = new Scanner(System.in); File inFile; boolean filenotfound = true; while(filenotfound) { try { System.out.print("Input File: "); inFile = new File(scan.nextLine()); vehicles = fileToArray(inFile).clone(); //deep-copies object array to avoid data loss filenotfound = false; } catch (IOException error) { System.out.println("File not found. Try again. "); } } int numManualBooths = 0, numAutomaticBooths = 0; //handle min no. booths here while(numManualBooths<1||numManualBooths>6) { System.out.print("Manual Toll Booths: "); numManualBooths = scan.nextInt(); if (numManualBooths<1||numManualBooths>6) System.out.println(" Invalic number of tollbooths. Please enter at least 1."); } scan.close(); //precents resource leaks by scanner numAutomaticBooths = MAX_LINE_INDEX+1 - numManualBooths; System.out.println("Automatic Toll Booths: "+ numAutomaticBooths); System.out.println(""); //create tollBooth collection here TollBoothLine [] tollBooths = new TollBoothLine[MAX_LINE_INDEX+1]; //create array sized only to what is needed //System.out.println("DEBUG: tollbooths = " + tollBooths.length); int lineindex = 0; for (int x=0;x<=MAX_LINE_INDEX;x++){ //sets first [x] values in array as manual tollBooths if (lineindex0) { //handles empty tollboothlines // If there are vehicles in the tollbooth[index], then check if the time currently is equal to the front time + passing time if (time-tollBooths[index].getQueue(0).getFrontTime() == tollBooths[index].getQueue(0).getPassingTime()) { //System.out.println(tollBooths[index].getQueue(0)); //if vehicle is front of the line and its passing time is called, then if(tollBooths[index].getQueue(0).isManual()) { doneList.addManual((ManualVehicle)tollBooths[index].next(time)); } //tollBooths[index].next() is a vehicle object that is returned. Object is converted into a subclass object else { doneList.addAutomatic((AutomaticVehicle)tollBooths[index].next(time)); } } } } for (index = count; index time) { break; } //minimizes number of comparisons in the list if the loop breaks when the desired item is found. Worst case is N comparisons } for (index = 0; index<=MAX_LINE_INDEX; index++) { //sets time when vehicle gets to front of line if (tollBooths[index].getLineLength()>0) //handles empty tollBooth lines. if (tollBooths[index].getQueue(0).getFrontTime()==null) { tollBooths[index].getQueue(0).setFrontTime(time); } //assigns or prevents reassigning of the front time for the first vehicle in line } if (count == vehicles.length) done = true; // once going through all the vehicles, then done. } //Display doneList stats to console below int linecount = 0; for (linecount=0;linecountcopyOf(fileArray,count); //truncates nulls from array here scan.close(); //prevents resource leaks return arr; } public static void addToShortestLine(Vehicle vehicle, TollBoothLine [] tollBooths){ int minLineLength = 0, lineLength = 0, index = 0, minLocation = 0; //vehicle or line indexing variables for (index=0; index<=MAX_LINE_INDEX;index++) { //checks all TollBoothLines in the collection if (vehicle.getType().equalsIgnoreCase(tollBooths[index].getLineType())){ //if vehicle and line are same type minLocation = (tollBooths[minLocation].getLineLength()>=tollBooths[index].getLineLength()) ? index:minLocation; //find shortest line of vehicle type } } tollBooths[minLocation].addVehicle(vehicle); //add vehicle to shortest line } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
