less20 java challenge

Hi,
I am trying below challenge


http://codingbat.com/prob/p133158      

I tried as  below
public boolean less20(int n) {
  if(n%20==1||n%20==2){
  
  return true;
  }
  
  else
  return false;
}

Open in new window


I am getting below result



Expected      Run            
less20(18) → true      false      X         
less20(19) → true      false      X         
less20(20) → false      false      OK         
less20(8) → false      false      OK         
less20(17) → false      false      OK         
less20(23) → false      false      OK         
less20(25) → false      false      OK         
less20(30) → false      false      OK         
less20(31) → false      false      OK         
less20(58) → true      false      X         
less20(59) → true      false      X         
less20(60) → false      false      OK         
less20(61) → false      true      X         
less20(62) → false      true      X         
less20(1017) → false      false      OK         
less20(1018) → true      false      X         
less20(1019) → true      false      X         
less20(1020) → false      false      OK         
less20(1021) → false      true      X         
less20(1022) → false      true      X         
less20(1023) → false      false      OK         
less20(37) → false      false      OK         
other tests
X         
            
          



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?

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

Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
You are checking for n%20 being 1 or 2, but you want them to be -1or-2 .
Tom BeckCommented:
Here's one way to solve it:
public boolean less20(int n) {
  if((n+20)%20>=18){
  
  return true;
  }
  
  else
  return false;
}

Open in new window

ozoCommented:
return n%20>=18;

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Tom BeckCommented:
n%20>=18 works!
gudii9Author Commented:
How to use mod method approach
ozoCommented:
return n%20==20-1 || n%20==20-2;  // return n%20>=18;
gudii9Author Commented:
i though mod means which covers either negative or positive but looks like it is reminder
ozoCommented:
% in Java only works as a modulo operator for non-negative numbers,
but in this challenge, you are given a non-negative number, so it's ok to use it here.
gudii9Author Commented:
how to use Match.mod() here?

please advise
ozoCommented:
The codingbat.com site does not seem to support the Java8 function Math.floorMod(n,20)
but you can emulate it with (n%20+20)%20
which is unnecessary in this case since you know that you are given a non-negative number
gudii9Author Commented:
public boolean less20(int n) {
  if(n%20==19||n%20==18){
  
  return true;
  }
  
  else
  return false;
}

Open in new window


above also passed all tests similar to other solution
gudii9Author Commented:
You are checking for n%20 being 1 or 2, but you want them to be -1or-2 .

how to check -1 and -2 for this challenge?
please advise
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
Change your line  if(n%20==1||n%20==2){

to  if(n%20==-1||n%20==-2){
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
No that's wrong sorry. Font is too tiny on my phone and I can't find a way to delete my prev comment.
gudii9Author Commented:
yes i tried as above but wont work
Thibault St john Cholmondeley-ffeatherstonehaugh the 2ndCommented:
Your comment ID: 41371808 looks like the correct solution.
A number mod 20 will return any value less than 20. The remainder after dividing by 20.
So your test for 18 OR 19 will be true if the number is 1 OR 2 less than a multiple of twenty.
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.