Reading Text From A File For A Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Starbuck01
    New Member
    • Dec 2007
    • 2

    Reading Text From A File For A Program

    I have to write a program for my AP Computer Science Class. Here is the instructions.

    The Police Department is asking for help in catching those who speed. You will write a batch style program (read data from a file) where the first set of data includes the distance in feet of an area marked off on a parkway and the speed limit (miles per hour) for that parkway. The rest of the data includes the time in seconds of how long it took a driver to travel through that marked distance.



    The Police Department wants to know how many drivers were driving faster than the speed limit, and what their average speed is (in miles per hour). Also, find the minimum, maximum, and average speed for ALL drivers.

    You should use several methods in your program, even if the methods are only one or two lines of code each.

    When I did this program, I used the following methods:

    [CODE=java]double carSpeed (double travelTime, double distance);
    // Calculate the speed of car in MPH
    double distanceInMiles (double distance);
    // Change the distance from feet to Miles
    double timeInHours (double travelTime);
    // Change the travel time from seconds to hours
    double average (double total, int driverNumber);
    // Calculate the average speed
    double maximumSpeed (double max, double speed);
    // Calculate the maximum speed
    double minimumSpeed (double min, double speed);
    // Calculate the minimum speed [/CODE]

    You may not assume that you know how much data there is. You may use the file speed.txt which contains following data to test your program:

    distance = 800 feet
    speed limit = 55MPH


    9 s
    12 s
    15 s
    8.5 s
    6.0 s
    20.5 s
    18.5 s
    17.5 s
    10 s
    14.5 s
    12.8 s
    13.75 s
    7.85 s
    9.5 s
    19.5 s
    8 s

    Here is what I have so far...

    [CODE=java]import java.io.FileRea der;
    import java.util.Scann er;


    public class Police_Program
    {
    public static void main(String [] Args)
    {
    Speed_Distance_ Time SDT = new Speed_Distance_ Time();


    Scanner in = new Scanner(inFile) ;
    try
    {
    FileReader inFile = new FileReader("Spe ed.txt");
    }
    catch(Exception e)
    {
    System.out.prin tln("Error: " + e);
    }

    if(carSpeed > 55)
    {
    System.out.prin tln("This driver was over the speed limit")
    }



    }

    }


    class Speed_Distance_ Time
    {

    double carSpeed(double travelTime, double distance)
    {

    double timeInHours = travelTime;
    double carSpeed = distnaceInMiles / travelTime;


    return carSpeed;

    // Calculate the speed of car in MPH
    }
    double distanceInMiles (double distance)
    {
    double feetInAMile = 5280
    double distanceInMiles = distance / feetInAMile;
    return distanceInMiles ;


    // Change the distance from feet to Miles
    }
    double timeInHours (double travelTime)
    {
    double timeInHours = 0; // some number from text file / 60

    return travelTime;

    // Change the travel time from seconds to hours
    }
    double average (double total, int driverNumber)
    {
    double average = 0; // divide by 16
    return average;

    // Calculate the average speed
    }
    double maximumSpeed (double max, double speed)
    {

    return maximumSpeed;

    // Calculate the maximum speed
    }
    double minimumSpeed (double min, double speed)
    {
    double minimumSpeed;
    return minimumSpeed;


    // Calculate the minimum speed
    }

    }[/CODE]
    Last edited by Ganon11; Dec 5 '07, 02:21 AM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I don't see a question...did you have one?

    Comment

    • Starbuck01
      New Member
      • Dec 2007
      • 2

      #3
      The problem is that I do not know how to read the text in from the file that they gave us...

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Starbuck01
        The problem is that I do not know how to read the text in from the file that they gave us...
        How to read and write text files

        Comment

        Working...