Answered step by step
Verified Expert Solution
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);
}
}
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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started