Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

f you follow too close behind another car and that other car stops suddenly, you

ID: 3627076 • Letter: F

Question

f you follow too close behind another car and that other car stops suddenly, you will not be able to stop in time, and you will crash into the rear of that other car. The following formula gives the distance between you and the car in front which allows you to stop safely:

stoppingDistance = speed * (2.25 + speed / 21)




LastNameFirstNameProg4.java

Write a program which determines whether a vehicle’s tailgating distance is safe. The program should read in the speed of the vehicle and the vehicle’s tailgating distance. The program should then calculate the distance that would be required to stop the vehicle.

*
If the stopping distance is less than the tailgating distance, your program should print “No problem.”
*
If the stopping distance equals the tailgating distance, your program should print “Minor wreck.”
*
If the stopping distance is greater than the tailgating distance, your program should print “Major wreck!”

Notice that the expression on the right side of the formula above will force the compiler to convert everything to type double. Use type double for your working variables.

Explanation / Answer

import java.util.Scanner;
class LastNameFirstNameProg4 {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // get the speed of the car
    System.out.print("Enter your speed:(in mph) ");
    double speed = in.nextDouble();
    System.out.print("Enter the tailgating distance:(in feet) ");
    double tailgatingDistance = in.nextDouble();
    double stoppingDistance = speed * (2.25 + (speed/21));
    // print out the type of damage done
    if (stoppingDistance < tailgatingDistance)
      System.out.println("No problem");
    else if (stoppingDistance == tailgatingDistance)
      System.out.println("Minor wreck");
    else if (stoppingDistance > tailgatingDistance)
      System.out.println("Major wreck!");

}
}