For my Java class I\'m having trouble with the following assignment: Standardize
ID: 3824354 • Letter: F
Question
For my Java class I'm having trouble with the following assignment:
Standardized Scores
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.
Here is what I have so far:
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
public class scores
{
public static void main(String[] args) throws IOException
{
String filename = ("scores.txt");
File file = new File(filename);
Scanner inputFile = new Scanner(file);
int ctr = 0;
while (inputFile.hasNextInt())
{
int tempInt = inputFile.nextInt();
ctr++;
}
inputFile.close();
JOptionPane.showMessageDialog(null,"The file contains "+ctr+" test scores.");
}
}
Here are the contents of the txt file:
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
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
import java.io.*;
import javax.swing.*;
public class scores
{
public static double getAverage(int[] scores){
double sum = 0;
for(int i=0; i<scores.length; i++)
sum += scores[i];
return sum/scores.length;
}
public static int maxScore(int []scores){
int max = scores[0];
for(int i=1; i<scores.length; i++)
if(max < scores[i])
max = scores[i];
return max;
}
public static int minScore(int []scores){
int min = scores[0];
for(int i=1; i<scores.length; i++)
if(min > scores[i])
min = scores[i];
return min;
}
public static int getMode(int[] count){
int mode = 1;
for(int i=1; i<count.length; i++){
if(count[i] > mode)
mode = count[i];
}
return mode;
}
public static String getCountScore(int[] count){
String res = "Score Count ";
for(int i=1; i<count.length; i++)
res = res + i+" "+count[i]+" ";
return res;
}
public static void main(String[] args) throws IOException
{
String filename = ("scores.txt");
File file = new File(filename);
Scanner inputFile = new Scanner(file);
int ctr = 0;
while (inputFile.hasNextInt())
{
inputFile.nextInt();
ctr++;
}
JOptionPane.showMessageDialog(null,"The file contains "+ctr+" test scores.");
inputFile.close();
Scanner fileScanner = new Scanner(file);
int scores[] = new int[ctr]; // to store the score
int counts[] = new int[51]; // maintain the count of each score
int i = 0;
while (fileScanner.hasNextInt())
{
int tempInt = fileScanner.nextInt();
scores[i] = tempInt;
counts[tempInt]++; // increasing the count of current read score
i++;
}
JOptionPane.showMessageDialog(null,"Average Score is "+getAverage(scores));
JOptionPane.showMessageDialog(null,"Max Score is "+maxScore(scores));
JOptionPane.showMessageDialog(null,"Min Score is "+minScore(scores));
JOptionPane.showMessageDialog(null,"Mode Score is "+getMode(counts));
JOptionPane.showMessageDialog(null,getCountScore(counts));
fileScanner.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.