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

Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and...
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...
2. Create a java program that reads a file line by line and extract the first...
2. Create a java program that reads a file line by line and extract the first word.        The program will ask for the name of input and output file. Input file: words.txt today tomorrow sam peterson peter small roy pratt Output file: outwords.txt tomorrow peterson small pratt
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT