We discussed the above method using
- Method max that has three int parameters and returns the largest using relational and logical operator.
- Method max that has three int parameters and returns the largest using nested if else.
- Method max that has three int parameters and returns the largest using
Math.max()
. - Method max that has three int parameters and returns the largest using ternary or conditional operator
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Maximum { public int max(int a, int b, int c) { if (a > b && a > c) { return a; } else if (b > a && b > c) { return b; } else { return c; } } public static void main(String[] args) { Maximum m = new Maximum(); int maxVal = m.max(7, 8, 6); System.out.println("Maximum is " + maxVal); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Maximum { public int max(int a, int b, int c) { if (a > b) { if (a > c) { return a; } else { return c; } } else if (b> c) { return b; } else { return c; } } public static void main(String[] args) { Maximum m = new Maximum(); int maxVal = m.max(9, 18, 19); System.out.println("Maximum is " + maxVal); } } |
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);
1 2 3 4 5 6 7 8 9 10 11 12 | public class Maximum { public int max(int a, int b, int c) { return Math.max(Math.max(a, b),c); } public static void main(String[] args) { Maximum m = new Maximum(); int maxVal = m.max(19, 18, 1); System.out.println("Maximum is " + maxVal); } } |
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);
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Maximum { public int max(int a, int b, int c) { return c>(a>b ? a:b)? c:(a>b ? a:b); } public static void main(String[] args) { Maximum m = new Maximum(); int maxVal = m.max(111, 18, 110); System.out.println("Maximum is " + maxVal); } } |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Maximum { public static int max(int a, int b, int c) { if (a > b) { if (a > c) { return a; } else { return c; } } else if (b > c) { return b; } else { return c; } } public static void main(String[] args) { int maxVal = Maximum.max(9, 18, 19); System.out.println("Maximum is " + maxVal); } } |
Read More