Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following method loops 2 to 5 times and each time it prints out the sum of 4

ID: 3703454 • Letter: T

Question

The following method loops 2 to 5 times and each time it prints out the sum of 4 for random numbers between 1 and 10. Fill in the blanks to complete the program.

public static void sums(Random rnd) {

int loop = rnd.nextInt( _________ ) +_________ ;

// Number of sums to compute
int sum;
int operands = 4; // 4 random numbers

// Loop for each sum
for(int i = 1; i <= loop; i++) {

sum = 0;

// Loop for each operand
for(int j = 1; j <=_________ ; j++) {

int num = rnd.nextInt(_______ ) + 1;

// generate number between 1 and 10
sum +=________ ;
System.out.print( ________);

if(j != _________ )
System.out.print(" + ");

}

System.out.printf(" = %d%n", __________ );
}

}

Explanation / Answer

public static void sums(Random rnd) { int loop = rnd.nextInt(4) + 2; // Number of sums to compute int sum; int operands = 4; // 4 random numbers // Loop for each sum for(int i = 1; i