Question

Java Question. Can someone write this code? Instructions This program will read in test data for...

Java Question. Can someone write this code?

Instructions

This program will read in test data for students(you should download the input file "PairGrades.txt" shown below) Your job is to produce an output file "average.txt" which displays the student's name once in uppercase and then a list of the grades, 4 underscores, the average for the grades, and an empty line. After all the students' information is listed, you should display the student with the highest average. One sentence should be displayed indicating which student scored the highest average.

The challenge will be getting the output file to look EXACTLY like below. You will need to display the average with exactly one digit after the decimal (use printf %.1f )

You will need to keep track of the previous and next student's name so you can group the scores together.

Example Input - PairGrades.txt file

Anna 85.5 98.5
Anna 67.4
ANNA 78.5 23.4
Carl 99.4 78.5
carl 45.2 
Farzana 87.5 77.8 45.2
FARZANA 90.2
Farzana 23.1

Example Output - Average.txt file

Here is a sample run of the program.

ANNA
  85.5
  98.5
  67.4
  78.5
  23.4
  ____
  avg = 70.7%

CARL
  99.4
  78.5
  45.2
  ____
  avg = 74.4%

FARZANA
  87.5
  77.8
  45.2
  90.2
  23.1
  ____
  avg = 64.8%

3 students were analyzed.  
CARL scored the highest average: 74.4%
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;

public class Demo252{

    public static void main(String[] args){
       String[] name = new String[20];
       double[][] marks = new double[20][10];
       int[] count = new int[20];
       for (int i = 0; i<20; i++)
           count[i] = 0;
       try {
           FileInputStream fin = new FileInputStream("PairGrades.txt");
           FileOutputStream fout = new FileOutputStream("Average.txt");
           PrintWriter pw = new PrintWriter(fout);
           Scanner fc = new Scanner(fin);
           int n = 0;
           while(fc.hasNextLine()){
              String line = fc.nextLine();
              String[] list = line.split(" ");
              int found = 0;
              for (int i = 0; i<n; i++){
                 if (name[i].equalsIgnoreCase(list[0])){
                    found = 1;
                    for (int j = 1; j<list.length; j++){
                        marks[i][count[i]] = Double.parseDouble(list[j]);
                        count[i]++;
                    }
                 }
              }
              if (found == 0){
                 name[n] = list[0].toUpperCase();
                 for (int j = 1; j<list.length; j++){
                        marks[n][count[n]] = Double.parseDouble(list[j]);
                        count[n]++;
                 }               
                 n++;
              }
           }
           fc.close();
           double max = 0;
           int maxi = 0;
           for (int i = 0; i<n; i++){
               pw.println(name[i]);
               double sum = 0;
               for (int j = 0; j<count[i]; j++){
                  pw.printf("%.1f\n",marks[i][j]);
                  sum = sum + marks[i][j];
               }
               pw.println("---");
               double avg = sum/count[i];
               pw.printf("avg = %.1f%s\n", avg,"%");
               pw.println("\n");
               if (avg > max){
                  max = avg;
                  maxi = i;
               }
           }
           pw.println(n + " students were analyzed");
           pw.printf("%s scored highest average: %.1f%s\n",name[maxi],max,"%");
           pw.close();
       }
       catch(Exception e){
          e.printStackTrace();
       }
    }
}

Add a comment
Know the answer?
Add Answer to:
Java Question. Can someone write this code? Instructions This program will read in test data for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT