Advertisement
svetlyo0659

Untitled

Feb 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class RaceResults
  5. {
  6.   public static void main(String[] args)
  7.   {
  8.     Scanner in = new Scanner(System.in);
  9.     System.out.println("Enter count of runners:");
  10.     int countOfRacers = in.nextInt(); // how many racers are in the race.
  11.  
  12.     double[] timesArray = new double[countOfRacers]; /// creates an array with the length of countOfRacers.
  13.     double time;
  14.     double averageTime;
  15.  
  16.     for (int i = 0; i < countOfRacers; i++) { /// this loop reads the input from the user and stores it in the arr Array.
  17.       System.out.println("Please enter the time in seconds for runner " + (i + 1));
  18.       time = in.nextDouble();
  19.       timesArray[i] = time;
  20.     }
  21.  
  22.     for (int i = 0; i < timesArray.length; i++) { /// this loop prints on the console the time of each racer.
  23.       System.out.println("Runner number " + (i + 1) + " time is " + timesArray[i] + " seconds");
  24.     }
  25.  
  26.     System.out.println(); // leaves space b/w different statistics.
  27.  
  28.     Arrays.sort(timesArray); //sorts from min to max value if int or double/alphabetical order if String Array!
  29.     System.out.println("The fastest time is " + timesArray[0] + " seconds");
  30.     System.out.println("The slowest time is " + (timesArray[timesArray.length - 1]) + " seconds");
  31.     System.out.println();  // leaves space b/w different statistics.
  32.  
  33.  
  34.     // long lay yo get average value ot the array
  35.     //for (int i = 0; i < timesArray.length; i++) {
  36.     //  averageTime += timesArray[i] / timesArray.length;
  37.     //}
  38.  
  39.     // short way yo get the average value ot the array.
  40.     averageTime = Arrays.stream(timesArray).sum(); // Using Arrays.stream() to convert arr to a stream and .sum()to sum
  41.     System.out.println("Average time is: " + averageTime / timesArray.length + " seconds");
  42.     System.out.println();
  43.  
  44.     //// formula for Standard Deviation
  45.  
  46.     double mean;
  47.     double variance = 0;
  48.     double standardDeviation;
  49.  
  50.     mean = Arrays.stream(timesArray).sum() / timesArray.length;
  51.     System.out.println("Mean = " + mean);
  52.  
  53.     for (int i = 0; i < timesArray.length; i++) {
  54.       variance += (Math.pow(Math.abs(timesArray[i] - mean), 2)) / (timesArray.length - 1);
  55.     }
  56.     System.out.println("Variance = " + variance);
  57.  
  58.     standardDeviation = Math.sqrt(variance);
  59.     System.out.printf("Standard Deviation = " + "%.2f" ,standardDeviation);
  60.     System.out.println(" SD");
  61.  
  62.   }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement