Click here to Skip to main content
15,896,290 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

Java
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) //Hint 5
            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");
        });
    }
Posted
Updated 17-Nov-21 20:20pm
v2
Comments
Richard MacCutchan 16-Nov-21 3:13am    
Your program only accepts integer values for the grades so 55.5 is invalid. Assuming the program reads that as 55 then the average will be 75.666.

1 solution

When collecting grades, you use the following line: double grade = sc.nextInt();.
The method nextInt() can only be used to collect int values from user input. So the value saved at grade will lose it's precision.

You need to call nextDouble() or work entirely with Integers.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900