How to write a java program to calculates the average, minimum, maximum, median, and standard deviation of the scores ?
Solution:
import java.util.Arrays; import java.util.Scanner; import javax.swing.JOptionPane; /*This project calculates the average, minimum, maximum, median, and standard deviation of the scores. */ public class ScoreCalculator3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); // declare and create a Scanner object which will read user input from keyboard int scores[] = new int[100]; int answer; do { double input; int numScores = 0; double sum = 0; double average = 0.0; double min = 100; double max = 0; double median = 0; double stdev = 0; System.out.println("Please enter scores on a single line and type a -1 at the end "); input = in.nextDouble(); if (input == -1) { System.out.println( "Number of scores : 0"); System.out.println("The average score : 0.00"); System.out.println("The minimum score : 0"); System.out.println("The maximum score : 0"); System.out.println("The median : 0"); System.out.println("The standard deviation: 0"); answer = JOptionPane.showConfirmDialog(null, "Do you want to continue?"); in.nextLine(); continue; } while (input != -1) { // Add input to scores array scores[numScores] = (int)input; // Increment numScores numScores++; input = in.nextDouble(); } in.nextLine(); //average = (double)(sum/(double)numScores); numScores = loadArray(scores); // Print output System.out.println("Number of scores : " + loadArray(scores)); System.out.printf("The average score: %.2f\n", findAverage(scores, numScores)); System.out.println("The minimum score : " + findMinimum(scores, numScores)); System.out.println("The maximum score : " + findMaximum(scores, numScores)); System.out.println("The median score : " + findMedian(scores, numScores)); System.out.println("The standard deviation : " + findSTD(scores, numScores, findAverage(scores, numScores))); answer = JOptionPane.showConfirmDialog(null, "Do you want to continue?"); } while (answer == 0); } public static int loadArray(int[] arr) // load array { int length = 0; for (int i = 0; i < 100; i++) { if (arr[i] != -1) { length++; } else{ break; } } return length; } /** * Compute the average scores * @param arr * @param numberOfScores * @return average */ public static double findAverage(int[] arr, int numberOfScores) { int sum = 0; for (int i = 0; i < numberOfScores; i++) { sum = sum + arr[i]; } return sum / numberOfScores; } /** * Compute the minimum scores for that courses * @param arr * @param numberOfScores * @return minimum */ public static int findMinimum(int[] arr, int numberOfScores) { int min = 100; for (int i = 0; i < numberOfScores; i++) { // If element is lesser than min if (arr[i] < min) { // Declare new min min = arr[i]; } } return min; } /** * Compute the maximum scores for that courses * @param arr * @param numberOfScores * @return maximum */ public static int findMaximum(int[] arr, int numberOfScores) { int max = 0; for (int i = 0; i < numberOfScores; i++) { // If element is more than max if (arr[i] > max) { // Declare new max max = arr[i]; } } return max; } /** * Compute the standard deviation for that courses * @param arr * @param numberOfScores * @param avg * @return STD */ public static double findSTD(int[] arr, int numberOfScores, double avg) { double sum_diff = 0; for (int i = 0; i < numberOfScores; i++) { sum_diff = sum_diff + Math.pow(arr[i] - avg, 2); } return Math.sqrt(sum_diff / numberOfScores); } /** * Compute median scores for that courses * @param arr * @param numberOfScores * @return median */ public static double findMedian(int[] arr, int numberOfScores) { Arrays.sort(arr); // If even number of scores if (numberOfScores % 2 == 0) { int mid = numberOfScores / 2; return (arr[mid-1] + arr[mid])/2; } // If odd number of scores else { int mid = numberOfScores / 2; return arr[mid]; } /* int mid = arr.length/2; if (arr.length%2 == 1) { return arr[mid]; } else { return (arr[mid-1] + arr[mid]) / 2.0; } */ } public static void printArray(int[] arr, int length) { for (int i =0; i < length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } }