In: Computer Science
In Java
Write a program that allows Professor Poindexter to get class
averages including the average for allher/his classes. Professor
Poindexter has M classes. M will be input from the terminal. For
each class he/she will input each student’s grade for that class
until 0 is input. Once the goof prof inputs 0, The class average
for that class is output in the form:
Average for Class X 1 is XXX.XX
Finally, once all the M class data is input, the final message will be output:
The Average for All of Professor Poindexter’s M classes is XX.XX
Here is the data you will use for your run:
How many classes does Professor Poindexter Have?
2
Input a student grade for class 1:
100
80
90
0
The Average for Class 1 is XX.XX
Input the grades for class 2:
80
75
90
99
0
The Average for Class 2 is XX.XX
The Average for All of Professor Poindexter’s M classes is XX.XX
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//initializing scanner
Scanner sc=new Scanner(System.in);
int M,i,j,totalStudents;
double marks,classMarks,totalMarks,classAverage;
//taking input for number of batches
System.out.println("How many classes does Professor Poindexter Have?");
M=sc.nextInt();
totalMarks=0.0;
totalStudents=0;
for(i=1 ; i<=M ; i++)
{
System.out.println("Input a student grade for class "+i+" :");
//initializing random inputs for the while loop execution
marks=-999.0;
j=0;
classMarks=0.0;
//while loop will execute till it gets 0 as input
while(marks!=0)
{
//taking marks input
marks=sc.nextDouble();
//calculating total marks for each class
classMarks=classMarks+marks;
//counting number of stuents for the class
if(marks!=0)
{
j++;
}
}
//calculating total marks for all students of all classes
totalMarks=totalMarks+classMarks;
//calculating total students for all students of all classes
totalStudents=totalStudents+(j);
System.out.printf("The Average for Class %d is %.2f\n",i, (classMarks/(j)));
}
//calculating average for all class average
classAverage=totalMarks/totalStudents;
System.out.printf("The Average for All of Professor Poindexter’s M classes is %.2f",classAverage );
}
}