go to previous page   go to home page   go to next page
ArrayOps.printRange( ar1, 3, 3);

Answer:

This will print out element 3 of the array, the value 5.


Sum Method

Here is the ArrayOps class, now including a new method. This new method adds up all the elements in an array.

class ArrayOps
{
  // previous methods go here

  // add up all the elements in an array
  public static 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 ) 
  {
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
 
    System.out.println("The sum of elements is: " + ArrayOps.sumElements( ar1 ) );   
  }

}

The declaration of the method says that it expects a reference to an array of int as a parameter, and that it will return an int back to the caller when it is done:

public static int sumElements ( int[] nums )

QUESTION 16:

Fill in the blanks of the sumElements() method.


go to previous page   go to home page   go to next page