Complete Program

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:

When the method is called, ar1 is a reference to the array object.

Complete Program

The method findMax() gets a reference to the array, so it can access the elements of that array object. Here is a complete program that contains both classes.

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 );
  }

}      

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.

QUESTION 5:

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