In: Computer Science
AverageGrade. Assume the professor gives five exams during the semester with grades 0 through 100 and drops one of the exams with the lowest grade. Write a program to find the average of the remaining four grades. The program should use a class named Exams that has 1. An instance variable to hold a list of five grades, 2. A method that calculate and return average. 3. A string that return the “exams Average” for printing. Your program output should prompt for an input for each grade. Use any number between 0 and 100. Your submission should include compiled output.
use java
Code:
import java.util.*;
import java.util.Scanner;
class Exams{
public static int grades[]=new int[5]; //it is instance variable of class to holds grades
public static double avarage=0;
public static double calavg(int[] g){
double sum=0;
Arrays.sort(g); //short the grades because we dont want to calculate avarage of lowest grade
for(int i = 1; i < 5; i++) //so here lowest grade is present at 0th index in grade array so we start from 1
{
sum+=g[i]; //it calculate sum of all grades excluding lowest grade
}
avarage=sum/4; //calculate avarage of grades excluding lowest grade
return avarage;
}
public static String print() //return string with avarage
{
return "Your avarage of grades excluding lowest grade is : "+avarage;
}
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.print("Enter your 5 grades from 0 to 100 :\n");
for(int i = 0; i < 5; i++) //it will take 5 grades from user
{
grades[i] = s.nextInt(); //store 5 grades into array
}
calavg(grades); //call function to calculate avarage of grades
System.out.print(print()); //print avarage of grdes
}
}
Output: