4. (25 pts) Write a complete program that reads the scores from the file midterm
ID: 645193 • Letter: 4
Question
Explanation / Answer
import java.io.*;
import java.util.*;
public class Histogram {
public static void main (String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("midterm.txt"));
int[] counts = new int[101]; // counters of test scores 0-100
while (input.hasNextInt()) { // read file into counts array
int score = input.nextInt();
counts[score]++; // if score is 94,then counts[94]++
}
for (int i=0; i < counts.lenght; i++ ) { // if score is 89,then counts[89]++
if (counts[i] > 0) {
System.out.print(i + " : ");
for (int j = 0; j < counts[i]; j++) {
System.out.print(" ");
}
System.out,println();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.