Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need screenshot of the output of this code import java.util.Scanner; public class ScoreAverage { //implement a named constant private static final int SUBJECTS=5; //The
I need screenshot of the output of this code
import java.util.Scanner;
public class ScoreAverage {
//implement a named constant
private static final int SUBJECTS=5;
//The class implements only one main method.
public static void main(String[] args) {
System.out.println("This program will calculate the average score of each student ");
//A Scanner class should be implemented
Scanner scanner = new Scanner(System.in);
int studentCount = 0;
//implement either a while or do
while (true) {
System.out.print("Enter student count: ");
studentCount = scanner.nextInt();
//implement if or switch structures
if (studentCount > 0) {
break;
} else {
System.out.print("Please enter a postive number");
}
}
//The class should declare variables.
int[][] scores = new int[studentCount][SUBJECTS];
for (int student = 0; student < studentCount; student++) {
System.out.println("Enter scores for Student - " + (student + 1));
for (int subject = 0; subject < SUBJECTS; subject++) {
scanner.nextLine();
System.out.print("Enter score for subject - " + (subject + 1) + ": ");
int num = scanner.nextInt();
scores[student][subject] = num;
}
}
//The class should declare variables.
//a double dimension array
double[] averageScores = getAverageScore(scores);
//implement formatted output (4.6), as well as unformatted
//displaying formatted output.
System.out.println(String.format("%-20s%8s", "Student", "Average"));
for (int student = 0; student < averageScores.length; student++) {
//implement formatted output (4.6), as well as unformatted
//displaying formatted output.
System.out.println(String.format("%-20s%8.2f",
"Student " + (student + 1),
averageScores[student]
));
}
}
//he class implements methods beyond the main method
private static double[] getAverageScore(int[][] scores) {
double[] averageScores = new double[scores.length];
//for loop use to traverse the array;
for (int student = 0; student < scores.length; student++) {
double sumScore = 0;
for (int score : scores[student]) {
sumScore += score;
}
averageScores[student] = sumScore / SUBJECTS;
}
return averageScores;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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