Test A: -20 19 1 Test B: Test C: 19 1 5 -1 27 19 5
In "Test B" the method is asked to start with an index of -1.
The test in the for loop returns false right away, and
the loop body is never executed.
In "Test C" the method is asked to print beyond the end of the array, but it quits after it has printed the last element.
Here is the ArrayOps class, again,
with a new method.
This new method will add up all the elements in
an array.
class ArrayOps
{
// . . . previous methods go here
// add up all the elements in an array
int sumElements ( int[] nums )
{
int sum = __________;
for ( int __________; ___________; __________ )
__________;
return __________;
}
}
Here is how the method might be used in main():
class ArrayDemo
{
public static void main ( String[] args )
{
ArrayOps operate = new ArrayOps();
int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ;
System.out.println("The sum of elements is: " +
operate.sumElements( ar1 ) );
}
}
The declaration of the method says that it expects an array of
int as a parameter, and that it will return an
int back to the caller when it is done:
int sumElements ( int[] nums )