Write a program that asks the user to enter five test scores. Assume valid score
ID: 662137 • Letter: W
Question
Write a program that asks the user to enter five test scores. Assume valid scores will be entered, each number followed by a comma.
The program should display a letter grade for each score and the average test score. Write the following functions in the program:
determine_grade - accepts a single number as an argument and displays a letter grade for the score based on the following grading scale:
Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F
calc_average() - takes a list of grades as an argument and displays the average of the scores, along with a letter grade equivalent to that average.
main() - asks the user to enter five test scores and puts them as numbers into a list
showLetters()
Explanation / Answer
Test.java
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Grades: ");
String string = input.next();
String[] items = string.split(",");
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
list.add(Integer.valueOf(items[i]));
}
showLetters(list);
calc_average(list);
}
public static void calc_average(ArrayList<Integer> list) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum = sum + list.get(i);
}
System.out.println("The average is " + sum / 5.0 + " which is "
+ getGrade(sum / 5.0));
}
public static void showLetters(ArrayList<Integer> list) {
for (int i = 0; i < 5; i++) {
System.out.println(Double.parseDouble(list.get(i).toString()) + " is " + getGrade(list.get(i)));
}
}
public static String getGrade(double i) {
if (i >= 90)
return "A";
else if (i >= 80)
return "B";
else if (i >= 70)
return "C";
else if (i >= 60)
return "D";
else
return "F";
}
}
Sample output:
Enter Grades: 65,80,90,71,85
65.0 is D
80.0 is B
90.0 is A
71.0 is C
85.0 is B
The average is 78.2 which is C
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.