What is the output of this code? Explain how did you made the file and provide a
Fantastic news! We've Found the answer you've been seeking!
Question:
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);
}
}
Related Book For
Accounting Information Systems
ISBN: 9780132871938
11th Edition
Authors: George H. Bodnar, William S. Hopwood
Posted Date: