In a training course, students are given tests at the end of each module. The number of modules is dependent on the course, and all tests are marked out of 100. At the end of the training program, the students are given the following messages (depending on their average) on their certificate
Average Message
80 and above You have excelled in the course
50 and above but less than 80 You have met the requirements of the course
Below 50 You have failed to meet the minimum requirements
Your task is to write a program that reads a variable number of test scores from the user, calculates the average, displays the average, then prints out the corresponding message.
Note: assume grade, average, and sum are double, while count and numberModules are int. You can assume the user provides valid input.
Test
After writing your program, calculate the average for the following cases manually and determine what message should be produced. Then, run your code, enter the test cases, and see if the output is as expected.
Number of Modules Grades Average Message
3 80, 100, 90
4 60, 80, 75, 65
2 30, 50
What I have tried:
import java.util.Scanner;
class PartB {
public static double calculateAverage(){
Scanner sc = new Scanner(System.in);
int numberOfModules;
double sum = 0;
double count = 0;
System.out.print("Number of Modules: ");
numberOfModules = sc.nextInt();
while(count < numberOfModules) {
double grade = sc.nextInt();
sum += grade;
count += 1;
}
double average = sum / numberOfModules;
System.out.printf("Average: " +average);
System.out.println(" ");
return average;
}
public static void printMessage(double average){
if(average < 50)
System.out.print("You have failed to meet the minimum requirements.");
else if(average <80)
System.out.print("You have met the requirements of the course.");
else
System.out.print("You have excelled in the course");
}
public static void generateCertificate() {
double average = calculateAverage();
printMessage(average);
}
}
This is the error I got
@Test
void testCalculateAverage() throws Exception {
withTextFromSystemIn("3","55.5","90","82").execute(() -> {
PartB pb = new PartB();
double avg = pb.calculateAverage();
assertEquals(75.83333333333333, avg,
"Something isn't quite right with your average calculation\n");
});
}