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

Answer:

Yes. Static methods can call static methods.


Max of Four

Say that you need the maximum of four integers. Call them A, B, C, and D. One way to do this is to

There is a static max()method of the Math class that computes the maximum of two integers. But let us ignore that.

Here is a program that implements this idea.


public class UnitTestMax
{
   
  public static int maxTwo( int X, int Y)
  {
    if ( X > Y )
      return X;
    else
      return Y;
  }

  public static int maxFour( int A, int B, int C, int D)
  {
    int maxAB = maxTwo( A, B );
    int maxCD = maxTwo( C, D );
    return maxTwo( maxAB, maxCD );
  }
  
  public static void main (String[] args ) 
  {
    int A= -8, B= 7, C= 21, D= 10;
    System.out.println("The max of " + A + ", " + B + ", " + C + ", " + D + " is: " + 
      maxFour( A, B, C, D ) );
  }
}

QUESTION 15:

Would the following work?

public static int maxFour( int A, int B, int C, int D)
  {
    return maxTwo( maxTwo( A, B ), maxTwo( C, D ) );
  }

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