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
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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

Open in new window


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

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
CEHJCommented:
return (a == 6) || (b == 6) || (Math.abs(a-b) == 6) || (a+b == 6);

Open in new window


Your more verbose version doesn't really add readability, so why not the above?
Jim CakalicSenior 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)

Open in new window


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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

krakatoaCommented:
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;

Open in new window

krakatoaCommented:
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;

Open in new window

gudii9Author Commented:
ASCII flowchart

i like this. Where i can find more information on it. I never used it. Please advise
Jim CakalicSenior 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.
gudii9Author 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?
gurpsbassiCommented:
Yes its cleaner and quicker to use logical OR.
gudii9Author 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;

above is not clear to me. please advise
gudii9Author 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
krakatoaCommented:
this is easier . . .

return (Math.abs(a-b)==6)||(a+b==6)?true:a==6||b==6?true:false;

Open in new window

ozoCommented:
return a==6 || b==6 || a+b==6 || Math.abs(a-b)==6;
CEHJCommented:
return a==6 || b==6 || a+b==6 || Math.abs(a-b)==6; 

Open in new window

I think you'll find i've already posted that ;)
ozoCommented:
i've already posted that ;)
Then why is anyone still posting anything else?
CEHJCommented:
Not sure ;)
krakatoaCommented:
I posted because the OP said he didnt follow my code.
krakatoaCommented:
Then why is anyone still posting anything else?

I might be wrong, but I think CEHJ was talking to *you* . ;)
gurpsbassiCommented:
:) A classic guddi9 puzzler
krakatoaCommented:
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;

Open in new window

gudii9Author Commented:
is it necessary to understand all solutions given by everyone or take the one which i understand easily?
Please advise
gudii9Author Commented:
what is limit of ternary inside other ternary inside other ternary...i can go till any number?
gudii9Author Commented:
I am some how not good fan of ternaries not sure why
gudii9Author 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
gudii9Author 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
krakatoaCommented:
is it necessary to understand all solutions given by everyone or take the one which i understand easily?
Please advise

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.
krakatoaCommented:
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.
CEHJCommented:
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
krakatoaCommented:
I class mine as tagliatelle. ;)
CEHJCommented:
I would aim for lasagne at the very worst
krakatoaCommented:
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.