In: Computer Science
In JAVA
• Write a program that calculates the average of a group of test
scores, where the
lowest score in the group is dropped. It should use the following
methods:
• int getScore() should ask the user for a test score, store it in
a reference
parameter variable, and validate it. This method should be called
by main once
for each of the five scores to be entered.
• void calcAverage() should calculate and display the average of
the four highest scores.
This method should be called just once by main, and should be
passed
the five scores.
• int findLowest() should find and return the lowest of the five
scores passed to it.
It should be called by calcAverage, which uses the method to
determine one of
the five scores to drop.
The program should display a letter grade for each score and the
average test
score.
Input Validation: Do not accept test scores lower than 0 or higher
than 100.
*******(use random numbers 50-100. Don't ask the user to enter
data)
********Show all test scores, the one that got dropped and the
average.
*******For random numbers, use the Random class not Math.random()
Program:
import java.util.Scanner;
public class Main
{
public static double calcAvearge(int[] array)
{
int toDrop = findLowest(array);
int sum = 0;
for(int i=0;i<5;i++)
{
if(array[i]!=toDrop)
{
sum =sum + array[i];
}
}
return (double)sum/4;
}
public static int findLowest(int[] arr)
{
int lowest = arr[0];
for(int i=0;i<5;i++)
{
if(arr[i]<lowest)
{
lowest = arr[i];
}
}
return lowest;
}
public static int getScore()
{
Scanner s = new Scanner(System.in);
System.out.print("Enter score: ");
int userScore = s.nextInt();
return userScore;
}
public static void main(String[] args) {
double avg;
int lowest;
int[] score = new int[5];
for(int i=0;i<5;i++)
{
while(true)
{
int value = getScore();
if(value>0 && value<100)
{
score[i] = value;
break;
}
else
{
System.out.println("Invalid score, Enter
again.");
continue;
}
}
}
avg = calcAvearge(score);
System.out.println();
for(int i=0;i<5;i++)
{
System.out.println();
System.out.print("Score "+ score[i] +" and grade is:
");
if(score[i]>=90)
{
System.out.print("A");
}
else if(score[i]>=80 &&
score[i]<=89)
{
System.out.print("B");
}
else if(score[i]>=70 &&
score[i]<=79)
{
System.out.print("C");
}
else if(score[i]>=60 &&
score[i]<=69)
{
System.out.print("D");
}
else
{
System.out.print("E");
}
}
System.out.println();
System.out.println("Average score is: "+ avg);
}
}
Output: