Question

I need this before the end of the day please :) In Java 10.13 Lab 10...

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

  • 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

Homework Answers

Answer #1

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;

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...