Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is the output of this code? Explain how did you made the file and provide a pic of the out put. import java.io.File; import

What is the output of this code?

Explain how did you made the file and provide a pic of the out put.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class XYProjectTwo{
    public static void main(String[] args) throws FileNotFoundException {
        try (Scanner console = new Scanner(System.in)) {
System.out.print("Enter the filename: ");
String filename = console.nextLine();

File file = new File(filename);
try (Scanner inputFile = new Scanner(file)) {
int numStudents = inputFile.nextInt();
int numQuizzes = inputFile.nextInt();

int[][] grades = new int[numStudents][numQuizzes];
String[] name = new String[numStudents];

for (int i = 0; i < numStudents; i++) {
    name[i] = inputFile.next();
    for (int j = 0; j < numQuizzes; j++) {
        grades[i][j] = inputFile.nextInt();
    }
}

displayQuizAverages(grades);
displayStudentAverages(grades, name);
displayHighestAverage(grades, name);
}
}
    }

    public static void displayQuizAverages(int[][] grades) {
        System.out.println("QuiztAverage");
        for (int j = 0; j < grades[0].length; j++) {
            double quizTotal = 0;
            int numStudentsWithGrades = 0;
            for (int i = 0; i < grades.length; i++) {
                if (grades[i][j] != 0) {
                    quizTotal += grades[i][j];
                    numStudentsWithGrades++;
                }
            }
            double quizAverage = quizTotal / numStudentsWithGrades;
            System.out.printf("%dt%.1f", j+1, quizAverage);
        }
    }

    public static void displayStudentAverages(int[][] grades, String[] name) {
        System.out.println("StudenttAverage");
        for (int i = 0; i < grades.length; i++) {
            int total = 0;
            for (int j = 0; j < grades[i].length; j++) {
                total += grades[i][j];
            }
            double average = (double) total / grades[i].length;
            System.out.printf("%-15st%.1f", name[i], average);
        }
    }

    public static void displayHighestAverage(int[][] grades, String[] name) {
        double highestAverage = 0;
        int highestStudent = 0;
        for (int i = 0; i < grades.length; i++) {
            int total = 0;
            int numGrades = 0;
            for (int j = 0; j < grades[i].length; j++) {
                if (grades[i][j] != 0) {
                    total += grades[i][j];
                    numGrades++;
                }
            }
            double average = (double) total / numGrades;
            if (average > highestAverage) {
                highestAverage = average;
                highestStudent = i;
            }
        }
        System.out.printf("%s had the highest overall average of %.1f", name[highestStudent], highestAverage);
    }
}

Step by Step Solution

3.54 Rating (151 Votes )

There are 3 Steps involved in it

Step: 1

This Java program reads data from a file specified by the user and processes it to calculate quiz av... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting Information Systems

Authors: George H. Bodnar, William S. Hopwood

11th Edition

0132871939, 978-0132871938

More Books

Students also viewed these Programming questions