old35 java challenge

Hi,
I am trying below challenge


http://codingbat.com/prob/p159612

I tried as  below
public boolean old35(int n) {

if(n%3==0||n%5==0){
return true;
}
else
return false;
  
}

Open in new window


I am getting below result
Expected      Run            
old35(3) → true      true      OK         
old35(10) → true      true      OK         
old35(15) → false      true      X         
old35(5) → true      true      OK         
old35(9) → true      true      OK         
old35(8) → false      false      OK         
old35(7) → false      false      OK         
old35(6) → true      true      OK         
old35(17) → false      false      OK         
old35(18) → true      true      OK         
old35(29) → false      false      OK         
old35(20) → true      true      OK         
old35(21) → true      true      OK         
old35(22) → false      false      OK         
old35(45) → false      true      X         
old35(99) → true      true      OK         
other tests
OK         


Not sure how to eliminate if both case.
how to fix that and improve my approach and design of this challenge. How do i make a graphical venn or ascii diagram to design it before writing single line of code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?
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.

Tom BeckCommented:
To the challenge:
public boolean old35(int n) {

if(n%3==0||n%5==0){
  if(n%3==0&&n%5==0) {
    return false;
  }else{
    return true;
  }
}
else
return false;
  
}

Open in new window

To the "graphical venn" part of the question: No clue
0
ozoCommented:
return n%3==0 ^ n%5==0;
0
gudii9Author Commented:
public boolean old35(int n) {

if(n%3==0||n%5==0){
  if(n%3==0&&n%5==0) {
    return false;
  }else{
    return true;
  }
}
else
return false;
  
}

Open in new window


why we need inner if else inside outer if loop?
please advise.

i do not understand why my code failed in first place as below?
public boolean old35(int n) {

if(n%3==0||n%5==0){
return true;
}
else
return false;
  
}

Open in new window

0
ozoCommented:
Your code
if(n%3==0||n%5==0){
return true;
}
else
return false;

Open in new window

returns true when both n%3==0 and n%5==0 are true, contrary to the stated challenge
You could correct that with
  return (n%3==0||n%5==0) && !(n%3==0&&n%5==0);
or more simply with
  return n%3==0 ^ n%5==0;
0

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.