Question

In: Computer Science

IN JAVA Lab 10 This program reads times of runners in a race from a file...

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

  • Create a class called Main
  • Delete what is in the class you created and copy the Main class below into the file.
  • There are 7 parts of the program that you will have to code. They are labeled Part A through Part G in the comments in the code.
  • The comments for each part will describe the code you are to write.
  • Using notepad, create the two data files given below and save them in the project folder on the hard drive
  • There is also output given below.
  • When finished, submit main to Zybooks

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

Solutions

Expert Solution

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


Related Solutions

Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT