# love6 challenge java

Hi,

I am working one below challenge

http://codingbat.com/prob/p137742

public boolean love6(int a, int b) {

if((a==6||b==6)|(a+b==6)|(a-b==6))
{
return true;
}
else{
return false;
}
}
i am failing one test case

Expected      Run
love6(6, 4) → true      true      OK
love6(4, 5) → false      false      OK
love6(1, 5) → true      true      OK
love6(1, 6) → true      true      OK
love6(1, 8) → false      false      OK
love6(1, 7) → true      false      X
love6(7, 5) → false      false      OK
love6(8, 2) → true      true      OK
love6(6, 6) → true      true      OK
love6(-6, 2) → false      false      OK
love6(-4, -10) → true      true      OK
love6(-7, 1) → false      false      OK
love6(7, -1) → true      true      OK
love6(-6, 12) → true      true      OK
love6(-2, -4) → false      false      OK
love6(7, 1) → true      true      OK
love6(0, 9) → false      false      OK
love6(8, 3) → false      false      OK
love6(3, 3) → true      true      OK
love6(3, 4) → false      false      OK
other tests
OK

how do i design this problem with flow chart etc and how to improve my design and implementation. please advise
LVL 7
###### Who is Participating?

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

Commented:
I would probably rewrite your code but in order to fix what you have you have to do the following:

``````public boolean love6(int a, int b) {
if((a==6||b==6)||(a+b==6)||(Math.abs(a-b)==6))
{
return true;
}
else{
return false;
}
}
``````

The question gave you a clue about using Math.abs(..)

Secondly, instead of using bitwise-OR you need to use ||

Experts Exchange Solution brought to you by

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

Commented:
``````return (a == 6) || (b == 6) || (Math.abs(a-b) == 6) || (a+b == 6);
``````

Senior Developer/ArchitectCommented:
My code would be similar to CEHJ minus some parens. Here's my lame attempt at an ASCII flowchart
``````
< a = 6 > ----(T)---+
|               |
(F)              |
|               |
v               |
< b = 6 > ----(T)---+
|               |
(F)              |
|               |
v               |
< a+b = 6 > ---(T)--+
|               |
(F)              |
|               |
v               |
< abs(a-b)=6 > -(T)-+
|               |
v               v
(F)             (T)
``````

Each <..> is  decision with the condition inside. The path to the right is taken if the decision is true. The path down is taken if the decision is false. The terminals at the bottom are what is returned when control reaches that point (false or true).

Jim
Commented:
You can do this too, which Codingbat will appreciate for its simplicity :

``````return (Math.abs(Math.max(a,b)-Math.min(a,b))==6)||(a+b==6)?true:a==6||b==6?true:false;
``````
Commented:
Or mebbe this :

``````return (Math.abs(Math.max(a,b)-Math.min(a,b))==6)||(a+b==6)?true:a+b-a==6||a+b-b==6?true:false;
``````
Author Commented:
ASCII flowchart

Senior Developer/ArchitectCommented:
Honestly, I made it up. Apparently it wasn't orginal though because there is a free tool to do something similar at asciiflow.com.
Author Commented:
The question gave you a clue about using Math.abs(..)

i overlooked that point

Secondly, instead of using bitwise-OR you need to use ||

why is it preferred due to performance (where if it is false (or true) then does not bother to check next one whether true or false since result is anyway false?
Commented:
Yes its cleaner and quicker to use logical OR.
Author Commented:
(Math.abs(Math.max(a,b)-Math.min(a,b))==6)||(a+b==6)?true:a+b-a==6||a+b-b==6?true:false;

Author Commented:
(Math.max(a,b)-Math.min(a,b))==6)-->checking max and min value?

||(a+b==6) then or condition(ie not bit or??)

?true:a+b-a==6||a+b-b==6?true:false

too confusing to me
Commented:
this is easier . . .

``````return (Math.abs(a-b)==6)||(a+b==6)?true:a==6||b==6?true:false;
``````
Commented:
return a==6 || b==6 || a+b==6 || Math.abs(a-b)==6;
Commented:
``````return a==6 || b==6 || a+b==6 || Math.abs(a-b)==6;
``````
I think you'll find i've already posted that ;)
Commented:
Then why is anyone still posting anything else?
Commented:
Not sure ;)
Commented:
I posted because the OP said he didnt follow my code.
Commented:
Then why is anyone still posting anything else?

I might be wrong, but I think CEHJ was talking to *you* . ;)
Commented:
:) A classic guddi9 puzzler
Commented:
Yep - always bound to become more complicated than the original question. ;)

``````return a+b==6||a-b==6||b-a==6||a+b-a==6||a+b-b==6;
``````
Author Commented:
is it necessary to understand all solutions given by everyone or take the one which i understand easily?
Author Commented:
what is limit of ternary inside other ternary inside other ternary...i can go till any number?
Author Commented:
I am some how not good fan of ternaries not sure why
Author Commented:
my mind wont go further down if it has more than 2, 3 ternaries not sure how you guys picture, imagine and code multiple ternaries scenarios
Author Commented:
return a+b==6||a-b==6||b-a==6||a+b-a==6||a+b-b==6;

in above line what is meaning of last part as below?

a+b-a==6||a+b-b==6(a+b-a means b right and same way a+b-b results in a right???not sure what we are conveying to compiler or to jvm whichever

other first part is clear which is a+b==6||a-b==6||b-a==6
Commented:
is it necessary to understand all solutions given by everyone or take the one which i understand easily?

Eventually, yes; but at the outset . . . like perhaps now . . . it's best to understand one at a time, the if statement structure first - which all the guys here and in your others questions are masterful at, and then, after that, the ternary ops.
Commented:
OK so my last post was severely overcomplicated and you ought to ignore it really for now, and look at what gurps, ozo and CEHJ have recommended, as these are all succinct and therefore powerful as well as easy to understand answers.
Commented:
my mind wont go further down if it has more than 2, 3 ternaries not sure how you guys picture, imagine and code multiple ternaries scenarios

I certainly don't ;) That's just spaghetti
Commented:
I class mine as tagliatelle. ;)
Commented:
I would aim for lasagne at the very worst
Commented:
Ahhh . . . so you are then nesting quite deeply. ;)
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.