Write the definition of a method max that has three int parameters and returns the largest

We discussed the above method using

  1. Method max that has three int parameters and returns the largest using relational and logical operator.
  2. Method max that has three int parameters and returns the largest using nested if else.
  3. Method max that has three int parameters and returns the largest using Math.max().
  4. Method max that has three int parameters and returns the largest using ternary or conditional operator
  5. Static method max that has three int parameters and returns the largest using nested if else

Program: Write the definition of a method max that has three int parameters and returns the largest

We have created a method max that takes three integer inputs a,b and c, and find greater among three using relational and logical operators

Program: Method max that has three int parameters and returns the largest using nested if else

Here three numbers are compared one by one using nested if-else.

Method max that has three int parameters and returns the largest using Math.max()

Math. max() is used to find the greatest among two numbers. to find the greatest among the three numbers this function is used inside another Math. max().

As below Math.max(Math.max(a, b),c);

Method max that has three int parameters and returns the largest using ternary or conditional operator

The ternary operator takes two-parameter and returns the greatest among the two.

To use for three numbers ternary operator nested ternary operators are a good choice.

c>(a>b ? a:b)? c:(a>b ? a:b);

Static method max that has three int parameters and returns the largest using nested if else

Here we have created static method max to find the greatest among three numbers.

To call a static method class name can use as below

Maximum.max(9, 18, 19)

method max that has three int parameters and returns the largest
Fig: Method max that has three int parameters and returns the largest

Read More

Methods in Java-how to call a method in java