In: Computer Science
IN JAVA
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
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Main.java
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.
Scanner readFile;
try {
readFile = new
Scanner(new File(fileName));
// 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
while
(readFile.hasNext()) {
array[numElems] = readFile.nextDouble();
numElems++;
}
// Part
C
// Close the
file
readFile.close();
} catch (FileNotFoundException
e) {
e.printStackTrace();
System.exit(0);
}
// Part D
// return the number of elements in
the array
return numElems;
}
public static void printTimes(double[] array, int
numElems) {
// Part E
// Write the loop to print the
array to the screen as shown
for (int i = 0; i < numElems;
i++) {
System.out.println(array[i]);
}
}
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
if (numE == 0) {
return -1;
} else {
double sum = 0,
avg = 0.0;
for (int i = 0;
i < numE; i++) {
sum += numbers[i];
}
avg = sum /
numE;
return
avg;
}
}
// 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
private static double findFastest(double[] times, int
numRacers) {
double min=0.0;
if(numRacers==0)
return -1;
else
{
min=times[0];
for(int
i=0;i<numRacers;i++)
{
if(min>times[i])
{
min=times[i];
}
}
}
return min;
}
}
=========================================
Output#1:
=========================================
Output#2:
=====================Could you plz rate me well.Thank You