Question

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

Using 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. 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?

Case 1: Initial       -1
2
0
1
Expected   Enter vehicle speed (in mph): Enter vehicle speed (in mph): Enter time travelled (in hrs): Enter time travelled (in hrs): Hour   Distance Travelled
--------------------------
1       2
-1
2
0
1
Final   Enter vehicle speed (in mph):
Enter time travelled (in hrs):
Hour   Distance Travelled
--------------------------------
1 0
1 0
-1
2
0
1
Remarks    The contents of your standard output is incorrect.

Case 2: Initial       60
2
Expected   Enter vehicle speed (in mph): Enter time travelled (in hrs): Hour   Distance Travelled
--------------------------
1       60
2       120
60
2
Final   Enter vehicle speed (in mph):
Enter time travelled (in hrs):
Hour   Distance Travelled
--------------------------------
1 60
2 120
60
2
Remarks    The contents of your standard output is incorrect.

Homework Answers

Answer #1

Solution

import java.util.*;
public class Main
{
        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter vehicle speed (in mph)");
                int Speed=sc.nextInt();
                System.out.println("Enter time travelled (in hrs)");
                int Time=sc.nextInt();
                if(Speed<0 && Time<1){
                    
                    System.out.println("Please Enter Valid Speed and Time \n");
                }
                else{
                        System.out.println("Hour   Distance travelled");
                    for(int i=1;i<=Time;i++){
                       int Distance=Speed*i;
                       System.out.println(i+"         "+Distance);
                        
                    }
                    
                }
        }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
do the java code below and prove that it works. 2. Distance Traveled The distance a...
do the java code below and prove that it works. 2. Distance Traveled 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...
in c++ Write a program that asks the user for the speed of a vehicle (in...
in c++ Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. It should use a loop to display the total distance traveled. The speed traveled should be limited by the fastest speed achieved by a car thus far. Values should not be negative.•Ex: if hours = 3 and speed = 40, then the program should display•Hour 1: Distance Traveled: 40 miles•Hour 2: Distance Traveled: 80...
Part A) Use the definition of linear speed to solve one of the four problems below....
Part A) Use the definition of linear speed to solve one of the four problems below. Show your work. Suppose that an object moves on a circle of radius r at a constant speed. If s is the distance traveled in time t on this circle, then the linear speed v of the object is defined as v=st . 1) An object is traveling around a circle with a radius of 7 feet. If in 40 seconds a central angle...
1. A helicopter spraying fertilizer over a field can fly 5 miles downhill in the same...
1. A helicopter spraying fertilizer over a field can fly 5 miles downhill in the same time as it can fly 4 miles upwind. Find the speed of the wind if the helicopter travels 45 mph in still air when dusting crops. 2. The illumination provided by a car's headlight varies inversely as the square of the distance from the headlight. A car's headlight produces an illumination of 3.75 footcandles at a distance of 40 feet. What is the illumination...
use matlab: ) Computing a distance and travel time between two locations. Obtain user input for...
use matlab: ) Computing a distance and travel time between two locations. Obtain user input for two variables, X and Y, which describe a coordinate location. Assume this coordinate is in units of miles. Write a script that : a) computes the distance D between the two points (0,0) and (X,Y) in kilometers. b) if D is greater than 5 kilometers, assume we drive at a speed of 40 km per hour. Otherwise, assume we walk at a speed of...
Learning Goal: To understand how to use integrated rate laws to solve for concentration. A car...
Learning Goal: To understand how to use integrated rate laws to solve for concentration. A car starts at mile marker 145 on a highway and drives at 55 mi/hr in the direction of decreasing marker numbers. What mile marker will the car reach after 2 hours? This problem can easily be solved by calculating how far the car travels and subtracting that distance from the starting marker of 145. 55 mi/hr×2 hr=110 miles traveled milemarker 145−110 miles=milemarker 35 If we...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
21) A person carries a 25.0-N rock through the path shown in the figure, starting at...
21) A person carries a 25.0-N rock through the path shown in the figure, starting at point A and ending at point B. The total time from A to B is 1.50 min. How much work did gravity do on the rock between A and B? A) 625 J B) 20.0 J C) 275 J D) 75 J E) 0 J 22) A person carries a 2.00-N pebble through the path shown in the figure, starting at point A and...
MATHEMATICS 1. The measure of location which is the most likely to be influenced by extreme...
MATHEMATICS 1. The measure of location which is the most likely to be influenced by extreme values in the data set is the a. range b. median c. mode d. mean 2. If two events are independent, then a. they must be mutually exclusive b. the sum of their probabilities must be equal to one c. their intersection must be zero d. None of these alternatives is correct. any value between 0 to 1 3. Two events, A and B,...
We constantly seem to be pricing ourselves out of some markets and not charging enough in...
We constantly seem to be pricing ourselves out of some markets and not charging enough in others. Our pricing policy is pretty simple: we mark up our full manufacturing cost by 50%. That means a computer that costs us $2,000 to manufacture will sell for $3,000. Until now I thought this was a workable approach, but now I’m not so sure. Steve Works, CEO, Cortland Manufacturing, Inc. (CMI) Steve’s Controller, Sally Nomer, had just told him that she believed the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT