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

Answer:

See below


Overloaded Method

Now each call of findMax() in main() uses the appropriate method.

class ArrayOps
{
  public static int findMax( int[] x )                
  {
    int max = x[0];
    for ( int index=0; index <x.length; index++ )
      if ( x[index] > max )
        max = x[index] ;
    return max ;
  }

  public static double   findMax( double[] x  )               
  {
    double   max = x[0];
    for ( int index=0; index <x.length; index++ )
      if ( x[index] > max )
        max = x[index] ;
    return max ;
  }
}

public class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    int[]    arI =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
    double[] arF =  { 2.1, -4.0, 13.2, 21.95, -6.3, 3.28, 6.0, 0.5 } ;

    System.out.println("The first  maximum is: " + ArrayOps.findMax( arI )  );
    
    System.out.println("The second maximum is: " + ArrayOps.findMax( arF )  );    
  }
}      

QUESTION 10:

(Review Question:) The findMax(int[] x) method uses a parameter that refers to an object. Can parameters also be primitive types, like int?


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