Untitled
package traffic; import java.util.Scanner; public class traffic { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input the total number of signals System.out.print("Enter total number of signals (n): "); int totalSignals = scanner.nextInt(); // Arrays to store signal timings and ways int[] travelTimes = new int[totalSignals]; // Travel times between signals int[] greenTimes = new int[totalSignals]; // Green light times for each signal int[] redTimes = new int[totalSignals]; // Red light times for each signal int[] delayTimes = new int[totalSignals]; // Delay times at each signal int[] signalTypes = new int[totalSignals]; // Types of signals (2-way, 3-way, 4-way) // Get travel times between signals (except for destination) for (int i = 0; i < totalSignals - 1; i++) { System.out.print("Enter time taken to travel from signal " + (i + 1) + " to " + (i + 2) + ": "); travelTimes[i] = scanner.nextInt(); } // For last signal, the travel time goes to destination System.out.print("Enter time taken to travel from signal " + totalSignals + " to destination: "); travelTimes[totalSignals - 1] = scanner.nextInt(); // Get green and red signal times for each signal for (int i = 0; i < totalSignals; i++) { System.out.print("Enter green signal time at signal " + (i + 1) + ": "); greenTimes[i] = scanner.nextInt(); System.out.print("Enter red signal time at signal " + (i + 1) + ": "); redTimes[i] = scanner.nextInt(); System.out.print("Enter delay time at signal " + (i + 1) + ": "); delayTimes[i] = scanner.nextInt(); // Loop to ensure the correct signal type is entered (2, 3, or 4) int signalType; while (true) { System.out.print("Enter the way for signal " + (i + 1) + " (2 for 2-way, 3 for 3-way, 4 for 4-way): "); signalType = scanner.nextInt(); if (signalType == 2 || signalType == 3 || signalType == 4) { signalTypes[i] = signalType; break; } else { System.out.println("Invalid input! Please enter 2, 3, or 4."); } } } // Get road condition factor System.out.print("Enter road condition factor (e.g., 0.9 for normal conditions): "); double roadCondition = scanner.nextDouble(); // Calculate total time to reach destination int totalTime = calculateTime(totalSignals, travelTimes, greenTimes, redTimes, delayTimes, signalTypes, roadCondition); // Output the total time System.out.println("Total time to reach the destination: " + totalTime + " seconds"); scanner.close(); } public static int calculateTime(int totalSignals, int[] travelTimes, int[] greenTimes, int[] redTimes, int[] delayTimes, int[] signalTypes, double roadCondition) { int totalTime = 0; // Track total journey time int currentSignal = 0; // Track the current signal that is green // Initially, all signals are green (starting condition) for (int i = 0; i < totalSignals; i++) { totalTime += greenTimes[i] + delayTimes[i]; // Add the green time and delay time for all signals in the beginning } // Loop through each signal after the initial green phase for (int i = 0; i < totalSignals; i++) { // The signal cycle (green + red time) int cycleTime = greenTimes[i] + redTimes[i]; // Now, we determine which signal will be green in the current iteration currentSignal = (i % totalSignals); // Ensures that one signal is green at a time // Calculate if we need to wait at the signal (if it's red) int timeInCycle = totalTime % cycleTime; // Time in the signal's current cycle // If the current signal is green, we pass immediately if (timeInCycle < greenTimes[currentSignal]) { // Green, no waiting } else { // If red, wait for the green light totalTime += (cycleTime - timeInCycle); } // Add delay time at each signal totalTime += delayTimes[currentSignal]; // Add travel time if it's not the last signal if (i < totalSignals - 1) { totalTime += travelTimes[i]; } // Adjust for the signal type (2-way, 3-way, 4-way) if (signalTypes[currentSignal] == 2) { totalTime += 2; // Adding some extra time for 2-way signals (adjust as needed) } else if (signalTypes[currentSignal] == 3) { totalTime += 3; // Adding extra time for 3-way signals (adjust as needed) } else if (signalTypes[currentSignal] == 4) { totalTime += 4; // Adding extra time for 4-way signals (adjust as needed) } } // Adjust for road conditions totalTime = (int) (totalTime * roadCondition); return totalTime; } }
Leave a Comment