Write a program that accepts 10 scores (integer values, 0-100) and calculates an
ID: 3793896 • Letter: W
Question
Write a program that accepts 10 scores (integer values, 0-100) and calculates an overall GPA. The program will need to convert raw scores (0-100) into “grade scores”.
90 -100 ‘A’ 4
80 - 89 ‘B’ 3
70 - 79 ‘C’ 2
60 - 69 ‘D’ 1
0 - 59 ‘F’ 0
If the entry is an integer, but not a valid score (i.e., not in 0-100), the program should print an error message and the scored should not be counted (i.e., if two-of-ten scores are invalid, you should compute the GPA based on 8 scores, not 10).
Print out the GPA (to two decimal places) and the number of scores used to compute the GPA.
Example: “Your GPA, based on # scores, is #.##”
Explanation / Answer
ScoresTest.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class ScoresTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int valid = 0;
int totalGPA = 0;
System.out.println("Enter 10 scores: ");
for(int i=0; i<10; i++){
int score = scan.nextInt();
if(score<0 || score > 100){
System.out.println("Invalid scores.");
}
else{
valid++;
if(score>=90 && score <= 100){
totalGPA = totalGPA + 4;
}
else if(score>=80 && score <90){
totalGPA = totalGPA + 3;
}
else if(score>=70 && score <80){
totalGPA = totalGPA + 2;
}
else if(score>=60 && score <70){
totalGPA = totalGPA + 1;
}
else{
totalGPA = totalGPA + 0;
}
}
}
double finalGPA = totalGPA/(double)valid;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Your GPA, based on "+valid+" scores, is "+df.format(finalGPA));
}
}
Output:
Enter 10 scores:
77
88
99
66
88
101
Invalid scores.
-2
Invalid scores.
55
33
22
Your GPA, based on 8 scores, is 1.62
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.