gudii9
asked on
largest of given three integers
Hi,
I am working on below challenge
http://codingbat.com/prob/p101887
I wrote as below
I have two test cases failing. Please advise on how to fix and effective way to write this code.
Expected Run
intMax(1, 2, 3) → 3 3 OK
intMax(1, 3, 2) → 3 3 OK
intMax(3, 2, 1) → 3 3 OK
intMax(9, 3, 3) → 9 9 OK
intMax(3, 9, 3) → 9 9 OK
intMax(3, 3, 9) → 9 9 OK
intMax(8, 2, 3) → 8 8 OK
intMax(-3, -1, -2) → -1 -1 OK
intMax(6, 2, 5) → 6 6 OK
intMax(5, 6, 2) → 6 5 X
intMax(5, 2, 6) → 6 5 X
I am working on below challenge
http://codingbat.com/prob/p101887
I wrote as below
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;
}
I have two test cases failing. Please advise on how to fix and effective way to write this code.
Expected Run
intMax(1, 2, 3) → 3 3 OK
intMax(1, 3, 2) → 3 3 OK
intMax(3, 2, 1) → 3 3 OK
intMax(9, 3, 3) → 9 9 OK
intMax(3, 9, 3) → 9 9 OK
intMax(3, 3, 9) → 9 9 OK
intMax(8, 2, 3) → 8 8 OK
intMax(-3, -1, -2) → -1 -1 OK
intMax(6, 2, 5) → 6 6 OK
intMax(5, 6, 2) → 6 5 X
intMax(5, 2, 6) → 6 5 X
ASKER
No need of covering below scenario?
if c> a then return c else return a
if c> a then return c else return a
That scenario is covered. Try it with your test data to validate.
ASKER
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;
}*/
}
I tried as above
still failing for few cases
Expected Run
intMax(1, 2, 3) → 3 0 X
intMax(1, 3, 2) → 3 3 OK
intMax(3, 2, 1) → 3 3 OK
intMax(9, 3, 3) → 9 9 OK
intMax(3, 9, 3) → 9 9 OK
intMax(3, 3, 9) → 9 0 X
intMax(8, 2, 3) → 8 8 OK
intMax(-3, -1, -2) → -1 -1 OK
intMax(6, 2, 5) → 6 6 OK
intMax(5, 6, 2) → 6 6 OK
intMax(5, 2, 6) → 6 0 X
Correct for more than half the tests
complete code is
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;
}*/
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Below is the simplest one with the help of Math.max().
public int intMax(int a, int b, int c) {
return Math.max(Math.max(a,b),c);
}
ASKER
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
You can do it with nine applications of the Math.max function.
ASKER
You can do it with nine applications of the Math.max function.
you mean nine separate java apllications. Like nine different java main programs. Please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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);
}
}
above code returned 345 which is correct
ASKER
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;
}
}
above also returned 8
Those results are not unexpected.
Did you have reason to think it would do something different?
Did you have reason to think it would do something different?
ASKER
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
ASKER
Those results are not unexpected.
Did you have reason to think it would do something different?
I agree. The results are correct and expected only.
if you are referring to the scenario c> a,
it was covered only when a<=b, otherwise your initial if(a>b)
would have immediately returned a
but instead of returning a, you could have returned Math.max(a,c);
it was covered only when a<=b, otherwise your initial if(a>b)
would have immediately returned a
but instead of returning a, you could have returned Math.max(a,c);
>> i did not understand how that scenario is covered. Please advise
Which scenario?
If you read your code - which I commented - you simply see that it covers all scenario's:
Maybe it's even more clear when you write it with some more else's like this:
Which scenario?
If you read your code - which I commented - you simply see that it covers all scenario's:
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.
}
Maybe it's even more clear when you write it with some more else's like this:
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;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if a > b and a > c then return a
if b > c then return b else return c