Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. (25 pts) Write a complete program that reads the scores from the file midterm

ID: 645193 • Letter: 4

Question


4. (25 pts) Write a complete program that reads the scores from the file midterm.txt, computes and prints the largest and second largest number in the file. You will need variable to store the largest and second largest numbers. After reading each number, update the variables for the largest and second largest numbers an needed. The file consists of student scores, one score per line. A sample file is given below 46 78 89 32 94 The output for the above file should he: Largest Score is 94 Second Largest Score is 89

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();

   }

   }

   }

   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote