nearTen java challenge

Hi,
I am trying below challenge

http://codingbat.com/prob/p193613      

I tried as  below
public boolean nearTen(int num) {

  if(num%10==1||num%10==2){
  
  return true;
  }
  
  else
  return false;

  
}

Open in new window


I am getting below result



Expected      Run            
nearTen(12) → true      true      OK         
nearTen(17) → false      false      OK         
nearTen(19) → true      false      X         
nearTen(31) → true      true      OK         
nearTen(6) → false      false      OK         
nearTen(10) → true      false      X         
nearTen(11) → true      true      OK         
nearTen(21) → true      true      OK         
nearTen(22) → true      true      OK         
nearTen(23) → false      false      OK         
nearTen(54) → false      false      OK         
nearTen(155) → false      false      OK         
nearTen(158) → true      false      X         
nearTen(3) → false      false      OK         
nearTen(1) → true      true      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 some other relevant 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.

gudii9Author Commented:
public boolean nearTen(int num) {
int a=num%10;
int b=a-1;
int c=a-2;


  if(a==0||b==0||c==0){
  
  return true;
  }
  
  else
  return false;

  
}

Open in new window


Expected      Run            
nearTen(12) → true      true      OK         
nearTen(17) → false      false      OK         
nearTen(19) → true      false      X         
nearTen(31) → true      true      OK         
nearTen(6) → false      false      OK         
nearTen(10) → true      true      OK         
nearTen(11) → true      true      OK         
nearTen(21) → true      true      OK         
nearTen(22) → true      true      OK         
nearTen(23) → false      false      OK         
nearTen(54) → false      false      OK         
nearTen(155) → false      false      OK         
nearTen(158) → true      false      X         
nearTen(3) → false      false      OK         
nearTen(1) → true      true      OK         
other tests
OK         

i made modifcations as above still failing the negative number cases. How to use mod method. please advise
0
ozoCommented:
return (num+2)%10<=4;
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
gudii9Author Commented:
why my original code failed

public boolean nearTen(int num) {
int a=num%10;
int b=a-1;
int c=a-2;


  if(a==0||b==0||c==0){
  
  return true;
  }
  
  else
  return false;

  
}

Open in new window


how to use Math,Mod() here as well?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

gudii9Author Commented:
public boolean nearTen(int num) {


   Integer intObj = new Integer(num);
  if(intObj.mod(10)==1||intObj.mod(10)==2){
  
  return true;
  }
  
  else
  return false;

  
}

Open in new window


i tried as above but getting error as below
Compile problems:


Error:      if(intObj.mod(10)==1||intObj.mod(10)==2){
                ^^^
The method mod(int) is undefined for the type Integer


see Example Code to help with compile problems
0
ozoCommented:
code in http:#a41348244 checks for numbers above a multiple of 10, but not for numbers below a multiple of 10
0
gudii9Author Commented:
public boolean nearTen(int num) {

return num%10==1||num%10==2||num%10==8||num%10==9||num%10==0;

  
}

Open in new window

i passed all tests as above.
0
gudii9Author Commented:
i see this is more refactored and optimized code

return (num+2)%10<=4;
0
gudii9Author Commented:
95% of challenge is understanding the challenge and making a diagram and identifying the best approach and 5 % is just writing code i feel? am i right?
0
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.