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

Java The distance a vehicle travels can be calculated as follows: Distance = Spe

ID: 3756839 • Letter: J

Question

Java

The distance a vehicle travels can be calculated as follows:
Distance = Speed * Time
For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles.

Write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. I should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 mph for a three-hour-time period, it should display a report similar to the one that follows:

Hour    Distance Traveled

1    40

2    80

3 120

Input Validation: Do not accept a negative number for speed and do not accept any value less than 1 for time traveled

can you please type your answer

Explanation / Answer

import java.util.Scanner;

class Distance

{

public static void main(String args[])

{

Scanner in = new Scanner(System.in);

int speed,hours;

System.out.print("Enter Speed of vehicle: ");

speed = in.nextInt();

if(speed<0)

{

System.out.println("Invalid Speed.Speed should be positive");

System.exit(0);

}

System.out.print("Enter No of Hours of travel: ");

hours = in.nextInt();

if(hours<=0)

{

System.out.println("Invalid Hours.Hours should be >=1");

System.exit(0);

}

int i;

System.out.println("Hour Distance Traveled");

for(i=1;i<=hours;i++)

{

System.out.println(i+" "+(i*speed));

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote