Write a Java program that prompts the user to enter five test grades between 0 a
ID: 3665428 • Letter: W
Question
Write a Java program that prompts the user to enter five test grades between 0 and 100. Store the values in an array of integers. If the number entered is outside the range, prompt again for a number. Write three methods that do the following:
1.Accept the array as a parameter and return the highest grade in the array.
2.Accept the array as a parameter and return the lowest grade in the array.
3.Accept the array as a parameter and return the average grade in the array. What type of value should be returned?
Print a report of the grades entered, the highest grade, the lowest grade, and the average of the grades.
Sample session (user input in bold):
Enter grade 1 of 5 (0-100): 95
Enter grade 2 of 5 (0-100): 86
Enter grade 3 of 5 (0-100): 109
Enter grade 3 of 5 (0-100): 100
Enter grade 4 of 5 (0-100): 82
Enter grade 5 of 5 (0-100): 90
Grades entered:
95 86 100 82 90
Highest grade: 100
Lowest grade: 82
Average grade: 90.6
Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
public class GradeCalculation {
private static int TOTAL_SIZE = 5;
public static void main(String[] args) {
int[] marks = getMarks();
Arrays.sort(marks);
int highGrade = getHighestGrade(marks);
int lowestGrade = getLowestGrade(marks);
double avgGrade = getAverageGrade(marks);
System.out.println("Highest grade: "+highGrade);
System.out.println("Lowest grade: "+lowestGrade);
System.out.println("Average grade: "+avgGrade);
}
private static int[] getMarks() {
int[] marks = new int[TOTAL_SIZE];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < TOTAL_SIZE; i++) {
System.out.print("Enter grade " + (i + 1) + " of " + TOTAL_SIZE
+ " (0-100):");
marks[i] = scanner.nextInt();
System.out.print(System.lineSeparator());
}
System.out.println("Grades entered:");
for(int i=0;i<TOTAL_SIZE;i++){
System.out.print(marks[i]+" ");
}
System.out.println();
return marks;
}
private static int getHighestGrade(int[] marks) {
return marks[TOTAL_SIZE - 1];
}
private static int getLowestGrade(int[] marks) {
return marks[0];
}
private static double getAverageGrade(int[] marks) {
double avgMark;
int sum = 0;
for (int i = 0; i < TOTAL_SIZE; i++) {
sum += marks[i];
}
avgMark = (double)sum / TOTAL_SIZE;
return avgMark;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.