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

I am needing to write a program: stoppingDistance = speed * (2.25 + speed / 21)

ID: 3624910 • Letter: I

Question

I am needing to write a program:

stoppingDistance = speed * (2.25 + speed / 21)

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!"

Use type double for your working variables.


This is what I got but it will not compile. Saying that I have 2 errors. I don't even know if I'm writing the code right.


-------------------Code below-----------------

/******************************
* Tailgating.java
*
*
*
*
*******************************/

import java.util.Scanner;

public class Tailgating
{
public static void main(String [] args)
{
Scanner stdIn = new Scanner(System.in);
String response;

System.out.print("Enter speed in mph: ");
speed = stdIn.nextDouble();

System.out.print("Enter tailgate distance in feet:");
distance = stdIn.nextDouble();

stoppingDistance = speed * (2.25 + speed / 21);

if (stoppingDistance < distance)
{
System.out.println("No problem.");
}
else if (stoppingDistance == distance);
{
System.out.println("Minor wreck.");
}
else (stoppingDistance < distance);
{
System.out.println("Major wreck!");
}

} // end main

} // end class

Explanation / Answer

First off, you never declared your variables. double speed; double distance; double stoppingDistance; After that, there was a few errors with your if statements. Mainly, it was double checking for both a minor and a major wreck. If you do one if statement saying if (stoppingDistance >= distance) and then follow it with another if statement by finding out whether it's equal or not, you should get what you're looking for. If (stoppingDistance == distance) {this} else {that} Also, it's unclear if it the formula is (2.25 + speed)/21 or 2.25 + (speed/21). The working code is below: /****************************** * Tailgating.java * * * * *******************************/ import java.util.Scanner; public class Tailgating { public static void main(String[] args) { double speed; double distance; double stoppingDistance; Scanner stdIn = new Scanner(System.in); String response; System.out.print("Enter speed in mph: "); speed = stdIn.nextDouble(); System.out.print("Enter tailgate distance in feet:"); distance = stdIn.nextDouble(); stoppingDistance = speed * (2.25 + speed / 21); if (stoppingDistance = distance) { if (stoppingDistance == distance) { System.out.println("Minor wreck."); } else { System.out.println("Major wreck!"); } } }//end main }