Write a program to calculate the average test score of each student and the clas
ID: 3685994 • Letter: W
Question
Write a program to calculate the average test score of each student and the class average. The program first prompts the user to enter number of students (N) and the number of test scores (M); and then allows the user to enter N names and N by M test scores Use three arrays: one dimensional array to store the name of N students, a (parallel) two-dimensional array to store N by M test scores, and a (parallel) one-dimensional array to store N averages. You must use the following four methods for this progranm 1) A method for inputting and storing the names and test scores Input to the method: an one-D array (N names) and a two-D array (N by M test scores) Output of the method: none (void method) 2) A method for calculating average for each student Input to the method: a two-D array (N by M test scores) and one-D array (N averages of each student) Output of the method: none (void method) 3) A method for calculating class average Input to the method: an one-D array (N averages of each student) Output of the method: a real number (class average) 4) A method for printing the result Input to the method: an one-D array (N names) a two- D array (N by M test scores), one-D array (N averages of each student), and a real number (class average) Output of the method: none (void method)Explanation / Answer
Program:
import java.util.Scanner;
import java.text.DecimalFormat;
class Student
{
public static void inputTestScores(String[] names,double[][] testScores)
{
int n=names.length,m=testScores[0].length;
Scanner sc = new Scanner(System.in);
for(int i=0;i<n;i++)
{
names[i]=sc.next();
for(int j=0;j<m;j++)
{
testScores[i][j]=sc.nextDouble();
}
}
}
public static void calAverage(double[][] testScores,double[] averages)
{
double sum;
int n=testScores.length,m=testScores[0].length;
for(int i=0;i<n;i++)
{
sum=0;
for(int j=0;j<m;j++)
{
sum=sum+testScores[i][j];
}
averages[i]=sum/m;
}
}
public static double calClassAverage(double[] averages)
{
double sum=0;
int n=averages.length;
for(int i=0;i<n;i++)
{
sum=sum+averages[i];
}
return sum/n;
}
public static void printResult(String[] names,double[][] testScores,double[] averages,double classAverage)
{
int n=names.length,m=testScores[0].length;
System.out.println("Student name Test scores ");
for(int i=0;i<n;i++)
{
System.out.print(names[i]+" ");
for(int j=0;j<m;j++)
{
System.out.print(testScores[i][j]+" ");
}
System.out.println();
}
//Used to display result in two decimal points
DecimalFormat df = new DecimalFormat(".##");
System.out.println("Student name Average Test scores ");
for(int i=0;i<n;i++)
{
System.out.println(names[i]+" "+df.format(averages[i]));
}
System.out.println("The average of the class is : "+df.format(classAverage));
}
public static void main(String[] args)
{
int num,numTestScores;
Scanner sc = new Scanner(System.in);
System.out.println("How many student in the class: ");
num=sc.nextInt();
System.out.println("How many test scores for each student: ");
numTestScores=sc.nextInt();
String[] names = new String[num];
double[][] testScores = new double[num][numTestScores];
double[] averages = new double[num];
System.out.println("Enter "+num+" names and "+numTestScores+" test scores for each name");
inputTestScores(names,testScores);
calAverage(testScores,averages);
double classAverage=calClassAverage(averages);
printResult(names,testScores,averages,classAverage);
}
}
Sample output:
How many student in the class:
4
How many test scores for each student:
3
Enter 4 names and 3 test scores for each name
king 85.5 72 64.5
harria 60.5 88 90
jones 77.6 89 73.5
miller 85 90.5 96
Student name Test scores
king 85.5 72.0 64.5
harria 60.5 88.0 90.0
jones 77.6 89.0 73.5
miller 85.0 90.5 96.0
Student name Average Test scores
king 74.0
harria 79.5
jones 80.03
miller 90.5
The average of the class is : 81.01
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.