Write a program which takes the grades from ten quizzes and computes the average
ID: 3811659 • 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. getSecondLowestscore: 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. 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
import java.math.BigDecimal;
import java.util.Scanner;
public class chegg_score{
private float score;
public static BigDecimal round(float d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd;
}
public float readQuizscores()
{
Scanner scanner = new Scanner(System.in);
score=scanner.nextFloat();
if(!(score>=0.0 && score <=100.0))
{
score=-1.0f;
}
return score;
}
public BigDecimal computeAverage(float[] score_list)
{
int i;
float avg=0f;
BigDecimal result;
for(i=0;i<10;i++)
{
avg=avg+score_list[i];
}
avg=avg/10;
result=round(avg,2);
return result;
}
public float getLowestScore(float[] score_list)
{
float lowest=110f;
int i;
for(i=0;i<10;i++)
{
if(lowest>score_list[i])
{
lowest=score_list[i];
}
}
return lowest;
}
public float getSecondLowestScore(float[] score_list)
{
float first_lowest=110f;
float second_lowest=110f;
int i;
for(i=0;i<10;i++)
{
if(first_lowest>score_list[i])
{
second_lowest=first_lowest;
first_lowest=score_list[i];
}
}
if(second_lowest==110f)
{
second_lowest=score_list[1];
}
return second_lowest;
}
public static void main(String[] args)
{
float score_list[] = new float[] {0f, 0f, 0f, 0f, 0f,0f, 0f, 0f, 0f, 0f};
int iterator;
float first_lowest,second_lowest;
BigDecimal average;
chegg_score scores=new chegg_score();
System.out.println("Enter the ten scores:");
for(iterator=0;iterator<10;iterator++)
{
score_list[iterator]=scores.readQuizscores();
while(score_list[iterator]==-1.0f)
{
System.out.println("Enter valid score: score should be >=0.0 and <=100.0");
score_list[iterator]=scores.readQuizscores();
}
}
average=scores.computeAverage(score_list);
System.out.println("Average:" +average);
first_lowest=scores.getLowestScore(score_list);
second_lowest=scores.getSecondLowestScore(score_list);
System.out.println("Two Lowest scores are:" +first_lowest +' ' +second_lowest );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.