| ||Punctuality is not exactly one of Dr. Wesley's strengths. His student John always shows up for class at the scheduled starting time, but he never knows when Dr. Wesley will arrive. This is a problem for John because he cannot stay in the same place doing nothing for a long period of time. Therefore, he has come up with the following method to reduce the amount of time he spends waiting by the classroom door.
Overall, John stands in front of the class door from time 0 to time waitTime, inclusive, then from time walkTime + waitTime to time walkTime + 2*waitTime, inclusive, and so on. At all other time moments he walks outside.
- John arrives at time 0.
- John waits for Dr. Wesley to arrive.
- If after waitTime seconds Dr. Wesley has not arrived yet, John goes to take a walk for walkTime seconds.
- John therefore arrives back to the classroom exactly at time waitTime + walkTime.
- If Dr. Wesley has not arrived yet, John waits another waitTime seconds and then proceeds to take a new walk. The process is repeated until John becomes aware that Dr. Wesley has arrived.
This has solved John's patience issues, but it has caused a new problem. When John returns from a walk, if Dr. Wesley is already in the classroom, it will appear that John has arrived late to class. Dr. Wesley does not like that, and he will deny John access to the class if he arrives lateTime or more seconds after the time at which Dr. Wesley arrived.
There are multiple variables that affect Dr. Wesley's arrival time, but for the purpose of the problem, assume that the arrival time will be a real number chosen uniformly from bestArrival to worstArrival, inclusive. Return the probability that John will be denied access to Dr. Wesley's class.
|Parameters:||int, int, int, int, int|
|Method signature:||double getProbability(int waitTime, int walkTime, int lateTime, int bestArrival, int worstArrival)|
|(be sure your method is public)|
|-||The returned value must have an absolute or relative error less than 1e-9.|
|-||waitTime, walkTime, lateTime and worstArrival will each be between 1 and 10000000, inclusive.|
|-||bestArrival will be between 0 and worstArrival, inclusive.|
|The professor will arrive at some random moment between 0 and 50 seconds, inclusive. During this interval, these are John's activities:|
John will only be too late if the professor arrives while he is away, and more than 10 seconds before he returns.
This only happens if the professor arrives strictly between times 20 and 40. The probability that this will happen is (40-20)/(50-0) = 0.4.
- He waits from time 0 to time 20, inclusive.
- He takes a walk between times 20 and 50, non-inclusive.
- He arrives back at time 50.
|If the professor arrives between 30 and 40 seconds or between 70 and 90 seconds, John will be late to the class.|
|John's walking time is 40 seconds. Hence, even if the professor arrives while John is taking a walk, John will always be less than 50 seconds late.|
|Dr. Wesley will always arrive at time 90 seconds. John will arrive at time 100 seconds after taking two different walks. Since the time difference is equal to 10 seconds, John will be too late.|