Question: Week 1 1 1 . Review Section 5 . 1 0 in the book about files. 2 . Modify code you wrote for the Distance

Week 11
1. Review Section 5.10 in the book about files.
2. Modify code you wrote for the Distance assignment (#2 page 344) according to the instructions in #3 on page 345.
Submit
ModifiedDistanceDemo.java and txt file that was created.
Note*:
Program template is attached.
To submit all files in one attempt you need to attach all files first and then click Submit. 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!