Question: [Task 3] In Lab2, Task 2, you wrote a program called GradeStatistics which prompted the user for the number of students in a class (a

[Task 3] In Lab2, Task 2, you wrote a program called GradeStatistics which prompted the user for the number of students in a class (a non-negative integer), and saved it in an int variable called numStudents. It then prompted the user for the grade of each student (integer between 0 to 100) and saved them in an int array called grades. This approach works well for a class of small size, say less than 10 students. However, it does not work well if the class size is large, say 100 students. Update the GradeStatistics code to pull data from a file instead. Use the supplied file grades.txt. The first line in this file is the number of students = 100. The next 100 lines are the grades of these students. Once you read data from the file, save the number of students to an int variable called numStudents and the grades to an int array called grades. The program shall then compute and print the average (in double rounded to 2 decimal places), the minimum and the maximum (in int) to a file called results.txt. Include the possible errors and exceptions that can happen while dealing with Files.

GradeStatistics Code:

import java.util.*;

public class GradeStatistics {

public static void main(String[] args) { int numStudents; //number of students. int grades []; //An array that holds grades for each student. double average; //Should be rounded to two decimal places. int minimum; //Holds the lowest grade\. int maximum; //Holds the highest grade. System.out.println("Enter the number of students: "); //We create a scanner object to hold input. Scanner input = new Scanner(System.in); numStudents = input.nextInt(); //This while loop assures that if a negative integer is inputed, we would ask the user to re-input a valid value. while (numStudents <= 0) { System.out.println("Please enter a non negative number: "); numStudents = input.nextInt(); } grades = new int[numStudents]; //We create a count variable of the integer type so as to iterate through each student. int count = 1; //This for loop prompts the user for the grade of each student. for (int i = 0; i < grades.length; i++) { System.out.println("Enter the grade for the student " + count + ":"); count++; grades[i] = input.nextInt(); //This while loop assures that if a negative integer is inputed, // or an integer greater 100, we would ask the user to re-input a valid value. while (grades[i] < 0 || grades[i] > 100) { System.out.println("Please enter a number between 0 - 100: "); grades[i] = input.nextInt(); } } double sum = 0; //This for loop calculates the sum of all of the values of the array. for(double num : grades) { sum = sum+num; } //We then divide the sum by the length of the array to get the average and assigned it to the average variable. average = sum/grades.length; //We print out the average. System.out.printf("The average is: " + "%.2f", average); System.out.println(""); //We call the getMinValue() method and feed it the grades array so as to calculate the minimum value of the array. //After which we print the minimum. minimum = getMinValue(grades); System.out.println("The minimum is: " + minimum); //We call the getMaxValue() method and feed it the grades array so as to calculate the maximum value of the array. //After which we print the maximum. maximum = getMaxValue(grades); System.out.println("The maximum is: " + maximum); }

//Method that finds the maximum value in an array by comparing one value to the next. public static int getMaxValue(int[] grades) { int maxValue = grades[0]; for (int i = 1; i < grades.length; i++) { if (grades[i] > maxValue) { maxValue = grades[i]; } } return maxValue; } //Method that finds the minimum value in an array by comparing one value to the next. public static int getMinValue(int[] grades) { int minValue = grades [0]; for (int i = 1; i < grades.length; i++) { if (grades[i] < minValue) { minValue = grades[i]; } } return minValue; } }

grades.txt file:

100 97 90 24 20 46 5 15 91 11 4 25 80 98 94 3 12 47 64 16 1 82 57 49 59 99 22 84 31 68 6 40 7 45 44 29 75 23 34 39 34 86 98 45 45 4 55 65 86 94 37 34 23 80 75 59 3 54 20 72 37 51 55 89 92 58 90 72 57 0 71 60 71 18 94 2 93 16 0 47 8 74 95 77 98 46 75 28 88 73 6 79 65 47 86 48 45 64 25 28 59

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!