Complete Question:
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8."
Answer:
import java.util.Arrays;
import java.util.Scanner;
public class SumOfExcess {
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
final int NUM_VALS = 5;
int[] testGrades = new int[NUM_VALS];
int sumExtra = 0;
System.out.println("Enter the test scores for the five courses");
testGrades[0]= in.nextInt();
for(int i =1; i<testGrades.length; i++){
System.out.println("Enter next score");
testGrades[i]= in.nextInt();
}
System.out.println("The test scores are "+ Arrays.toString(testGrades));
//Finding the sum of excess credit
for(int i = 0 ;i < testGrades.length; ++i){
if(testGrades[i] > 100){
sumExtra = testGrades[i] - 100 + sumExtra;
}
else {
}
}
System.out.println("Total sumExtra: " + sumExtra);
}
}
Step-by-step explanation:
- This has been solved using Java programming language
- create the array of type int and of length NUM_VALS in this case 5 int[] testGrades = new int[NUM_VALS];
- Use a for loop to request user to enter the values for the credits
- Use Java's Arrays.toString Method to display the array after all the values have been entered
- Create and initialize the variable sumExtra
- Using a second for loop iterate the entire array, using an if statement determine values above 100, subtract the extra value and add to the variable sumExtra