A local high school is giving a standardized test to all of its students. There
ID: 3869385 • Letter: A
Question
A local high school is giving a standardized test to all of its students. There are 50 questions on the test and the school would like you to gather some statistics on how the students performed. Scores can range anywhere from 0 to 50. All the scores are stored in a file (scores.txt) that you will need to download. Input the data from the file and store the data into an array. To do this, you will have to have your program open the data file, count the number of elements in the file, close the file, initialize your array, reopen the file and load the data into the array. Then calculate and output the following statistics: average score, the mode of the scores, and the highest/lowest score. In addition, output a list of the scores and the number of students that received each score. (Hint: you may need to use additional arrays to store information.) Your program should be written using methods and should be well documented internally and externally. Your output should be displayed using Message boxes.
Explanation / Answer
scores.txt
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 35 36 37 38 39 40 41 42 43 44 45 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 38 39 40 41 42 20 21 22 23 24 25 20 21 22 23 24 25 26 27 28 29 30 26 27 28 29 30 26 27 28 29 30 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 31 32 33 34 35 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 40 41 42 43 44 45 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 45 46 47 48 49 50 48 49 50
____________________
StudentTestScores.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentTestScores {
public static void main(String[] args) throws FileNotFoundException {
//Declaring Scanner class Reference
Scanner sc = null;
int marks[] = null;
//Declaring variables
int count = 0, mode, max, min;
double total = 0, avg = 0.0;
//Opening the file
sc = new Scanner(new File("scores.txt"));
//Reading the data from the file
while (sc.hasNext()) {
sc.nextInt();
//Counting how many no of Students are there
count++;
}
//Colsing the file
sc.close();
//Creating an array
marks = new int[count];
//Opening the file
sc = new Scanner(new File("scores.txt"));
int index = 0;
//Reading the data from the file and populating the data into the array
while (sc.hasNext()) {
marks[index] = sc.nextInt();
total += marks[index];
index++;
}
sc.close();
//calculating the average
avg = total / count;
//Displaying the average of test scores
System.out.printf("The Average of test Scores is :%.2f ", avg);
//Calling the method
mode = findMode(marks);
//Displaying the mode
System.out.println("The Mode of test Scores is :" + mode);
//Calling the method
max = findMax(marks);
//Displaying the maximum value of all test scores
System.out.println("The Maximum of test Scores is :" + max);
//Calling the method
min = findMin(marks);
//Displaying the minimum value of all test scores
System.out.println("The Minimum of test Scores is :" + min);
listOfScores(marks);
}
//Displaying the test scores and no of students who got that test scores
private static void listOfScores(int[] marks) {
int students[] = new int[51];
for (int i = 0; i < marks.length; i++) {
students[marks[i]]++;
}
System.out.println(" Displaying Students test Scores :");
for (int i = 0; i < 51; i++) {
System.out.println("No of Students who got " + i + " marks is :" + students[i]);
}
}
//Method which finds the minimum value of an array
private static int findMin(int[] marks) {
int max = marks[0];
for (int i = 0; i < marks.length; i++) {
if (max < marks[i])
max = marks[i];
}
return max;
}
//Method which finds the maximum value of an array
private static int findMax(int[] marks) {
int min = marks[0];
for (int i = 0; i < marks.length; i++) {
if (min > marks[i])
min = marks[i];
}
return min;
}
//Method which finds the mode of an array
private static int findMode(int[] marks) {
int maxVal = 0, maxCnt = 0;
for (int i = 0; i < marks.length; ++i) {
int cnt = 0;
for (int j = 0; j < marks.length; ++j) {
if (marks[j] == marks[i])
++cnt;
}
if (cnt > maxCnt) {
maxCnt = cnt;
maxVal = marks[i];
}
}
return maxVal;
}
}
__________________________
Output:
The Average of test Scores is :38.31
The Mode of test Scores is :40
The Maximum of test Scores is :20
The Minimum of test Scores is :50
Displaying Students test Scores :
No of Students who got 0 marks is :0
No of Students who got 1 marks is :0
No of Students who got 2 marks is :0
No of Students who got 3 marks is :0
No of Students who got 4 marks is :0
No of Students who got 5 marks is :0
No of Students who got 6 marks is :0
No of Students who got 7 marks is :0
No of Students who got 8 marks is :0
No of Students who got 9 marks is :0
No of Students who got 10 marks is :0
No of Students who got 11 marks is :0
No of Students who got 12 marks is :0
No of Students who got 13 marks is :0
No of Students who got 14 marks is :0
No of Students who got 15 marks is :0
No of Students who got 16 marks is :0
No of Students who got 17 marks is :0
No of Students who got 18 marks is :0
No of Students who got 19 marks is :0
No of Students who got 20 marks is :20
No of Students who got 21 marks is :20
No of Students who got 22 marks is :20
No of Students who got 23 marks is :20
No of Students who got 24 marks is :20
No of Students who got 25 marks is :20
No of Students who got 26 marks is :24
No of Students who got 27 marks is :24
No of Students who got 28 marks is :24
No of Students who got 29 marks is :24
No of Students who got 30 marks is :24
No of Students who got 31 marks is :32
No of Students who got 32 marks is :32
No of Students who got 33 marks is :32
No of Students who got 34 marks is :32
No of Students who got 35 marks is :64
No of Students who got 36 marks is :44
No of Students who got 37 marks is :44
No of Students who got 38 marks is :88
No of Students who got 39 marks is :88
No of Students who got 40 marks is :108
No of Students who got 41 marks is :108
No of Students who got 42 marks is :108
No of Students who got 43 marks is :64
No of Students who got 44 marks is :64
No of Students who got 45 marks is :92
No of Students who got 46 marks is :40
No of Students who got 47 marks is :40
No of Students who got 48 marks is :44
No of Students who got 49 marks is :44
No of Students who got 50 marks is :44
__________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.