In: Computer Science
Java Input, Output and for-loop function.
Practice01) Write-an average SCORE application called TestScores01 This project reads in a list of integers as SCOREs, one per line, until a sentinel value of -1. After user type in -1, the application should print out how many SCOREs are typed in, what is the max SCORE, the 2nd max SCORE, and the min SCORE, the average SCORE after removing the max and min SCORE.
When SCORE >= 90, the school will give this student scholarship.
Count how many scholarships the school will give and print it our in your results.
(hint, you can add all SCOREs to a total first, then when you need the average SCORE without min and max score, you can use the total - (min + max), then divided it by the (number of inputs minus two)
Please enter test scores:
? 90
? 45
? 99
? 68
? 80
?-1
Result:
There are total 4 SCORES
The min SCORE is 45
The max SCORE is 99
The 2nd min SCORE is 68
The 2nd max SCORE is 90
The average SCORE after removing max/min SCORE is: 80
Total 2 scholarships will be given
Explanation:I have written the class AverageScoreApplication which also has the main method to show the output of the program, please find the image attached with the answer.I have used Scanner to take inputs from user.Please upvote if you liked my answer and comment if you need any modification or explanation.
//code
import java.util.Scanner;
public class AverageScoreApplication {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
int countScores = 0, maxScore = -1,
secondMaxScore = -1,
minScore = 1000000, secondMinScore = 1000000,
totalScore = 0,
countScholarship = 0;
System.out.println("Please enter
test scores:");
while (true) {
System.out.print("? ");
int score =
input.nextInt();
if (score == -1)
{
break;
}
countScores++;
totalScore +=
score;
// logic to find
minimum and second minimum score
if (score <
minScore) {
secondMinScore = minScore;
minScore = score;
} else if (score
< secondMinScore && score != minScore)
secondMinScore = score;
if (score
>= 90) {
countScholarship++;
}
// logic to find
maximum and second maximum score
if (score >
maxScore) {
secondMaxScore = maxScore;
maxScore = score;
} else if (score
> secondMaxScore && score < maxScore) {
secondMaxScore = score;
}
}
System.out.println("Result:");
System.out.println("There are total
" + countScores + " SCORES");
System.out.println("The min SCORE
is " + minScore);
System.out.println("The max SCORE
is " + maxScore);
System.out.println("The 2nd min
SCORE is " + secondMinScore);
System.out.println("The 2nd max
SCORE is " + secondMaxScore);
System.out.println("The average
SCORE after removing max/min SCORE is:"
+ (totalScore - (minScore + maxScore)) /
(countScores - 2));
System.out.println(
"Total " + countScholarship + " scholarships
will be given");
input.close();
}
}
Output: