Compare Average Scores and Find the Best Performing Group

Java Programming Language Array in java (Article) Array in java (Program)

29

Problem Statement:

You are given three different groups, and you need to determine which group has performed the best based on their average scores. Write a Java program that:

  1. Takes the average scores of three groups (Group 1, Group 2, and Group 3) as input.
  2. Compares the average scores.
  3. Outputs the group with the highest average score along with the score.

Requirements:

  • Your program should ask the user to enter the average score for each group.
  • After comparing the average scores, the program should display the group with the highest average.
  • If two or more groups have the same highest score, the program can select any of them as the "best" group.

Bonus Task:

  • Add an additional condition to handle the case where all three groups have the same score. In this case, output a message like: "All groups have the same score."

Given Input:

Enter the average score for Group 1: 75.5
Enter the average score for Group 2: 88.0
Enter the average score for Group 3: 83.5

Expected Output:

The best performing group is Group 2 with an average score of: 88.0

asdasd
Figure: dasd

Program:

import java.util.Scanner;

public class BestPerformingGroup {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Variables to store average scores for each group
        double group1, group2, group3;

        // Input the average scores for Group 1, Group 2, and Group 3
        System.out.print("Enter the average score for Group 1: ");
        group1 = scanner.nextDouble();

        System.out.print("Enter the average score for Group 2: ");
        group2 = scanner.nextDouble();

        System.out.print("Enter the average score for Group 3: ");
        group3 = scanner.nextDouble();

        // Determine the best-performing group
        String bestGroup = "Group 1";
        double highestAverage = group1;

        if (group2 > highestAverage) {
            bestGroup = "Group 2";
            highestAverage = group2;
        }

        if (group3 > highestAverage) {
            bestGroup = "Group 3";
            highestAverage = group3;
        }

        // Bonus Task: Check if all groups have the same score
        if (group1 == group2 && group2 == group3) {
            System.out.println("All groups have the same score.");
        } else {
            // Output the result
            System.out.println("\nThe best performing group is: " + bestGroup 
                + " with an average score of: " + highestAverage);
        }

        // Close the scanner
        scanner.close();
    }
}

Output:


                                        

Explanation:

  • Inputs:

    • The program takes the average scores for Group 1, Group 2, and Group 3 as input from the user using the Scanner.
  • Comparison:

    • The program compares the scores of the three groups and keeps track of the group with the highest score using the bestGroup variable.
  • Bonus Task:

    • If all the groups have the same score, it prints "All groups have the same score." instead of declaring a "best" group.
  • Output:

    • The group with the highest score is displayed along with the score.

Example Input/Output (Bonus Task):

Sample Input:


Enter the average score for Group 1: 80.0
Enter the average score for Group 2: 80.0
Enter the average score for Group 3: 80.0

Sample Output:


All groups have the same score.

This assignment teaches the concepts of comparison logic and basic input/output handling in Java. The bonus task enhances their problem-solving skills by considering edge cases.


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.