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]
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]
Comment