Write a program in java that prompts for an integer N (number of students) and M
ID: 3756509 • Letter: W
Question
Write a program in java that prompts for an integer N (number of students) and M (number of test scores for each students), and allows the user to N*M real numbers.
The program then calculates the average for each student and class average.
(1) Prompt the user to input N students and M test scores, then display the inputs
Enter number of students: You entered: 2
Enter number of test scores: You entered: 3
Enter 6 real numbers:
You entered:
85.3 73.4 98.5
92.1 86.4 88.9
(2) Calculate the averages of each student
(3) Calcuate the class average
The class average is 87.43.
Explanation / Answer
import java.util.*;
import java.text.*;
class classScore{
public static void main(String srgs[]){
int n,m;
double sum=0,classSum = 0,classAvg;
Scanner sc=new Scanner(System.in);
DecimalFormat f = new DecimalFormat("##.00");
System.out.println("Enter number of students: ");
n=sc.nextInt();
System.out.println("Enter number of test scores: ");
m=sc.nextInt();
double scores[][]=new double[n][m];
double avg[]=new double[n];
System.out.println("Enter "+(n*m)+" real numbers:");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scores[i][j]=sc.nextDouble();
}
}
System.out.println("Enter number of students: You entered: "+n);
System.out.println("Enter number of test scores: You entered: "+m);
System.out.println("Enter "+(n*m)+" real numbers:");
System.out.println("You entered:");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(" "+scores[i][j]);
sum=sum+scores[i][j];
}
avg[i]=sum/m;
sum=0;
System.out.println("");
}
System.out.println("There are "+n+" students.");
System.out.println("The averages for each student are");
for(int i=0;i<n;i++)
{
System.out.println(f.format(avg[i]));
classSum=classSum+avg[i];
}
classAvg=classSum/n;
System.out.println("The class average is "+f.format(classAvg));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.