Write a java program which: 1. Prompts the user for 5 test scores as integers gr
ID: 3824011 • Letter: W
Question
Write a java program which:
1. Prompts the user for 5 test scores as integers greater than or equal to zero and less than or equal to 100.
2. Validates that the user entries are valid (integers greater than or equal to zero and less than or equal to 100).
3. Stores the 5 tests scores in an array.
4. Uses the bubble sort to sort the scores in ascending order.
5. Calculates the letter grade for each test
6. Displays the letter grade of each score as well as the overall average (the average of the 5 tests).
SPECIFIC REQUIREMENTS
1. You must include a method called calcAverage to compute the average of the 5 test scores; it takes an array of type int as a parameter and returns the average. 2. You must include a method called validateUserInput to ensure user entries are integers greater than or equal to zero and less than or equal to 100.
4. You must include a method called bubbleSort which sorts the values (test scores) stored in the array in ascending order.
5. You must include a method called displayTestScores which displays each test score and its corresponding letter, as well as the overall average.
GENERAL RESTRICTIONS
1. No infinite loops, examples include:
a. for(;;)
b. while(1)
c. while(true)
d. do{//code}while(1);
2. No break statements to exit loops
Output:
Letter score Grade 90 80 andExplanation / Answer
package org.students;
import java.util.Scanner;
public class Grades {
public static void main(String[] args) {
//Declaring variables
int grade = 0;
//Creating array which stores grades
int grades[]=new int[5];
//Creating array which stores grade array
char letterGrades[]=new char[5];
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
int i=0;
do
{
//Getting the grade entered by the user and populating them into an array
System.out.print("Enter Score :"+(i+1)+":");
grade=sc.nextInt();
//Validating the grades entered by the user
if(validateUserInput(grade))
{
grades[i]=grade;
i++;
}
}while(i<5);
//calling the methods
bubbleSort(grades);
determineGrade(grades,letterGrades);
displayTestScores(grades,letterGrades);
}
//This method will display the grade and letter grade
private static void displayTestScores(int[] grades, char[] letterGrades) {
System.out.println(" Score Letter Grade");
for(int i=0;i<grades.length;i++)
{
System.out.println(grades[i]+" "+letterGrades[i]);
}
//Method which calculates the average of grades array
double avg=calcAverage(grades);
//Displaying the average
System.out.println("The Average is :"+avg);
}
//Finding the letter grade for each grade in the grades[] array
private static void determineGrade(int[] grades, char[] letterGrades) {
for(int i=0;i<grades.length;i++)
{
if(grades[i]>=90)
{
letterGrades[i]='A';
}
else if(grades[i]>=80 && grades[i]<90)
{
letterGrades[i]='B';
}
else if(grades[i]>=70 && grades[i]<80)
{
letterGrades[i]='C';
}
else if(grades[i]>=60 && grades[i]<70)
{
letterGrades[i]='D';
}
else if(grades[i]>=0 && grades[i]<60)
{
letterGrades[i]='F';
}
}
}
//Sorting the grades[] array
private static void bubbleSort(int[] grades) {
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < grades.length; i++)
{
for (int j = 1; j < (grades.length-i); j++)
{
if (grades[j-1] > grades[j])
{
temp = grades[j-1];
grades[j-1] = grades[j];
grades[j] = temp;
}
}
}
}
//This method will valid the user entered grade
private static boolean validateUserInput(int grade) {
if(grade<0 || grade>100)
{
System.out.println("that is not an integer X such that :0<=X<=100,try again");
return false;
}
else
return true;
}
//This method will calculate the average of grades array
private static double calcAverage(int[] grades) {
double sum=0.0,avg=0.0;
for(int i=0;i<grades.length;i++)
{
sum+=grades[i];
}
return sum/grades.length;
}
}
_______________________
Output:
Enter Score :1:90
Enter Score :2:1002
that is not an integer X such that :0<=X<=100,try again
Enter Score :2:89
Enter Score :3:78
Enter Score :4:83
Enter Score :5:71
Score Letter Grade
71 C
78 C
83 B
89 B
90 A
The Average is :82.2
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.