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...
Variation Direct Variation y = kx is equivalent to the statements y varies directly as x...
Variation Direct Variation y = kx is equivalent to the statements y varies directly as x y is directly proportional to x Worksheet 1. The number of patients Dr. Wong can see varies directly as the time it takes her to see them. If she can see 11 patients in 3 hours, how long it take her to see 66 patients? 2. The speed of a vehicle varies inversely as the time it takes to travel a fixed distance. If...
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...
you have been tasked with developing a Java Swing GUI that displays time, traffic signals and...
you have been tasked with developing a Java Swing GUI that displays time, traffic signals and other information for traffic analysts. The final GUI design is up to you but should include viewing ports/panels to display the following components of the simulation: 1. Current time stamps in 1 second intervals 2. Real-time Traffic light display for three major intersections 3. X, Y positions and speed of up to 3 cars as they traverse each of the 3 intersections Some of...
1.If a car's speedometer indicates a constant speed of 30 miles per hour, can it be...
1.If a car's speedometer indicates a constant speed of 30 miles per hour, can it be say that the car is not accelerating? 2. Describe an example of something that has constant speed and at the same time a variable speed. Can you describe an example of something that has a constant speed and variable speed? Sustain your answers.   3.Describe an example of a body experiencing acceleration when traveling rapidly constant. Is it possible to describe an example of a...
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)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT