In: Computer Science
NOTE:
All the values are directly passed as parameters.
CODE WITH EXPLANATION :
public class Main//Main class declaration
{
public static void main(String[] args)//starting point of execution.
{
System.out.println("Demonstarting getMaxOf2Ints() :");
System.out.println("The maximum of 2 and 3 is : "+getMaxOf2Ints(2,3));//returns maximum of two values ie 2,3 by calling respective function.
System.out.println("Demonstarting getMinOf2Ints() :");
System.out.println("The minimum of 2 and 3 is : " + getMinOf2Ints(2,3));//returns minimum of two values ie 2,3 by calling respective function.
System.out.println("Demonstarting getMaxOf3Ints() :");
System.out.println("The maximum of 2 , 3 and 4 is : "+ getMaxOf3Ints(2,3,4));//returns maximum of three values ie 2,3 and 4 by calling respective function.
System.out.println("Demonstarting getMedianOf3Ints() :");
System.out.println("The median of 2 , 3 and 4 is : "+getMeadianOf3Ints(2,3,4));//returns median of three values ie 2,3 and 4 by calling respective function.
}
public static int getMaxOf2Ints(int a,int b)//function defintion
{
if(a>b)//if a is greater than b
return a;//returns a as a is maximun and exits function.
else //As we compare only two numbers if a is not maximun value then b is the maximun value
return b;//returns b as b is maximun and exits function.
}
public static int getMinOf2Ints(int a,int b)//function defintion
{
if(a>b)//if a is greater than b
return b;//returns b as b is minimun and exits function.
else //As we compare only two numbers if a is not maximun value then b is the maximun value
return a;//returns a as a is minimun and exits function.
}
public static int getMaxOf3Ints(int a,int b,int c)
{
if(a>b&&a>c)//if a is greater than b and a is greater than c.
return a;//returns a as a is maximun and exits function.
else if(a<b&&b>c)//if b is greater than a and b is greater than c.
return b;//returns b as b is maximun and exits function.
else//upo finally we need not have any confusion that c is the maximum value at the above 2 cases gets false.
return c;//returns c as c is maximun and exits function.
}
public static int getMinOf3Ints(int a,int b,int c)//function defintion
{
if(a<b&&a<c)//if a is lesser than b and a is lesser than c.
return a;//returns a as a is minimum and exits function.
else if(a>b&&b<c)//if b is lesser than a and b is lesser than c.
return b;//returns b as b is minimum and exits function.
else//upon finally we need not have any confusion that c is the minimum value at the above 2 cases gets false.
return c;//returns c as c is minimum and exits function.
}
public static int getMeadianOf3Ints(int a,int b,int c)//function defintion.
{
/*
How to calculate the median value.
Median is the middle value.
for the three numbers example 5,4,2
the median is calculated as follows
1)sort the values in ascending order => 2,4,5
2)Median is the middle value.
Therefore 4 is the median of 2,4,5
For 3 numbers it is observed that the median ie middle element will not be maximum and also minimum.
Hene median calculated using the above strategy.
*/
if(a!=getMinOf3Ints(a,b,c) && a!=getMaxOf3Ints(a,b,c))//If a is not maximum an also a is not minimum
return a;//return a as a will be on the middle.
else if(b!=getMinOf3Ints(a,b,c) && b!=getMaxOf3Ints(a,b,c))//If b is not maximum an also b is not minimum
return b;//return b as b will be on the middle.
else //upon finally we need not have any confusion that c is the middle value at the above 2 cases gets false.
return c;//returns c as c is median and exits function.
}
}
OUTPUT: