CMPS 134
Examples of Java methods to compute a maximum

The following are four versions of a method to calculate the maximum of two values of type int.1 From Java's point of view, the four are identical to one another. From the human reader's point of view, they differ in formatting details.

The formatting style used in the if statement in the first two (i.e., the two leftmost versions) are suitable when the bodies of the "then" branch and the "else" branch are single statements (or at least fit easily on one line). The two versions on the right are also suitable when the two branches of the if include multiple statements.

int maxOf2(int x, int y) {
   int result;
   if (x > y) { result = x; }
   else { result = y; }
   return result;
} 
int maxOf2(int x, int y) {
   int result;
   if (x > y)
      { result = x; }
   else
      { result = y; }
   return result;
} 
int maxOf2(int x, int y) {
   int result;
   if (x > y) {
      result = x;
   }
   else {
      result = y;
   }
   return result;
}
int maxOf2(int x, int y)
{
   int result;
   if (x > y)
   {
      result = x;
   }
   else
   {
      result = y;
   }
   return result;
}

Of course, if we replace each occurrence of int with, say, double in the method, we get a method to compute the maximum of two given values of type double. (The same can be said for any of the other primitive types of Java, which are byte, short, long, float, char, and boolean.)

To compute the maximum of three values of type int, we can make use of maxOf2:

int maxOf3(int x, int y, int z) {
   int result;
   if (x > y) {
      result = maxOf2(x,z);
   }
   else {  // x <= y
      result = maxOf2(y,z);
   }
   return result;
}
int maxOf3(int x, int y, int z) {
   return maxOf2(maxOf2(x,y), z);
}

The solution on the right is clearly superior to that on the left.

Inferior solutions are shown below (in three versions differing only in formatting style), simply for the purpose of illustrating that each of the "then" and "else" branches of an if statement may include "complex" code, including other if statements.

int maxOf3(int x, int y, int z) {
   int result;
   if (x > y) {
      if (x > z)
         { result = x; }
      else  // x>y but x<=z
         { result = z; }
   }
   else {  // x<=y
      if (y > z)
         { result = y; }
      else  // x<=y and y<=z
         { result = z; }
   }
   return result;
}
int maxOf3(int x, int y, int z) {
   int result;
   if (x > y) {
      if (x > z) {
         result = x;
      }
      else {  // x>y but x<=z
         result = z;
      }
   }
   else {  // x<=y
      if (y > z) {
         result = y;
      }
      else {  // x<=y and y<=z
         result = z;
      }
   }
   return result;
}
int maxOf3(int x, int y, int z)
{
   int result;
   if (x > y) 
   {
      if (x > z)
      {
         result = x;
      }
      else  // x>y but x<=z
      {
         result = z;
      }
   }
   else  // x<=y
   {  
      if (y > z)
      {
         result = y;
      }
      else  // x<=y and y<=z
      {
         result = z;
      }
   }
   return result;
}

For the purpose of illustrating the use of Java's Conditional-AND operator (written &&), as well as an if statement that has "more than two" branches, here are two versions (differing only in formatting style) of yet another way to write maxOf3:

int maxOf3(int x, int y, int z) {
   int result;
   if (x >= y && x >= z) {
      result = x;
   }
   else {  // either y or z is the max
      if (y >= z) {
         result = y;
      }
      else {  // neither x nor y is larger than z
         result = z;
      }
   }
   return result;
} 
int maxOf3(int x, int y, int z) {
   int result;
   if (x >= y && x >= z) {
      result = x;
   }
   else if (y >= z) {
      result = y;
   }
   else {  // neither x nor y is larger than z
      result = z;
   }
   return result;
} 

Footnote

[1] Because the Math.max() method is in Java's standard library, in real life there would be no reason to develop such a method.