In this assignment, you will write a Java program that asks 5 students for three
ID: 3793736 • Letter: I
Question
In this assignment, you will write a Java program that asks 5 students for three exam scores each. You should print out the average and letter grade (using a 90/80/70/60 scale for A/B/C/D/F) for each student. Then, you should print the average and average letter grade for the class.
Here is a sample run of the program (user input are the individual grades):
Enter first score for student 1: 72
Enter second score for student 1: 56
Enter third score for student 1: 80
Average for student 1: 69 (D)
Enter first score for student 2: 37
Enter second score for student 2: 90
Enter third score for student 2: 76
Average for student 2: 67 (D)
Enter first score for student 3: 95
Enter second score for student 3: 93
Enter third score for student 3: 88
Average for student 3: 92 (A)
Enter first score for student 4: 75
Enter second score for student 4: 66
Enter third score for student 4: 90
Average for student 4: 77 (C)
Enter first score for student 5: 32
Enter second score for student 5: 78
Enter third score for student 5: 44
Average for student 5: 51 (F)
Overall class average: 71 (C)
Requirements This program should contain a single class (called Proj3) with a main method. Your program should compile and run in BlueJ. Here are some additional requirements/tips: You should have a loop that steps through the 5 students. You will need to add if-statements inside the loop to determine the letter grade. Do not worry about decimals in this program. Use ints for all the numbers you want to store. This may cut off the decimal when you are calculating the average (for example, 67.67 will become 67), but that’s OK. Your program should look EXACTLY like the example above when it runs (except the values of the scores, depending on user input)
Explanation / Answer
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int overallAvg = 0;
for (int i = 1; i <= 5; i++) {
System.out.println("enter the first score for student " + i);
int score1 = scanner.nextInt();
System.out.println("enter the second score for student " + i);
int score2 = scanner.nextInt();
System.out.println("enter the third score for student " + i);
int score3 = scanner.nextInt();
int sum = score1 + score2 + score3;
int avg = sum / 3;
if (avg >= 90) {
System.out.println("average for student " + i + " " + avg
+ ":(A)");
} else if (avg >= 80 || avg < 90) {
System.out.println("average for student " + i + " " + avg
+ ":(B)");
} else if (avg >= 70 || avg < 80) {
System.out.println("average for student " + i + " " + avg
+ ":(C)");
} else if (avg >= 60 || avg < 70) {
System.out.println("average for student " + i + " " + avg
+ ":(D)");
} else {
System.out.println("average for student " + i + " " + avg
+ ":(F)");
}
overallAvg = overallAvg + avg;
}// for
overallAvg=overallAvg/5;
if (overallAvg >= 90) {
System.out.println("overall class average :" + overallAvg + "(A)");
} else if (overallAvg >= 80 || overallAvg < 90) {
System.out.println("overall class average :" + overallAvg + "(B)");
} else if (overallAvg >= 70 || overallAvg < 80) {
System.out.println("overall class average :" + overallAvg + "(c)");
} else if (overallAvg >= 60 || overallAvg < 70) {
System.out.println("overall class average :" + overallAvg + "(D)");
} else {
System.out.println("overall class average :" + overallAvg + "(F)");
}
}// main
}// class
output
enter the first score for student 1
58
enter the second score for student 1
69
enter the third score for student 1
47
average for student 1 58:(B)
enter the first score for student 2
58
enter the second score for student 2
89
enter the third score for student 2
78
average for student 2 75:(B)
enter the first score for student 3
69
enter the second score for student 3
89
enter the third score for student 3
78
average for student 3 78:(B)
enter the first score for student 4
58
enter the second score for student 4
69
enter the third score for student 4
78
average for student 4 68:(B)
enter the first score for student 5
98
enter the second score for student 5
89
enter the third score for student 5
78
average for student 5 88:(B)
overall class average :73(B)