1. Engineering
  2. Computer Science
  3. using java simulate a roundabout by implementing array lists roundabouts...

Question: using java simulate a roundabout by implementing array lists roundabouts...

Question details

Using Java: Simulate a roundabout by implementing Array Lists Roundabouts, as shown in Figure 1 are a popular al- ternative to traffic intersections. According to stan- dard traffic rules, a car entering a roundabout must yield to cars already in the roundabout. That is, if a car wishes to enter the roundabout, there shouldb no car to the left of . A car continues around the roundabout in a counterclockwise direction until it reaches the desired exit and departs the roundabout Branch 1 Branch 3 B roundabout simulator is a program that sim ulates a roundabout one second at a time. For each second, the simulator uses the current position of each car to compute each cars position one second later The simulation continues until there are no more cars Write a simulator called Roundabout. java that reads in a stream of cars and outputs when each car departs the roundabout 1: Example of a roundabout. Input Your program should read in the input using a Scanner object, which is instantiated with System.in. The input will contain one or more lines of text. The first line will contain a single integer, N, denoting the number of cars to simulate. This is followed by N lines. Each line denotes an arriving car and will be of the form TPAD I is the time in seconds (int) of a cars arrival at a branch leading to the roundabout. All cars will be in chronological order. P is a licence plate of the car. This is a single alphanumerie word (String) A is the arrival branch, denoted by one of 0,1, 2, or 3 (int) D is the departure branch, denoted by one of 0, 1, 2, or 3 (int) For example, a car with license plate ABC128 that arrives at 30942 seconds on banch 2 and departs on branch 1 is denoteod 30942 ABC123 2 1 Hint: Use the Scanner object to easily parse the input. For this assignment you only need to use the nertInt() and nert) methods. It may be casier to read in all the input first and store the cars in a list. There are more examples on the next page. Processing The simulatiion consists of zero or more rounds where each round represents one (1) second of time. (Hint: you will need a main loop, where each iteration of the loop simulates one simulation ends when all N cars have departed the roundabout A roundabout has four branches, numbered 0,1,2,3, through which cars enter and depart the roundabout. Multiple cars may be in a branch at the same time. The cars are ordered by their arrival time. All arrival times in a branch will be unique. The roundabout has four positions, also numbered 0,1,2, 3, which are depicted in Fig- ure l by D·L 인 ad 3. Each position can hold at most one car 12and3.Each posion can hold at most one car Each round R consists of two (2) phases: Phase 1: All cars (T, P,A, D) with arrival time T- R are added to branch A Phase 2: The following actions all take place at the same time If a car is in branch B and has the earliest arrival time and there is no car in the roundabout at postion B-1 mod 4, then the car moves to position B its departure (D) branch is B+1 mod 4 identifier (P) and R. its departure (D) branch is not B1 mod 4, If a car is at position B and If a car is at position B and then t moves to position B1 mod 4. by the cars current position in the rounadbout. E.g., If car J is at position 1 and car Kis at position 3, and both cars depart the roundabout via branches 2 and 0, respectively, thenthe output for car J should precede the output for car K. Hint: this is a natural outcome of processing the cars in the order of their position: 0,1,2, 3 Hints: You can use four lists to keep track of cars in branches and a four-element array to keep track of cars in the roundabout. To simulate actions that take place at the same time, use two four-element arrays. One array stores the state of the roundabout at the beginning of the round, and the second array stores the state at the end of the round. The actions in Phase 2 use the state of the first array to compute the new positions and store the results in the second array. At the end of round, the second array is copied to the first and -initialized Output The simulator should output to the console (System.out) when a car leaves the roundabout The output should have the following format Car P departed at time FR where P is the cars unique identifier, and R is the current round Examples Sample InputSample Output Car HELLO departed at time 2 1 HELL0 0 1 Car ABC123 departed at time 3 Car L8ER departed at time 3 1 L8ER 2 0 2 ABC123 0 1 Car HELLO departed at time 2 Car BYEBYE departed at time 4 1 HELL0 0 1 2 BYEBYE 1 2

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution