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

Answer:

ar1 is a reference variable holding a reference to the array object.


Complete Program

class ArrayOps
{                          // the parameter x refers to the data
  int findMax( int[] x )   //  this method is called with.                     
  {
    int max = x[0];

    for ( int index=0; index <x.length; index++ )
      if ( x[index] > max )
        max = x[index] ;

    return max ;
  }
}

class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;

    ArrayOps operate = new ArrayOps();     // create an ArrayOps object
    int biggest = operate.findMax( ar1 );  // call the findMax() method with the array
    System.out.println("The maximum is: " + biggest );
  }

}      

The method findMax() gets a reference to the array, so it can access the elements of that array object.

When you run the program it prints out "The maximum is: 27". You might want to copy this program to a file (call it ArrayDemo.java) and play with it.

The complete program contains both classes. If you are using BlueJ, make each a separate class (which will correspond to separate files).


QUESTION 5:

During one run of the program, how many arrays are created?