A Math teacher is teaching Math course to a set of classes

A Math teacher is teaching Math course to a set of classes (each class may have different number of students) and want to check the behavior of his students in a homework report. The teacher gave the classes the same exam and marked their answer and wants to know the class whose students got the highest average.

Help the teacher in the required analysis of student’s marks by implementing a Java program ClassAverageMArks2DimmArray using 2-dimensional array to store the students marks and then compute the average of each class student’s marks. The program has the following specification:

• A Method titled averageClassMarks for computing the average (as double) of a class students marks (given to the method as a single-dimensional array)

• Another Method titled averageAliclassesMarks for computing the average (as single array of double) of all the classes class student’s marks (given to the method as a 2- dimensional array). This method has to repeatedly call the first method averageClassMarks to calculate the average of student’s marks in each class.

• The program reads from the user the number of classes and then reads the classes student’s marks (as integer values) line by line. Each line has the number of students in the class then the student’s marks.

• The program calls method averageAliclassesMarks to calculate all the average values of marks of all the classes. Again, method averageAliclassesMarks has to repeatedly call the first method averageClassMarks.

• Finally, the program then prints all the average values (rounded to 2 decimal points) of all classes student’s marks and the highest average mark as well.

 • The program has to work EXACTLY as given in the following sample run. Sample Run:

Enter number of classes: 5

Enter student’s marks of all the classes line by line such that, each line contains first the number of students in a class followed by the student’s marks:

6 50 84 76 94 62 98

5 42 64 97 88 80

6 25 35 86 47 36 75

7 59 74 96 94 82 75 98

4 88 62 56 74

Classes average marks:

Class Average

  1.   77.33
  2. 74.20
  3. 50.67
  4. 82.57
  5. 70.00

The highest average is 82.57 and it is for class 4.

==================================

Solution:

Output:

Code:

Main.java:

import java.io.*;
import java.util.*;

public class Main
{
   public static double averageClassMarks(int[] arr)
   {
       int sum=0;
       int n=arr.length;
      
       for(int i=0;i<n;i++)
       {
           sum=sum+arr[i];
       }
      
       double average=(double)sum/n;
      
       return average;
   }
   public static void averageAllClassesMarks(int[][] arr)
   {
       double highest_average=-1;
       int highest_class=0;
      
       int size=arr.length;
       System.out.println(“\nClasses average marks:”);
       System.out.println(“Class   Average”);
       for(int i=0;i<size;i++)
       {
           double average=averageClassMarks(arr[i]);
           System.out.println(i+1+”   “+String.format(“%.2f”,average));
          
           if(average>highest_average)
           {
               highest_average=average;
               highest_class=i+1;
           }
       }
      
       System.out.println(“\nThe highest average is “+String.format(“%.2f”,highest_average)+” and it is for class “+highest_class);
       System.out.println(“==================================================”);
      
   }
  
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
      
       System.out.print(“\nEnter number of classes: “);
       int n=sc.nextInt();
      
       System.out.println(“\nEnter student’s marks of all the classes line by line such that, each line contains first the number of students in a class followed by the student’s marks:\n”);
      
      
       int[][] arr=new int[n][];
      
       for(int i=0;i<n;i++)
       {
           int temp=sc.nextInt();
           arr[i]=new int[temp];
          
           for(int j=0;j<temp;j++)
           {
               arr[i][j]=sc.nextInt();
           }
       }
      
       averageAllClassesMarks(arr);
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *