Answer to Question #234891 in Java | JSP | JSF for Momo

Question #234891
Design a Java application that will allow a user to capture the top students for a module. Allow the
user to enter in the module name, followed by the number of students the user would like to
capture.
Continue to capture the results for the number of students entered. Once all the student results
have been captured create a results report.
In the results report display the student results entered, a student count and the average result
obtained.
Make use of a method to get the input from the user, another method to accumulate the student
results, a method to determine the average result and a final method to create the results report.
In your solution make provision if a user has entered in an invalid result amount. Any result
entered that is below zero (0) is an invalid entry.
1
Expert's answer
2021-09-08T16:10:54-0400
import java.util.Scanner;

public class Main {

    private static double getStudentResult(Scanner in) {
        double result = -1;
        while (result < 0) {
            result = Double.parseDouble(in.nextLine());
        }
        return result;
    }

    private static double[] getStudentResults(Scanner in, int numberOfStudents) {
        double[] results = new double[numberOfStudents];
        for (int i = 0; i < results.length; i++) {
            System.out.println("Enter the student result: ");
            results[i] = getStudentResult(in);
        }
        return results;
    }

    private static double getAverageResult(double[] results) {
        double sum = 0;
        for (double result : results) {
            sum += result;
        }
        return sum / results.length;
    }

    private static void showReport(double[] results, String moduleName) {
        System.out.println(moduleName + ", students: " + results.length);
        for (double result : results) {
            System.out.println(result);
        }
        System.out.println("Average: " + getAverageResult(results));
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the module name: ");
        String moduleName = in.nextLine();
        System.out.print("Enter the number of students: ");
        int numberOfStudents = Integer.parseInt(in.nextLine());
        showReport(getStudentResults(in, numberOfStudents), moduleName);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS