Write a program which takes the grades from ten quizzes and computes the average
ID: 3816494 • Letter: W
Question
Write a program which takes the grades from ten quizzes and computes the average quiz score. However, the teacher usually drops the lowest two scores. You must take this into consideration when computing the average quiz grade. Your program must implement the following methods: readQuizScore: This method asks and reads one quiz score from the console, validates the score, and then returns the score. This method will be called once for each score. computeAverage: This method takes the ten scores as input, computes the average, and returns the average. getLowestscore: This method takes the ten scores as input and returns the lowest score of the tens cores. getsecondLewestscore: This method takes the ten scores as input, and returns the second lowest score of the ten scores. Display the two lowest scores, and the correct average. Input Validation; Each score must be a positive value between 0.0 and 100.0. Requirements: All four of the required methods must be implemented, and all must be correctly used within your program to get credit. Round the solution to two decimal places. You are only allowed to ask the user to enter the 10 scores once. You will need to figure out how to pass the scores to your methods In order to find the solution. Sample Execution: Enter the ten scores: 100.0 78.45 89.23 98.00 67.87 88.29 82.67 87.50 90.56 94.38 Two Lowest Scores: 67.87 and 78.45 Average: 91.33Explanation / Answer
QuizScores.java
import java.util.ArrayList;
import java.util.Scanner;
public class QuizScores {
static Scanner sc=null;
public static void main(String[] args) {
//Declaring variables
double score,average,lowest,secondLowest;
ArrayList<Double> arl=new ArrayList<Double>();
//getting the inputs entered by the user
System.out.println("Enter ten scores :");
for(int i=0;i<10;i++)
{
score=readQuizScore();
arl.add(score);
}
//calling the methods
average=computeAverage(arl);
lowest=getLowestScore(arl);
secondLowest=getSecondLowest(arl);
//displaying the outputs
System.out.println("Two Lowest Scores :"+lowest+" and "+secondLowest);
System.out.println("Average :"+average);
}
//this method will find the second lowest element
private static double getSecondLowest(ArrayList<Double> arl) {
double min=getLowestScore(arl);
double secondMin=arl.get(0);
for(int i=0;i<arl.size();i++)
{
if(secondMin>arl.get(i) && arl.get(i)!=min)
{
secondMin=arl.get(i);
}
}
return secondMin;
}
//this method will find the lowest element
private static double getLowestScore(ArrayList<Double> arl) {
double min=arl.get(0);
for(int i=0;i<arl.size();i++)
{
if(min>arl.get(i))
min=arl.get(i);
}
return min;
}
//this method will calculate the average
private static double computeAverage(ArrayList<Double> arl) {
double sum=0.0,avg=0.0;
for(int i=0;i<arl.size();i++)
{
sum+=arl.get(i);
}
avg=(sum/arl.size());
return avg;
}
//this method will read the inputs entered by the user
private static double readQuizScore() {
double score,ret = 0;
//Scanner object is used to get the inputs entered by the user
sc=new Scanner(System.in);
score=sc.nextDouble();
if(score<0.0 || score>100.0)
System.out.println("Invalid.Must be between 0.0 -100.0");
else
ret=score;
return ret;
}
}
____________________
Output:
Enter ten scores :
100.0
78.45
89.23
98.00
67.87
88.29
82.67
87.50
90.56
94.38
Two Lowest Scores :67.87 and 78.45
Average :87.695
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.