Solved

largest of given three integers

Posted on 2014-10-06
20
110 Views
Last Modified: 2014-10-09
Hi,

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;
  
}

Open in new window


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
0
Comment
Question by:gudii9
  • 8
  • 4
  • 3
  • +3
20 Comments
 
LVL 4

Expert Comment

by:AnthonyHamon
ID: 40364169
The logic you need to apply is:

if a > b and a > c then return a
if b > c then return b else return c
0
 
LVL 7

Author Comment

by:gudii9
ID: 40364184
No need of covering below scenario?

if c> a then return c else return a
0
 
LVL 4

Expert Comment

by:AnthonyHamon
ID: 40364217
That scenario is covered.  Try it with your test data to validate.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 7

Author Comment

by:gudii9
ID: 40364357
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;
		  
		}*/

}

Open in new window


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;
		  
		}*/

}
}

Open in new window

0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 100 total points
ID: 40364370
You want &&, not & (although it will do the right thing in this case, it's not the same thing in all cases).

You should not return 0 there. If a>b && a>c was false and b > c was also false, you return 0. What should you return there instead?
0
 
LVL 4

Assisted Solution

by:AnthonyHamon
AnthonyHamon earned 100 total points
ID: 40364381
You have a 'return 0;' statement in your code which should be 'return c;'  The final 'if' block is remarked out - you can remove this completely if you wish.
0
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 40364549
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);
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40366569
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40366580
You can do it with nine applications of the Math.max function.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40366611
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
0
 
LVL 21

Assisted Solution

by:Amitkumar Panchal
Amitkumar Panchal earned 100 total points
ID: 40367661
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

try the below out:

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);
0
 
LVL 37

Accepted Solution

by:
zzynx earned 100 total points
ID: 40367729
>> you mean nine separate java apllications. Like nine different java main programs. Please advise
No, of course not!

What was meant was this:

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);
}

Open in new window


Which looks (and is) ridiculous, but works. ;-)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40368282
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);

	}
	
	
	

}

Open in new window


above code returned 345 which is correct
0
 
LVL 7

Author Comment

by:gudii9
ID: 40368287
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;
		

}
}

Open in new window


above also returned 8
0
 
LVL 84

Expert Comment

by:ozo
ID: 40368314
Those results are not unexpected.
Did you have reason to think it would do something different?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40368316
That scenario is covered.  Try it with your test data to validate.

Open in new window

i validated with test data and works fine. But i did not understand how that scenario is covered. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40368359
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40368391
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);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40370120
>> 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:

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.
}

Open in new window


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;
  }
}

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 40370384
I agree if you are referring to the code in http:#a40364357, but the question of http:#a40364184 was posted prior to that code, so wouldn't it have been referring to the code in http:#questionViewInlineCode10-28532206-1 ?
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 169
spring jars download 1 35
intellij error 2 17
Eclipse Java import and method not resolved 4 47
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question