Parameter Connected to New Data

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. New lines are inserted into the program to show this:

Parameter Connected to New Data

In the revised program, the findMax() method is used first with one array, and then with the other array. This is possible because the parameter x of the method refers to the current array, whichever one is used in the method call.

class ArrayOps
{
  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 ) 
  {
    ArrayOps operate = new ArrayOps();
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
    int[] ar2 =  { 2, 4, 1, 2, 6, 3, 6, 9 } ;

    System.out.println("The first  maximum is: " + operate.findMax( ar1 )  );
    
    System.out.println("The second maximum is: " + operate.findMax( ar2 )  );    
  }
}      

The program prints:

C:/>java ArrayDemo
The first maximum is: 27
The second maximum is: 9

QUESTION 7:

  1. Must each array contain the same number of elements?
  2. Must each array be an array of int?