This task should render in an executable program in Java. In one (or more) java files, solve the following issues:
Background info:
The distance a vehicle travels can be calculated according to: Distance (s) = Speed (v) * Time (t) For example, if a car travels at an average speed of 50 kilometers per hour for 2 hours, the distance traveled is: s = 50 * 2 = 100 km.

Write a program that requests the speed of a vehicle (kilometers per hour, km / h) and the number of hours (h) in travel time. Use any loop to present the distance a vehicle has traveled for each hour of a period specified by the user. For example, if a vehicle travels at 80 km / h for four hours, a report should be printed on the screen as in Figure 1:
Time (h)   Distance (km / h)
1.                      80
2.                      160
3.                      240
4.                      320
Figure 1.
2. Input Validation: Do not accept negative numbers for speed and do not accept any value less than 1 for travel time.
3. Make the extension so that the program also writes the report to a text file. Open the file in Notes / Notepad or any other text editor to make sure you are done correctly.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

public class Assignment2 {

    public static void main(String[] args){
       // Variables to store speed and number of hours
       double speed;
       int numHours;

       // Scanner to take inputs from console
       Scanner scanner = new Scanner(;

       // Reading speed
       System.out.print("Speed of the vehicle (KM/h): ");
       speed = scanner.nextDouble();

       // Checking if it is a valid input, showing error message and returning
       if(speed < 0){
            System.out.println("Speed must be positive");

       // Reading number of hours travel
       System.out.print("Number of hours travel: ");
       numHours = scanner...

By purchasing this solution you'll be able to access the following files:

50% discount

$10.00 $5.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats