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

Lab 4: Distance Traveled (Chapter 4: Programming Challenge #2) The distance a ve

ID: 441252 • Letter: L

Question

Lab 4: Distance Traveled (Chapter 4: Programming Challenge #2) The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour (MPH) for three hours, the distance traveled is 120 miles (40 MPH * 3 hours = 120 miles). Write a program that asks the user for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. Assumption: You may assume that the user will enter positive integer values for both the time and the distance. Here is a sample run (user input is given in <>): Please enter the vehicle

Explanation / Answer

import java.util.*;

class test

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.println(" enter no of hours");

int h = in.nextInt();

System.out.println(" enter no of spped");

int s = in.nextInt();

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

{

System.out.println(" distance travelled in " + i + " hours " + (i*s));

}

}

}