Consider the following instance variable and incomplete method. The method calcTotal is intended to return the sum of all values in vals.
private int[] vals;
public int calcTotal()
{
int total = 0;
/* missing code */
return total;
}
Which of the code segments shown below can be used to replace /* missing code */ so that calcTotal will work as intended?
1)
for (int pos = 0; pos < vals.length; pos++)
{
total += vals[pos];
}
2)
for (int pos = vals.length; pos > 0; pos--)
{
total += vals[pos];
}
3)
int pos = 0;
while (pos < vals.length)
{
total += vals[pos];
pos++;
}
A) I only
B) II only
C) III only
D) I and III
E) II and III