In: Computer Science
write a program called GradeCalculator that will calculate the grading statistics of a desired number of students.
Your program should start out by prompting the user to enter the number of students in the classroom and the number of exam scores. Your program then prompts for each student’s name and the scores for each exam. The exam scores should be entered as a sequence of numbers separated by blank space. Your program will also check for negative scores. If any score is negative, it will prompt the user to reenter the scores. You do not have to deal with inputs that are not numbers.
For each student, calculate the student’s the average score, lowest score, highest score, letter grade, and the number of stars to give the student. Once the letter grade has been calculated, use it to calculate an appropriate number of stars the student will received. The letter grade is computed as followed: 90 to 100 is A, 80 and less than 90 is B, 70 and less than 80 is C, 60 and less than 70 is D, anything less than 60 is F. The number of stars is assigned as followed: A is 4 stars, B is 3 stars, C is 2 stars, D is 1 star, and F is 0 stars (no star). The program then displays the statistics about the student as shown in the sample run.
Before the program terminates, it prints the class statistics: average grade, lowest grade, highest. See sample output for format.
See sample run to have a better idea of what the program should do.
Grading:
program is expected to have comments. Also comment code that is not obvious. Your program should be beautifully format, well designed, and efficient. Your program should perform as described. Try to match your output with the same run.
Sample Run:
Welcome to GradeCalculator!
Please enter the number of students: 2 Please enter the number of exams : 3
---------------------------------------- Enter student 1’s name : Micheal Brown
Enter exam scores : 90 100 -80
Invalid exam scores, reenter: 90 100 80
Grade statistics for Micheal Brown Average: 90 Letter grade: A Micheal Brown gets 4 stars! ****
---------------------------------------- Enter student 2’s name: Will Smith
Enter exam scores: 80 95 70
Grade statistics for Will Smith Average: 81.66 Letter grade: C Will Smith gets 2 stars! **
---------------------------------------- Class statistics: Average: 85.83 Lowest : 81.66 Highest: 90
Done. Good bye!
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to GradeCalculator!");
System.out.print("Please enter the number of students: ");
int numStud = sc.nextInt();
System.out.print("Please enter the number of exams : ");
int examNum = sc.nextInt();
int []scores = new int[examNum];
int i = 0, num = 0;
double high = 0, low = 0;
while(i < numStud){
System.out.print("Enter student "+(i+1)+"'s name : ");
sc = new Scanner(System.in);
String name = sc.nextLine();
int flag = 0,sum = 0;
System.out.print("Enter exam scores : ");
do{
for (int j = 0; j < scores.length; j++) {
scores[num] = sc.nextInt();
sum+=scores[num];
}
for (int j = 0; j < scores.length; j++) {
if(scores[i] < 0){
break;
}else{
flag = 1;
}
}
if(flag == 1){
break;
}else{
System.out.print("Invalid exam scores, reenter: ");
sum = 0;
}
}while(true);
double avg = ((double)sum/examNum);
if(avg >= 90 && avg <=100 ){
high = avg;
System.out.print("Grade statistics for "+name);
System.out.printf("Average: %.2f",avg);
System.out.print(" Letter grade: A "+name+ " gets 4 stars!
****\n");
}else if(avg>=80 && avg <90){
low = avg;
System.out.print("Grade statistics for "+name);
System.out.printf("Average: %.2f",avg);
System.out.print(" Letter grade: B " +name+" gets 3 stars!
***\n");
}else if(avg >= 70 && avg < 80){
low = avg;
System.out.print("Grade statistics for "+name);
System.out.printf("Average: %.2f",avg);
System.out.print("Letter grade: C " +name+" gets 2 stars!
**\n");
}else if(avg >= 60 && avg < 70){
low = avg;
System.out.print("Grade statistics for "+name);
System.out.printf("Average: %.2f",avg);
}else{System.out.print(" Letter grade: D " +name+" gets 2 stars!
*\n");
low = avg;
System.out.print("Grade statistics for "+name);
System.out.printf("Average: %.2f",avg);
System.out.println( "Letter grade: D " +name+" gets 0
star!\n");
}
i++;
}
System.out.print("Class Statistics: Average: ");
System.out.printf("%.2f ",((double)(high+low)/2));
System.out.printf("Lowest : %.2f ",low);
System.out.printf("Highest: %.2f \n",high);
System.out.println("Done. Good bye!");
}
}
/* OUTPUT */
run:
Welcome to GradeCalculator!
Please enter the number of students: 2
Please enter the number of exams : 3
Enter student 1's name : Micheal Brown
Enter exam scores : 90 100 -80
Invalid exam scores, reenter: 90 100 80
Grade statistics for Micheal BrownAverage: 90.00 Letter grade: A
Micheal Brown gets 4 stars! ****
Enter student 2's name : Will Smith
Enter exam scores : 80 95 70
Grade statistics for Will SmithAverage: 81.67 Letter grade: B Will
Smith gets 3 stars! ***
Class Statistics: Average: 85.83 Lowest : 81.67 Highest:
90.00
Done. Good bye!