?
Solved

largest number of the range

Posted on 2014-10-09
12
Medium Priority
?
127 Views
Last Modified: 2014-10-09
Hi,

I am trying below challenge
http://codingbat.com/prob/p177372

I wrote code as below

public int max1020(int a, int b) {

If(a>=10 && a<=20 && b>=10 && b<=20)
{
if(Math.abs(10-a)>Math.abs(10-b))
{
return b;
}
return a;
}
return 0;
  
}

Open in new window



I am getting error as below

Compile problems:


Error:      If(a>=10 && a<=20 && b>=10 && b<=20)
                                         ^
Syntax error, insert ";" to complete BlockStatements




How to fix this error and improve above code. I really do not want return 0. I want to remove that line
But i cannot remove that line as the method is expecting return just before method closing braket(parenthesis)

Please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40371171
public int max1020(int a, int b) {


if(Math.abs(10-a)>Math.abs(10-b))
{
return b;
}
return a;
}

Open in new window


when i write like above failing test cases as i am not checking if numbers in range of 10,20

Expected      Run            
max1020(11, 19) → 19      11      X         
max1020(19, 11) → 19      11      X         
max1020(11, 9) → 11      11      OK         
max1020(9, 21) → 0      9      X         
max1020(10, 21) → 10      10      OK         
max1020(21, 10) → 10      10      OK         
max1020(9, 11) → 11      9      X         
max1020(23, 10) → 10      10      OK         
max1020(20, 10) → 20      10      X         
max1020(7, 20) → 20      7      X         
max1020(17, 16) → 17      16      X
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371174
when i put the condition check for 10,20 range i am getting errors as posted in my original question. please advise
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 40371215
You misspelled 'if' as 'If'

change the 'I' to 'i' and java should be able to compile it.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 7

Author Comment

by:gudii9
ID: 40371256
Good catch. I wonder why i could not noticed it.

public int max1020(int a, int b) {

if(a>=10 && a<=20 && b>=10 && b<=20)
{
if(Math.abs(10-a)>Math.abs(10-b))
{
return b;
}
return a;
}
return 0;
  
}

Open in new window


i still have failed test cases which i need to fix

Expected      Run            
max1020(11, 19) → 19      11      X         
max1020(19, 11) → 19      11      X         
max1020(11, 9) → 11      0      X         
max1020(9, 21) → 0      0      OK         
max1020(10, 21) → 10      0      X         
max1020(21, 10) → 10      0      X         
max1020(9, 11) → 11      0      X         
max1020(23, 10) → 10      0      X         
max1020(20, 10) → 20      10      X         
max1020(7, 20) → 20      0      X         
max1020(17, 16) → 17      16      X
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371260
public int max1020(int a, int b) {

if(a>=10 && a<=20 && b>=10 && b<=20)
{
if(a>b)
{
return a;
}
return b;
}
return 0;
  
}

Open in new window


modified as above still failing below

Expected      Run            
max1020(11, 19) → 19      19      OK         
max1020(19, 11) → 19      19      OK         
max1020(11, 9) → 11      0      X         
max1020(9, 21) → 0      0      OK         
max1020(10, 21) → 10      0      X         
max1020(21, 10) → 10      0      X         
max1020(9, 11) → 11      0      X         
max1020(23, 10) → 10      0      X         
max1020(20, 10) → 20      20      OK         
max1020(7, 20) → 20      0      X         
max1020(17, 16) → 17      17      OK         
Your progress graph for this problem
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371265
max1020(11, 9) → 11      0      X

how this is 11. I thought it supposed to return 0 as per challenge.

Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371268
You want to return 0 only when neither is in that range.
You are returning 0 unless both are in that range.

Also, you want the largest number, not the number that is closest to 10
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371294
You corrected the part about returning the largest number instead of the number closest to 10
but are you understanding what " if neither is in that range. " means?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371321
" if neither is in that range. "

yes

like 5,14 for  a and b
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371328
Is a in that range?
Is b in that range?
Are neither in that range?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40371352
public int max1020(int a, int b) {

if((a>=10 && a<=20)&&(b>=10 && b<=20))
{
if(a>b)
{
return a;
}
return b;
}
else if(a>=10 && a<=20)
return a;

else if(b>=10 && b<=20)
return b;

else

return 0;

  
}

Open in new window


this seems all passing
0
 
LVL 84

Expert Comment

by:ozo
ID: 40371378
I agree
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 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