Braking distance

Report a typo

Now you need to write a function calculateBrakingDistance that calculates the braking distance of a car.

In order to calculate the distance, you need the initial speed (when braking starts) and the acceleration of the car:

S=v22v122aS = {v_{2}^{2} - v_{1}^{2}\over2a},

where SS is the braking distance, aa is the acceleration, v1v_{1} is the initial speed, and v2v_{2} is the final speed, which in the case of this problem is always equal to 0.

The function accepts String values as input: convert them to Int. Take care that different exceptions may be thrown. If an exception is thrown, display its message using message. It may happen that the acceleration is 0: then you might get an ArithmeticException. If an ArithmeticException is thrown, display the following instead of the exception message: The car does not slow down!.

If the braking distance could not be calculated, return -1.

Input: v1: String, a: String

Output: Int

Don't worry about the units of measurement, they are always SI units.

Sample Input 1:

6
-1

Sample Output 1:

18

Sample Input 2:

10
0

Sample Output 2:

The car does not slow down!
-1

Sample Input 3:

nine
one

Sample Output 3:

For input string: "nine"
-1
Write a program in Kotlin
import kotlin.Exception

fun calculateBrakingDistance(v1: String, a: String): Int {
// write your code here
}
___

Create a free account to access the full topic