Find Maximum 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

Answer:

By scanning through the elements of the array, updating a provisional maximum until the last element is reached.

Find Maximum Method

This is the same algorithm that was used in the previous chapter. Now it will be implemented as a method. Here is a partial definition of the ArrayOps class.

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 = x[index] ;

    return max ;
  }
}

The ArrayOps class contains a method findMax() that finds the maximum of an array.

The parameter list is of the method is: int[] x

  • This declares a formal parameter x which will be a reference to an array object.
  • The caller is expected to supply a reference to an array as an actual parameter.
  • The method is written using the parameter x to stand for the actual array it will work with.

The parameter x means "whatever data is supplied when the method starts to run." This might be different data at different times.

QUESTION 3:

Fill in the blank so that the method is complete.