Can someone please help on this. It seems impossible
The possible trains are Cargo Trains, Intercity Trains and Commuter Trains. The tracks are straight (rather than circular as in Exercise 1). The tracks are 10 km long.
Trains have different maximum speeds: Intercity Trains have a maximum speed of 100 Kilometres per hour (KPH), Commuter Trains 70 KPH and Cargo Trains 40 KPH. A train is identified by an Id number. Notice that in real life these different train types would have more characteristics (e.g. Intercity and Commuter trains would have a passenger capacity, Cargo trains would have a cargo capacity, etc.). In this simulation we can ignore these.
Each train has a position on the tracks. The position of a train is expressed with a double number, e.g. position 7.53 means that a train is on the 7th kilometre, 530 meters on the tracks. Given the position, we can measure the distance of two trains. For example, if t1 is at position 8.93 and t2 at position 7.23 then using the absolute value of their difference t1.dist(t2)=1.70.
Trains can accelerate up to their maximum speed and decelerate to 0 KPH (you can use a setter like setSpeed(double speed) to change speed).
The simulation starts with N trains (experiment with different values for N, you can have it preset in your program), stored in an array tlist. The type of train is determined randomly, the trains are positioned randomly on the tracks (anywhere between position 0 and 10), and their initial speed is also randomly generated (below their maximum speed of course).
You should make sure that the order in the array reflects the order of the position of the trains on the tracks, i.e. tlist[i+1].position > tlist.position.
Once a train has passed position 10 (i.e. the end of the tracks) it is not on the tracks anymore and therefore it disappears from the simulation; when a train has disappeared a new one arrives around position 0 (i.e. in a random position between 0 and 1).
The simulation is managed by a main loop (repeat n times, where n is read as a command line argument), each iteration of which is meant to represent 20 seconds. At each iteration a train will accelerate if it has clear tracks ahead, i.e. if the distance with the train in front is large enough. You can decide what large means and write your choice in a comment in the program. It has however to be a reasonable and realistic choice. If a train t1 is close enough to train t2 and t1’s speed is such that after 20 or less seconds t1 will crash on t2, then t1 will reduce speed so that it doesn’t crash on t2.
The speed at which a train is running will determine its position at the next instant, for example if an Intercity Train is at position 1 (1 kilometre) and runs at 100 KPH then after 20 seconds it will be at position 1.555=1+200/360.
Intercity and Commuter trains have to stop at track positions 3.5, 6 and 8.5 for 40 seconds (i.e. for 2 iterations). You can see these stops as stops at stations. No passengers this time!!! Cargo trains don’t stop at all.
PROGRAM OUTPUT Your program must print out the initial list of trains and the significant actions that happen. For example, if there is a "reducing speed action" we should see: Intercity train 18 at position 3.40 breaking before Cargo train 19 at position 3.44. New speed for Intercity train 18 is 40 kph. Similarly, your program must print all the information if a train disappears from the simulation and a new train appears, and when Intercity or Commuter trains have to stop: Cargo train 12 left the tracks. Commuter Train 24 entered the tracks at position 0.345 at speed 34kph. You must produce informative output so that we can check form that whether your program is running correctly or not.