91.6k views
3 votes
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

User YJR
by
4.4k points

2 Answers

5 votes

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

User Drowe
by
4.9k points
6 votes

Answer:

/**

a for loop that sets sumExtra to the total extra credit received.

**/

import java.util.Scanner;

public class SumOf Excess {

public static void main (String [

args) {

final int NUM_VALS

int [] testGrades = 4;

new int [NUM_VALS];

int i;

int sumExtra = -9999;

/** Assign

sumExtra with 0 before your

for loop**/

testGrades [0] = 101;

testGrades [ 1] = 83;

testGrades [2] = 107;

testGrades [3] = 90;

sumExtra = 0;

for(i = 0 ;i < testGrades.length

:++i){

if(testGrades [i]> 100){

testGrades [i] = sumExtra - 100+ SumExtra;

}

else {

System.out.println("sumExtra:

sumExtra);

}

}

Step-by-step explanation:

total extra credit received. Full credit

is 100, SO anything over 100 is

extra credit.

Ex If testGrades = {101, 83, 107, 90

then sum Extra = 8, because 1 +0+ 7

+0 is 8.

User Romeozor
by
4.8k points