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:
"All groups have the same score."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
The best performing group is Group 2 with an average score of: 88.0
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();
}
}
Inputs:
Scanner.Comparison:
bestGroup variable.Bonus Task:
"All groups have the same score." instead of declaring a "best" group.Output:
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.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.