Java Program: Write a program that prompts the user to input 10 grades for a tes
ID: 3690092 • Letter: J
Question
Java Program:
Write a program that prompts the user to input 10 grades for a test. You are to use a method to find the average grade for the tests and methods to find the minimum grade and maximum grade for the test. You are only required to create a main program that accepts 10 inputs from the user for the grade amounts (they must be between 0 and 100, inclusive). If the user enters an amount outside the range, they should be prompted to enter the value again. You must use an array to store the valid values the user enters. Recall that average is the sum of the values divided by the number of values. The method averageGrade() should return the average for all the values in the array. The method minGrade() should return the minimum value in the array. The method maxGrade() should return the largest value from the array.
Test your program using the test data given below and call the averageGrade() , minGrade() and maxGrade() methods defined above passing in the values. Must use the test data that I give you. Please use this practice wisely to test yourself.
These are the values to test with:
87.4
99
91.2
88
87.2
87
89
65
44.2
125-------error reprompt the user for the correct number
99.3
-------------------------------------------------
Example of what your output should look like:
The test grades are:
87.40
99.00
91.20
88.00
87.20
87.00
89.00
65.00
44.20
99.30
The Average is: 83.73
The Minimum Grade is: 44.20
The Maximum Grade is 99.30
Explanation / Answer
TestGrades.java
package org.students;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class TestGrades
{
static NumberFormat f;
public static void main(String[] args)
{
//To format the number
f=NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(2);
Scanner sc=new Scanner(System.in);
double testg[]=new double[10];
//This for loop will take the grades from the user and display Error Message if User entered not valid Grade.
for(int i=0;i<10;i++)
{
while(true)
{
System.out.print("Enter the Test Grades "+(i+1)+" ::");
testg[i]=sc.nextDouble();
if(testg[i]<0 || testg[i]>100)
{
System.out.println(":: Enter Valid Grade ::");
continue;
}
else
break;
}//End of while Loop
}//End of For loop
//Calling the Methods
double avg=averageGrade(testg);
System.out.println("The Average is ::"+f.format(avg));
double min=minGrade(testg);
System.out.println("The Minimum Grade is ::"+f.format(min));
double max=maxGrade(testg);
System.out.println("The Maximum Grade is ::"+f.format(max));
}
//Method is used to Calculates the Maxuimum Grade from the array
private static double maxGrade(double[] testg)
{
double max=testg[0];
for(int i=0;i<testg.length;i++)
{
if(testg[i]>max)
{
max=testg[i];
}
}
return max;
}
//Method is used to Calculates the Minimum Grade from the array
private static double minGrade(double[] testg)
{
double min=testg[0];
for(int i=0;i<testg.length;i++)
{
if(testg[i]<min)
{
min=testg[i];
}
}
return min;
}
//Method is used to Calculates the Average Grade from the array
private static double averageGrade(double[] testg)
{
double sum=0.0,avg=0.0;
System.out.println("The Test Grades are ::");
for(int i=0;i<testg.length;i++)
{
System.out.println(f.format(testg[i]));
sum+=testg[i];
}
avg=sum/testg.length;
return avg;
}
}
_______________________________________________________________________________________
output:
Enter the Test Grades 1 ::87.4
Enter the Test Grades 2 ::99
Enter the Test Grades 3 ::91.2
Enter the Test Grades 4 ::88
Enter the Test Grades 5 ::87.2
Enter the Test Grades 6 ::87
Enter the Test Grades 7 ::89
Enter the Test Grades 8 ::65
Enter the Test Grades 9 ::44.2
Enter the Test Grades 10 ::125
:: Enter Valid Grade ::
Enter the Test Grades 10 ::99.3
The Test Grades are ::
87.40
99.00
91.20
88.00
87.20
87.00
89.00
65.00
44.20
99.30
The Average is ::83.73
The Minimum Grade is ::44.20
The Maximum Grade is ::99.30
___________________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.