Question: import java.io . * ; / / Needed for PrintWriter and IOException import java.util.Scanner; / / Needed for Scanner class / * * * Chapter

import java.io.*; // Needed for PrintWriter and IOException
import java.util.Scanner; // Needed for Scanner class
/**
* Chapter 5
* Programming Challenge 3: Distance File
* This program demonstrates the Distance class.
*/
public class ModifiedDistanceDemo
{
public static void main(String[] args)
throws IOException
{
String filename; // File name
double speed; // Vehicle's speed
int maxHours; // Number of hours
int period; // Counter for time periods
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the file name.
System.out.print("The distance report will be "+
"written to a file.
Enter the "+
"file name: ");
filename = keyboard.nextLine();
// Open the file; use PrintWriter class; name the PrintWriter class object outfile.
................................................
// Get the speed.
System.out.print("Enter the vehicle's speed: ");
speed = keyboard.nextDouble();
// Validate the speed.
while (speed <0)
{
...................
...................
}
// Get the number of hours.
System.out.print("Enter the number of hours the "+
"vehicle was in motion: ");
maxHours = keyboard.nextInt();
// Validate the hours.
while (maxHours <1)
{
..........................
..........................
}
// Write the table header.
outFile.println("Hour\tDistance Traveled");
outFile.println("----------------------------------");
// Write the table of distances.
period =1;
while (period <= maxHours)
{
// Create a Distance object for this period.
Distance d = new Distance(speed, period);
// Write the distance for this period.
.............................
// Increment period.
period++;
}
// Close the file.
..................
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!