Completed printRange()

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

Answer:

The complete program follows:

Completed printRange()

It is OK to use the same parameter names (like x) and the same local variable names (like index) in several methods. The scope of parameters and local variables is limited to the method in which they are declared.

class ArrayOps
{
  . . . . // other methods

  // print elements start through end
  void printRange ( int[] x, int start, int end )
  {
    for ( int index=start; index <= end ; index++ )
      System.out.print( x[index] + " " );
    System.out.println();
  }

}

class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    ArrayOps operate = new ArrayOps();
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
    
    // print elements at indexes 1, 2, 3, 4, 5
    operate.printRange( ar1, 1, 5 );
  }

}      

When printRange() is called, the three actual values given in the call are copied to the parameters of printRange(). The parameter x refers to the array, start gets the value "1", end gets the value "5".

QUESTION 11:

What does this program print out?