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

Answer:

Two methods are overloaded when they have the same name but different types or numbers of parameters. The compiler will inspect the parameters of a method call and pick the method that best matches the parameter list.


Overloaded Method

It would be useful to have a method that finds the maximum of an array of double. Here is a start on this 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   findMax(   )               
  {
      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 9:

Fill in the blanks so that the second method call matches the second findMax() method.


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