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 vehicleExplanation / 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));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.