Sum Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page
operate.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 with 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
  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 )

QUESTION 15:

Fill in the blanks of the sumElements() method.