Average Calculation Modification: For this program, you will modify the average
ID: 3703999 • Letter: A
Question
Average Calculation Modification: For this program, you will modify the average calculation assignment from Chapter 8. The program should start the same, asking the user to enter 6 test scores and storing them in the array. The program should sort the array in ascending order and display the current average, dropping the lowest score. The final step is to give the user the opportunity to modify one of the grades to see how it affects the average. Ask the user which score they would like to modify and what value to change it to. Modify the score and then display the new average using the new score. Example Output: Enter grade #1 100[Enter] Enter grade #2 90[Enter] Enter grade #3 95[Enter] Enter grade #4 98[Enter] Enter grade #5 88[Enter] Enter grade #6 92[Enter] The low grade of 88 was dropped resulting in an average of 95 Which score would you like to change? 85 [Enter] I could not find that score, please enter a valid score: 88 [Enter] What will this score be changed to? 92 [Enter] The low grade of 90 was dropped resulting in an average of 95.4
Explanation / Answer
AverageDropLowGrade.java
import java.util.Scanner;
public class AverageDropLowGrade {
public static void main(String[] args) {
int grades[] = new int[6];
Scanner scan = new Scanner(System.in);
for(int i=0;i<grades.length;i++) {
System.out.println("Enter grade #"+(i+1)+": ");
grades[i]= scan.nextInt();
}
int min = grades[0],total = 0;
for(int i=0;i<grades.length;i++) {
if(min>grades[i])
min = grades[i];
total+=grades[i];
}
total-=min;
System.out.println("The low grade of "+min+" was dropped resulting in an average of "+(total/(double)(grades.length-1)));
System.out.println("Which score would you like to change? ");
int score = scan.nextInt();
int scoreIndex = 0;
boolean found = false;
while(!found) {
for(int i=0;i<grades.length;i++) {
if(grades[i]==score) {
found=true;
scoreIndex = i;
break;
}
}
if(!found) {
System.out.println("I could not find that score, please enter a valid score: ");
score = scan.nextInt();
}
}
System.out.println("What will this score be changed to? ");
int newScore = scan.nextInt();
grades[scoreIndex]=newScore;
min = grades[0];
total = 0;
for(int i=0;i<grades.length;i++) {
if(min>grades[i])
min = grades[i];
total+=grades[i];
}
total-=min;
System.out.println("The low grade of "+min+" was dropped resulting in an average of "+(total/(double)(grades.length-1)));
}
}
Output:
Enter grade #1:
100
Enter grade #2:
90
Enter grade #3:
95
Enter grade #4:
98
Enter grade #5:
88
Enter grade #6:
92
The low grade of 88 was dropped resulting in an average of 95.0
Which score would you like to change?
85
I could not find that score, please enter a valid score:
88
What will this score be changed to?
92
The low grade of 90 was dropped resulting in an average of 95.4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.