Write a Java Program that can compute the mean, median, the highest, and the lowest of the students’ scores in a class. First, the user inputs the number of students in the class and you capture this number. Then the user inputs the grades of all the students in the class. After capturing all the inputs, your program should output the mean, the median, the highest, and the lowest.

Respuesta :

Answer:

import java.util.Arrays; // The Array class allows for array operations

import java.util.Scanner; // The Scanner class allows for user's inputs

public class Statistics {

public static void main(String[] args) {

 // create an object of the Scanner class

 Scanner input = new Scanner(System.in);

 System.out.println("Please enter the number of students");

 // Using "input" which is an object of the Scanner class,

 // get the number of students.

 int noOfStudents = input.nextInt();

 // Create an array of scores whose length is equal to the number of students

 // Array is of type double as students' scores could be floating point numbers

 double[] scores = new double[noOfStudents];

 // Create a loop that cycles as many times as the number of students.

 // Each loops asks the user to enter the score of each student

 // Initialize the scores array with each score at every loop

 int i = 0; // This is a counter for the loop

 while (i < noOfStudents) {

  System.out.println("Enter score for student " + (i + 1));

  scores[i] = input.nextDouble();

  i++; // increment counter at every loop

 }

 // At the end of the loop above, the scores array should be completely

 // initialized

 // You might want to print out the scores before sorting

 System.out.println(Arrays.toString(scores));

 // Sort the scores array so as to be able to calculate the median

 Arrays.sort(scores);

 // Print out the results by calling the necessary methods

 System.out.println("Mean score : " + mean(scores));

 System.out.println("Median score is : " + median(scores));

 System.out.println("Highest score : " + highest(scores));

 System.out.println("Lowest score : " + lowest(scores));

}

// Method to calculate the mean of the scores in the scores array

// Initialize a variable, sum, to zero to allow for cumulative addition of the

// array elements.

// Loop through the scores array adding up the array elements cumulatively

// Divide the result of the sum by the length of the array to get the

// mean(average).

// In this case, scores array is represented by arr

public static double mean(double[] arr) {

 double sum = 0;

 for (int i = 0; i < arr.length; i++) {

  sum += arr[i];

 }

 return sum / arr.length; // Return the mean (average).

 

} // End of the mean method  

 

 

// Method to calculate the median of the scores in the scores array

// Median is the middle element in a sorted set of elements

// If the length(number of elements) of the sorted set of elements is even

// there will be two elements representing the median.

// To break this tie, the average of the two elements is found.

// In this case, scores array is represented by arr

public static double median(double[] arr) {

 //Check if the length of the array is even or not.

 //This will help to break the tie if it occurs.

 

 if (arr.length % 2 != 0) {

  int mid = (arr.length + 1) / 2;   //mid represents the middle number

  return arr[mid - 1];    // Because arrays are indexed from zero(0), 1 is subtracted from mid

 } else {

  int mid = (arr.length) / 2;

  return (arr[mid] + arr[mid + 1]) / 2.0;

 }

} // End of the method median

 

 

// Method to calculate the highest of the scores in the scores array

// In this case, the scores array is represented by arr

public static double highest(double[] arr) {

 

 //Declare and initialize a temp variable to hold the first element in the array

 double temp = arr[0];

 //Loop through the array starting at the second position since the value in the first position has been stored in a temp variable

 for (int i = 1; i < arr.length; i++) {

  //At every loop, check if there is a number greater than the one stored in the temp variable

  //If there is, replace temp with such number

  if (arr[i] > temp) {

   temp = arr[i];

  }

 }

 return temp; //Return the highest number

} // End of the method highest

 

 

// Method to calculate the lowest of the scores in the scores array

// In this case the scores array is represented by arr

public static double lowest(double[] arr) {

 

 //Declare and initialize a temp variable to hold the first element in the array

 double temp = arr[0];

 //Loop through the array starting at the second position since the value in the first position has been stored in a temp variable

 for (int i = 1; i < arr.length; i++) {

  //At every loop, check if there is a number less than the one stored in the temp variable

  //If there is, replace temp with such number

  if (arr[i] < temp) {

   temp = arr[i];

  }

 }

 return temp; //Return the lowest number

} // End of the method lowest

}

Explanation:

Explanations are explicitly written in the answer above in comments.

The source code file (Statistics.java) has been attached to this answer.

Hope this helps!.

Ver imagen stigawithfun