Question

In: Computer Science

The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For...

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. Both values are assumed to be integers. It 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:

Hours Distance Traveled
---------------------------
1 40
2 80
3 120

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

Sample Run
java DistanceTravelled

Enter vehicle speed (in mph): 60↵
Enter time travelled (in hrs): 2↵
Hour→Distance Travelled↵
--------------------------↵
1→→60↵
2→→120↵

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// DistanceTravelled.java

import java.util.Scanner;
import java.util.logging.Handler;

public class DistanceTravelled {

   public static void main(String[] args) {
       int speed,time,distance;
      
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       while(true)
       {
       //Getting the input entered by the user
System.out.print("Enter time (in hrs):");
time=sc.nextInt();
if(time<1)
{
   System.out.println("** Invalid.Must be greater than 1 **");
   continue;
}
else
   break;
       }

       while(true)
       {
       //Getting the input entered by the user
System.out.print("Enter speed (in miles/per hr):");
speed=sc.nextInt();
if(speed<0)
{
   System.out.println("** Invalid.Must be greater than 0 **");
   continue;
}
else
   break;
       }
      
       System.out.println("Hours\tDistance Travelled");
       System.out.println("-----\t--------------");
      
       for(int i=1;i<=time;i++)
       {
           distance=i*speed;
           System.out.println(i+"\t"+distance);
       }

   }

}
________________________

Output:

Enter time (in hrs):0
** Invalid.Must be greater than 1 **
Enter time (in hrs):3
Enter speed (in miles/per hr):40
Hours   Distance Travelled
-----   --------------
1   40
2   80
3   120

_______________Could you plz rate me well.Thank You


Related Solutions

PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance...
PROGRAM IN C++ The distance a vehicle travels can be calculated using the following equation: distance = speed * time For example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each...
Over a very long period of time, a sheriff determines the mean vehicle speed on a...
Over a very long period of time, a sheriff determines the mean vehicle speed on a road is 67 miles per hour, the standard deviation is 3 miles per hour. Treat this as the population, not a sample. If the speed limit is 60 miles per hour, what percentage of the vehicles exceed the speed limit? The sheriff cracks down on speeders. His deputies ignore any vehicles going less than 63 mph, and issue warnings to those going between 63...
Vehicle speed on a particular bridge in China can be modeled as normally distributed. (a) If...
Vehicle speed on a particular bridge in China can be modeled as normally distributed. (a) If 5% of all vehicles travel less than 39.13 m/h and 10% travel more than 73.23 m/h, what are the mean and standard deviation of vehicle speed? (b) What is the probability that a randomly selected vehicle's speed is between 50 and 65 m/h? (c) What is the probability that a randomly selected vehicle's speed exceeds the speed limit of 70 m/h?
Vehicle speed on a particular bridge in China can be modeled as normally distributed. (a) If...
Vehicle speed on a particular bridge in China can be modeled as normally distributed. (a) If 5% of all vehicles travel less than 39.19 m/h and 10% travel more than 73.21 m/h, what are the mean and standard deviation of vehicle speed? (Round your answers to three decimal places.) mean standard deviation (b) What is the probability that a randomly selected vehicle's speed is between 50 and 65 m/h? (Round your answer to four decimal places.) (c) What is the...
The power delivered to the wheels of a vehicle (w) as a function of vehicle speed...
The power delivered to the wheels of a vehicle (w) as a function of vehicle speed (V) is given by: w = 0.01417[hp/mph2]V 2 + 0.6300[hp/mph]V - 0.3937[hp] where power is in horsepower and velocity in mph. The amount of heat rejected from the engine block (qb) is approximately equal to the amount of power delivered to the wheel (the rest of the energy from the fuel leaves with the exhaust gas). The heat is removed from the engine by...
A car travels a certain distance along a straight road (in the +x direction). The distance...
A car travels a certain distance along a straight road (in the +x direction). The distance the car travels as a function of time is shown in the figure below. When the car changes speed it does so uniformly. Select the appropriate choice for each statement. It wont let me post the photo of the graph for some reason, so here is a link to it https://d2vlcm61l7u1fs.cloudfront.net/media%2Fa8f%2Fa8f89426-1896-44e8-863a-df989e927825%2FphpeLSO6i.png The acceleration magnitude at 15 sec is ..... the acceleration magnitude at 37...
Vehicle speed on a particular bridge in China can be modeled as normally distributed. If 5%...
Vehicle speed on a particular bridge in China can be modeled as normally distributed. If 5% of all vehicles travel less than 39.15 m/h and 10% travel more than 73.27 m/h, what are the mean and standard deviation of vehicle speed? What is the probability that a randomly selected vehicle's speed is between 50 and 65 m/h? What is the probability that a randomly selected vehicle's speed exceeds the speed limit of 70 m/h?
Vehicle speed on a particular bridge in China can be modeled as normally distributed? If 5%...
Vehicle speed on a particular bridge in China can be modeled as normally distributed? If 5% of all vehicles travel less than 39.18 m/h and 10% travel more than 73.27 m/h, what are the mean and standard deviation of vehicle speed? (Round your answers to three decimal places.) a) mean? standard deviation? b) What is the probability that a randomly selected vehicle's speed is between 50 and 65 m/h? (Round your answer to four decimal places.) c) What is the...
The relationship between the speed of a car and its stopping distance can be modelled by...
The relationship between the speed of a car and its stopping distance can be modelled by the function D= 0.0575v2, where D is the stopping distance, in metres, and v is the speed, in kilometres per hour. a) Express the speed, v, as a function of D, in the form of y = af[k(x-d)] + c b) Explain the meaning of the inverse function. c) Graph each relation on separate axes. Label axes and title each graph. d) State the...
The time a salesperson travels from customer A to B varies but can be described by...
The time a salesperson travels from customer A to B varies but can be described by a Normal probability function with mean 45 minutes and standard deviation 6 minutes. What proportion of the journeys take less than 35 minutes. What proportion of journeys take over 60 minutes. How long should a salesperson allow for journey if they want to be 70% sure that they will not be late?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT