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
An instance variable to hold a list of five grades,
A method that calculate and return average.
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.
In Java Please
Step 1 : Save the following code in Exams.java
import java.util.Scanner;
public class Exams {
private int[] marks;
private static int count = 0;
public Exams() {
marks = new int[5];
}
public void addMark(int mark) {
if(count < 5) {
marks[count++] =
mark;
}
}
public float calculateAverage() {
int min = 101;
// First find out minimum
mark
for(int i = 0; i < 5; i++)
{
if(marks[i] <
min) {
min = marks[i];
}
}
// Skip minimum mark and find out
total
int i = 0;
float total = 0;
for(; i < 5; i++) {
if(marks[i] ==
min) {
break;
} else {
total += marks[i];
}
}
i++;
// After skipping minimum add
remaining marks
for(; i < 5; i++) {
total +=
marks[i];
}
return total / (count - 1);
}
@Override
public String toString() {
return "Exams Average = " +
calculateAverage();
}
public static void main(String[] args) {
// Scanner object to read
input
Scanner reader = new
Scanner(System.in);
// Create Exams object
Exams ex = new Exams();
int mark;
System.out.println("Enter the marks
for 5 exams : ");
for(int i = 0; i < 5; i++)
{
System.out.print("Enter mark" + (i+1) + " (0-100): ");
mark =
reader.nextInt();
if(mark < 0
|| mark > 100) {
System.out.println("Invalid input. Please try
again. " + mark);
reader.close();
return;
}
ex.addMark(mark);
}
reader.close();
System.out.println(ex);
}
}
Step 2 : Build the project and run the program
Following is my sample output