Programming Lab 7: Distance File

Objective: To produce a formatted text file report showing distance travelled over time

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. It should use a loop to output 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 save a report similar to the one that follows:

Hour             Distance Traveled
----------------------------------
  1                     40.0
  2                     80.0
  3                    120.0
Input Validation: Do not accept a negative number for speed and do not accept any value less than 1 for time traveled.

You must use at least one method in addition to main for this program. Don't use any class-level variables. Each variable should be declared inside a method.

Write the report to a file instead of the screen. Open the file in Notepad, pico, or another text editor to confirm the output.

Here is the sample output. Remember that the Unix command to display a text file is "cat", so that's how I displayed the file below. Notice the way the numbers line up vertically, and how results are rounded. Yours should do the same:

-bash-3.2$ java DistanceFile
Enter the vehicle's speed: 120
Enter the number of hours the vehicle was in motion: 11
Report written to DistanceReport.txt.
-bash-3.2$ cat DistanceReport.txt
Hour             Distance Traveled
----------------------------------
  1                    120.0
  2                    240.0
  3                    360.0
  4                    480.0
  5                    600.0
  6                    720.0
  7                    840.0
  8                    960.0
  9                  1,080.0
 10                  1,200.0
 11                  1,320.0
-bash-3.2$ java DistanceFile
Enter the vehicle's speed: -2
Enter 0 or greater for speed: 3.25
Enter the number of hours the vehicle was in motion: 0
Enter 1 or greater for hours: 4
Report written to DistanceReport.txt.
-bash-3.2$ cat DistanceReport.txt
Hour             Distance Traveled
----------------------------------
  1                      3.3
  2                      6.5
  3                      9.8
  4                     13.0
-bash-3.2$
When you turn in your homework, make sure you turn in your Java source code, the screen output, and the file which your program produced - you can copy and paste the file output, clearly labeling it, so I can see all of this in one text file. Please show at least 2 sample outputs and files, as I did above.

For 5% Extra Credit

Your program should input the total time in minutes, and also ask the user how many minutes should pass between each line of output. The total number of minutes must be 60 or greater, and the number of minutes to pass between each output must be between 1 and the total number of minutes. Make sure to show the distance after the total time has passed, even if means that fewer minutes pass between the last two lines of output. Here is the sample output:
-bash-3.2$ java DistanceFileExtra
Enter the vehicle's speed in miles per hour: 100
Enter the number of minutes the vehicle was in motion: 180
How many minutes should be skipped between lines in the report? 30
Report written to DistanceReport.txt.
-bash-3.2$ cat DistanceReport.txt
Minute           Distance Traveled
----------------------------------
 30                     50.0
 60                    100.0
 90                    150.0
120                    200.0
150                    250.0
180                    300.0
-bash-3.2$ java DistanceFileExtra
Enter the vehicle's speed in miles per hour: 75
Enter the number of minutes the vehicle was in motion: 5
Enter 60 or greater for minutes: 95
How many minutes should be skipped between lines in the report? 100
Minutes skipped must be betwen 1 and 95: 15
Report written to DistanceReport.txt.
-bash-3.2$ cat DistanceReport.txt
Minute           Distance Traveled
----------------------------------
 15                     18.8
 30                     37.5
 45                     56.3
 60                     75.0
 75                     93.8
 90                    112.5
 95                    118.8
-bash-3.2$

Return to CS 111A page