if a > b and a > c then return a
if b > c then return b else return c
public int intMax(int a, int b, int c) {
if(a>b)
{
int minValue=Math.min(a,b);
return a;
}
if(a>c)
{
int minValue=Math.min(a,c);
return a;
}
if(b>c)
{
int minValue=Math.min(b,c);
return b;
}
return c;
}
public int intMax(int a, int b, int c) {
if((a>b)&(a>c))
{
//int minValue=Math.min(a,b);
return a;
}
if(b>c)
{
//int minValue=Math.min(a,c);
return b;
}
return 0;
/*if(b>c)
{
int minValue=Math.min(b,c);
return b;
}
return c;
}*/
}
public class Test4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int largeValue=intMax(5,6,2);
System.out.println("largeValue is--->"+largeValue);
}
public static int intMax(int a, int b, int c) {
if((a>b)&(a>c))
{
//int minValue=Math.min(a,b);
return a;
}
if(b>c)
{
//int minValue=Math.min(a,c);
return b;
}
return 0;
/*if(b>c)
{
int minValue=Math.min(b,c);
return b;
}
return c;
}*/
}
}
public int intMax(int a, int b, int c) {
return Math.max(Math.max(a,b),c);
}
You can do it with nine applications of the Math.max function.
Let us say if i have 10 given int numbers (instead of 3 in this case) can i find the largest of the 10 using Math.max function. Please advise
public int intMax(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10) {
return Math.max(Math.max(Math.max(Math.max(Math.max(Math.max(Math.max(Math.max(Math.max(p1,p2),p3), p4),p5),p6),p7),p8),p9),p10);
}
public class Test6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] values = {12,45,23,67,45,86,43,67,98,5,45,345,67,89,34,56};
int maxval = values[0];
for (int ind=1; ind < values.length; ind++) {
maxval=Math.max(maxval, values[ind]);
}
System.out.println("Max Value : " + maxval);
}
}
public class Test7 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int largeValue=intMax(5,6,8);
System.out.println("largeValue is--->"+largeValue);
}
public static int intMax(int a, int b, int c) {
if((a>b)&&(a>c))
{
//int minValue=Math.min(a,b);
return a;
}
if(b>c)
{
//int minValue=Math.min(a,c);
return b;
}
return c;
}
}
That scenario is covered. Try it with your test data to validate.
i validated with test data and works fine. But i did not understand how that scenario is covered. Please advise
Those results are not unexpected.
Did you have reason to think it would do something different?
public static int intMax(int a, int b, int c) {
if ((a>b)&&(a>c)) { // a is bigger than b and bigger than c...
return a; // ...so return it
}
// If you're here, you already know that a is not the biggest one, so let's focus on b and c
if (b>c) { // b is bigger than c...
return b; // ... so return b
}
// if you're here, you know that a is not the biggest, b is not the biggest, so...
return c; // c must be the biggest. Well, return it.
}
public static int intMax(int a, int b, int c) {
if ((a>b)&&(a>c)) {
return a;
} else if (b>c) {
return b;
} else {
return c;
}
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
print map entry | 34 | 53 | |
ForLoop Example | 3 | 40 | |
web services creation SOAP vs REST | 5 | 17 | |
Advice in Xamarin | 21 | 44 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
10 Experts available now in Live!