Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Hi,

I was working on the int in the range of 30 to 50 challenge

http://codingbat.com/prob/p132134

I wrote code like below

Expected Run

in3050(30, 31) → true true OK

in3050(30, 41) → false false OK

in3050(40, 50) → true true OK

in3050(40, 51) → false true X

in3050(39, 50) → false false OK

in3050(50, 39) → false false OK

in3050(40, 39) → true true OK

in3050(49, 48) → true true OK

in3050(50, 40) → true true OK

in3050(50, 51) → false true X

in3050(35, 36) → true true OK

in3050(35, 45) → false false OK

Correct for more than half the tests

How to fix and improve my program. Please advise

I was working on the int in the range of 30 to 50 challenge

http://codingbat.com/prob/p132134

I wrote code like below

```
public boolean in3050(int a, int b) {
if((Math.abs(a-30)>=10 &&Math.abs(b-30)>=10) || (Math.abs(a-40)>=10&& Math.abs(b-40)>=10))
{
return true;
}
if((Math.abs(a-40)>=10 && Math.abs(b-40)>=10) || (Math.abs(a-50)>=10 && Math.abs(b-50)>=10))
{
return true;
}
return false;
}
```

I have couple of test cases failing as belowExpected Run

in3050(30, 31) → true true OK

in3050(30, 41) → false false OK

in3050(40, 50) → true true OK

in3050(40, 51) → false true X

in3050(39, 50) → false false OK

in3050(50, 39) → false false OK

in3050(40, 39) → true true OK

in3050(49, 48) → true true OK

in3050(50, 40) → true true OK

in3050(50, 51) → false true X

in3050(35, 36) → true true OK

in3050(35, 45) → false false OK

Correct for more than half the tests

How to fix and improve my program. Please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
public boolean in3050(int a, int b) {
if((Math.abs(a-30)>=10 &&Math.abs(b-30)>=10) || (Math.abs(a-40)>=10&& Math.abs(b-40)>=10))
{
return true;
}
if((Math.abs(a-40)>=10 && Math.abs(b-40)>=10) || (Math.abs(a-50)>=10 && Math.abs(b-50)>=10))
{
return true;
}
if((a-50)>=1 && (b-50)>=1)
{
return false;
}
return false;
}
```

I think i improved my code bit still 2 failing

Expected Run

in3050(30, 31) → true true OK

in3050(30, 41) → false false OK

in3050(40, 50) → true true OK

in3050(40, 51) → false true X

in3050(39, 50) → false false OK

in3050(50, 39) → false false OK

in3050(40, 39) → true true OK

in3050(49, 48) → true true OK

in3050(50, 40) → true true OK

in3050(50, 51) → false true X

in3050(35, 36) → true true OK

in3050(35, 45) → false false OK

What does:

Math.abs(a-30)>=10

return for "a = 25"?

And is this what you want?

Doug

Everyone is slow when they start out.

Doug

```
public class Test9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
in3050(40,51);
}
public static boolean in3050(int a, int b) {
if((Math.abs(a-30)<=10 &&Math.abs(b-30)<=10) || (Math.abs(a-40)<=10&& Math.abs(b-40)<=10))
{
return true;
}
if((Math.abs(a-40)<=10 && Math.abs(b-40)<=10) || (Math.abs(a-50)<=10 && Math.abs(b-50)<=10))
{
return true;
}
if((a-50)>0 && (b-50)>0)
{
return false;
}
return false;
}
}
```

tried as above but still not working. WIll modify more

Think about what happens when a is 29, 30, 31, 39, 40, 41

Another hint: How would you test for in the range -10..10?

I think that's because you use the method Math.abs()

I wonder why you use it for this challenge?

Let's do it in English:

Checking if an int x is in the range 30...40 is just as simple as

1) checking that x is 30 or more

but ALSO

2) checking that x is no more than 40, thus 40 or less

In java

1) x >= 30

AND

2) x <= 40

Hence, checking if an int x is in the range 30...40 is just as simple as:

if ( x>=30 && x<=40) {

}

You have to check if both a and b are in the range 30..40, so that just doubling what you already had above:

if ( (a>=30 && a<=40) && (b>=30 && b<=40) ) {

}

And you have to check it for another range: 40...50, so that's replacing 30 by 40 and 40 by 50:

if ( (a>=40 && a<=50) && (b>=40 && b<=50) ) {

}

So the eventual result is:

```
public boolean in3050(int a, int b) {
if ( (a>=30 && a<=40) && (b>=30 && b<=40) ) { // a and b in the range 30...40?
return true; // Yes!
}
if ( (a>=40 && a<=50) && (b>=40 && b<=50) ) { // a and b in the range 40...50?
return true; // Yes!
}
return false; // no succes (otherwhise, we shouldn't have come till here)
}
```

```
public class Test8 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean b=in3050(30, 51);
System.out.println("b is-->"+b);
}
public static boolean in3050(int a, int b) {
if ( a>=30 && a<=40 && b>=30 && b<=40 ) { // a and b in the range 30...40?
return true; // Yes!
}
if ( a>=40 && a<=50 && b>=40 && b<=50 ) { // a and b in the range 40...50?
return true; // Yes!
}
return false; // no succes (otherwhise, we shouldn't have come till here)
}
}
```

Even if i remove the inner brackets I still get same results.

Instead of

if ( (a>=30 && a<=40) &&( b>=30 && b<=40) )

changed as below

if ( a>=30 && a<=40 && b>=30 && b<=40 )

So no need of inner extra brackets right?

Please advise

but I was asking whether you could figure out if

30-50 corresponds to Math.abs(a-40)<=10

then

30-40 corresponds to what?

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

You may also want to think about how you are doing the range 30..40 inclusive and range 40..50 inclusive tests