Question

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 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 Input Validation: Do not accept a negative number for speed and do not accept any value less than 1 for time traveled.

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

Homework Answers

Answer #1

Java code:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        //initializing speed and time
        int speed,time;
        //asking for speed
        System.out.print("Enter vehicle speed (in mph):");
        //accepting it
        speed=input.nextInt();
        //asking for time
        System.out.print("Enter time travelled (in hrs):");
        //accepting it
        time=input.nextInt();
        //printing Hour→Distance Travelled
        System.out.println("Hour→Distance Travelled");
        //looping from 1 to time
        for(int i=1;i<=time;i++)
        //printing time and Distance travelled
            System.out.println(i+"→→"+i*speed);
   }
}

Screenshot:


Input and Output:

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
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...
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...
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...
1. It is advertised that the average braking distance for a small car traveling at 65...
1. It is advertised that the average braking distance for a small car traveling at 65 miles per hour equals 120 feet. A transportation researcher wants to determine if the statement made in the advertisement is false. She randomly test drives 36 small cars at 65 miles per hour and records the braking distance. The sample average braking distance is computed as 114 feet. Assume that the population standard deviation is 22 feet. a. H0: average breaking distance for small...
Q1.Energy is defined as the ability to do work. the conservation of displacement. the distance traveled...
Q1.Energy is defined as the ability to do work. the conservation of displacement. the distance traveled divided by time. the time rate of doing work. Q2.While rearranging a dorm room, a student does 300 J of work in moving a desk 2.0 m. What was the magnitude of the applied horizontal force? 150 N 600 N 0 N 2940 N Q3.Power is force divided by time. is work divided by time. is work times time. has the same units as...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
For many years, Thomson Company manufactured a single product called LEC 40. Then three years ago,...
For many years, Thomson Company manufactured a single product called LEC 40. Then three years ago, the company automated a portion of its plant and at the same time introduced a second product called LEC 90 that has become increasingly popular. The LEC 90 is a more complex product, requiring 0.80 hours of direct labor time per unit to manufacture and extensive machining in the automated portion of the plant. The LEC 40 requires only 0.40 hours of direct labor...
Monroe County is trying to determine where to place the county fire station. The locations of...
Monroe County is trying to determine where to place the county fire station. The locations of the county’s four major towns are as follows: (10, 20), (60, 20), (40, 30), and (80, 60) (see Figure 7.50). Town 1 averages 40 fires per year; town 2, 25 fires; town 3, 20 fires; and town 4, 30 fires. The county wants to build the fire station in a location that minimizes the average distance that a fire engine must travel to respond...
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...