I need this before the end of the day please :)
In Java
10.13 Lab 10
Lab 10
This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time.
In BlueJ create a project called Lab10
Text File: times1.txt
12.4321 23.543 10.23 16.342 21.12
Text File: times2.txt
14.473 17.5 21.178 11.8 9.874 18.71 19.801 14.310 20.7 12.78 9.915 11.789
main method to be used for lab
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Main { public static void main(String [] args) { //DO NOT CHANGE MAIN double average; double fastest; double [] times; times = new double[50]; int numRacers; numRacers = fillArray(times); System.out.println("\nThere were " + numRacers + " people in the race "); // Hint: You may want to comment out the following lines of code until you // get the method fillArray to work (use the debugger to check if it is // working. Then uncomment the next method call you are implementing and // get that to work. Do one method at a time. System.out.println("\nThe times were: "); printTimes(times, numRacers); average = findAverage(times, numRacers); System.out.printf("The average time was: %.2f%n", average); fastest = findFastest(times, numRacers); System.out.printf("The fast time was: %.2f%n", fastest); } public static int fillArray(double [] array) { int numElems = 0; Scanner keyboard = new Scanner(System.in); String fileName; System.out.print("Enter the file name: "); fileName = keyboard.next(); // Part A // Declare a input file Scanner and link it to the filename the user // typed in. Make sure the file opens correctly and if it doesn't print // and error message to the screen and exit the program. // Part B // Code the loop that will read in the numbers from the file and put them // into the array. The integer numElems should keep track of how many numbers // are being placed into the array // Part C // Close the file // Part D // return the number of elements in the array } public static void printTimes(double [] array, int numElems) { // Part E // Write the loop to print the array to the screen as shown } public static double findAverage(double [] numbers, int numE) { // Part F // Write the code required to calculate the average of all of the // numbers in the array The method should return the average. If // there are no elements in the array it should return a -1 } // Part G // Write the method to find the fastest time in the array. The method // should return the fastest time. If there are no times in the array // the method should return a -1 }
Sample Output 1
Enter the file name: times1.txt There were 5 people in the race The times were: 12.43 23.54 10.23 16.34 21.12 The average time was: 16.73 The fast time was: 10.23
Sample Output 2
Enter the file name: times2.txt There were 12 people in the race The times were: 14.47 17.50 21.18 11.80 9.87 18.71 19.80 14.31 20.70 12.78 9.92 11.79 The average time was: 15.24 The fast time was: 9.87
Code for Part A:
File file = new File(fileName);
try {
Scanner sc = new Scanner(file,
StandardCharsets.UTF_8.name());
}
catch (IOException e) {
System.out.println("File could not be opened");
}
Code for Part B:
int i = 0;
while (sc.hasNextLine()){
times[i] = sc.nextDouble();
i++;
}
numElems = i;
System.out.println("Number of Elements = " +
numElems);
Code for Part C:
sc = null;
file = null;
Code for Part D:
return numElems;
Code for Part E:
for (int i=0; i<numElems; i++) {
System.out.println(times[i]);
}
Code for Part F:
double sum=0, avg;
if (numElems == 0) {
return -1;
}
for (int i=0; i<numElems; i++) {
sum += times[i];
}
avg = sum / numElems;
return avg;
Code for Part G:
double fastest;
if (numElems == 0) {
return -1;
}
fastest = times[0]
for (int i=1; i<numElems; i++) {
if (times[i] > fastest) {
fastest =
times[i];
}
}
return fastest;
Get Answers For Free
Most questions answered within 1 hours.