• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JAVA

Xylitol

Diamond Member
What would this be?



Consider the following incomplete method:

public int total(int[] scores)
// precondition: The sentinel -999 occurs
// somewhere in scores
{
int k = 0;
int sum = 0;
while (scores[k] != -999)
{
<program statements>
}
return sum;
}

Method total is intended to return the sum of the integers in parameter scores, beginning with the first integer in scores and up to, but not including the sentinel ?999 (which occurs somewhere in scores). Which of the following code segments could be used to replace <program statements> so that total will work as intended?
 
Originally posted by: Xylitol
What would this be?



Consider the following incomplete method:

public int total(int[] scores)
// precondition: The sentinel -999 occurs
// somewhere in scores
{
int k = 0;
int sum = 0;
while (scores[k] != -999)
{
<program statements>
}
return sum;
}

Method total is intended to return the sum of the integers in parameter scores, beginning with the first integer in scores and up to, but not including the sentinel ?999 (which occurs somewhere in scores). Which of the following code segments could be used to replace <program statements> so that total will work as intended?

What would it be? Looks to me like an attempt to get ATOT to do your homework :roll:
 
sum += scores[k];
k++;

GML3G0 beat me to it.
Also if -999 is not in the array it will cause an indexou tof bound to be thrown.
 
Back
Top