Primitive Parameters

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:

Yes.

Primitive Parameters

Often parameters are used to "fine-tune" the actions performed by a method. For example, imagine that you want to print only some of the elements of an array. Here is the ArrayOps class definition with a new method added:

import java.io.*;

class ArrayOps
{
  . . . . // other methods

  // print elements start through end
  void printRange ( int[] x, int start, int end )
  {
    for ( int  ;  ;  )
      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 );
  }

}      

The method printRange() prints the elements from start to (and including) end. In this particular program, main() uses the method to print the elements at indexes 1 through 5. The values 19, 1, 5, -1, 27 are printed.

QUESTION 10:

Fill in the blanks for printRange(). Assume that start and end are legal indexes for the array.