more20 java challenge

Hi,
I am trying below challenge


http://codingbat.com/prob/p118290

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

Open in new window


I am getting below result

Expected      Run            
more20(20) → false      false      OK         
more20(21) → true      true      OK         
more20(22) → true      true      OK         
more20(23) → false      false      OK         
more20(25) → false      false      OK         
more20(30) → false      false      OK         
more20(31) → false      false      OK         
more20(59) → false      false      OK         
more20(60) → false      false      OK         
more20(61) → true      true      OK         
more20(62) → true      true      OK         
more20(1020) → false      false      OK         
more20(1021) → true      true      OK         
more20(1000) → false      false      OK         
more20(1001) → true      true      OK         
more20(50) → false      false      OK         
more20(55) → false      false      OK         
more20(40) → false      false      OK         
more20(41) → true      true      OK         
more20(39) → false      false      OK         
more20(42) → true      true      OK         
other tests
OK         


how to fix that and improve my approach and design of this challenge. Not sure on how to use mod method
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.

krakatoaCommented:
If the result is correct what more are you expecting?
0
gudii9Author Commented:
any other better alternate approaches please ? which expands my thinking and points out to my shortcomings
0
ozoCommented:
   if((n%20==1||n%20==2)){
   return true;
   }
  return false;
is equivalent to
  return n%20==1||n%20==2;

for a non-negative number, this can be transformed to
  return (n+19)%20<2;
0
Ultimate Tool Kit for Technology Solution Provider

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

krakatoaCommented:
public boolean more20(int n) {
  while (n-20>=20){n=n-20;}
  if(n-20>0&&n-20<3){return true;}else{return false;}
}

Open in new window

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:
how to use Match.mod() here


public boolean more20(int n) {
 // if((n%20==1||n%20==2)){
 
 if(n.mod(20)==1||n.mod(20)==2)
  return true;
  }
  return false;
}

Open in new window


below did not work. Of course negative scenario is kind of redundant here but just want to learn approach?
0
krakatoaCommented:
public boolean more20(int n) {
 // if((n%20==1||n%20==2)){
 
 if(n%20==1||n%20==2){
  return true;
  }
  return false;
}

Open in new window

0
gudii9Author Commented:
above passed all tests

 if(n.mod(20)==1||n.mod(20)==2)

can only be return with % as below
 if(n%20==1||n%20==2){

there is no such method as mod() in string or other wrapper classes??somehow my mind says there is method like mod() which i read or thinking mistakenly that i read?
please advise
0
krakatoaCommented:
Yeah, I think you were mistaken, except that BigInteger has a mod method.   So unless you are using that class then just use "%".


And read this link too, it has another interesting extra mod operator effect. Mod operator
0
ozoCommented:
1 and 2 are both non-negative numbers 1 or 2 more than a multiple of 20, but neither would pass http:#a41241765
0
gudii9Author Commented:
1 and 2 are both non-negative numbers 1 or 2 more than a multiple of 20, but neither would pass http:#a41241765

i am not clear on this comment?
what you mean by this comment. please advise.

are you saying

public boolean more20(int n) {
 // if((n%20==1||n%20==2)){
 
 if(n%20==1||n%20==2){
  return true;
  }
  return false;
}

above wont work all the time for non negative?
0
ozoCommented:
n%20==1||n%20==2
will work all the time for non negative

  while (n-20>=20){n=n-20;}
  if(n-20>0&&n-20<3){return true;}else{return false;}
won't work all the time
In particular, it will not work when n==1 or n==2

it could also be slow for n==2000000002
0
krakatoaCommented:
but neither would pass http:#a41241765

that's ok, because they wouldn't need to - 1 and 2 are both less than 20. :0)
0
krakatoaCommented:
it could also be slow for n==2000000002

I tried that number and the result was returned almost before the Enter key had returned to the up position.
0
ozoCommented:
1 and 2 are both less than 20
But they are "1 or 2 more than a multiple of 20"
0
krakatoaCommented:
No they are not. 0 (zero) is not a "multiple of twenty".
0
ozoCommented:
0 is most certainly a multiple of 20
0
ozoCommented:
returned almost before the Enter key had returned to the up position.
That may be fine if you always call all your functions from the enter key. It may not be fine if you call your functions from the middle of a tight loop.
0
krakatoaCommented:
You mean it's the artificial construct : 20^0 == 1 ?

That's just a mathematical nicety for convenience, and  is not applicable in this context.
0
krakatoaCommented:
That may be fine if you always call all your functions from the enter key. It may not be fine if you call your functions from the middle of a tight loop.

A "tight loop" is not part of the problem space here in any case. And even if it were, you'd have to show us what sort of impact it has.
0
ozoCommented:
In java 20^0 == 20
I don't see the relevance of that to the fact that 20*0 == 0
or that 0%20==0
0
ozoCommented:
not part of the problem space here
Indeed, for "the problem space here" the following code could have passed
public boolean more20(int n) {
if( n==20 ){ return  false; }
if( n==21 ){ return true; }
if( n==22 ){ return true; }
if( n==23 ){ return false; }
if( n==25 ){ return false; }
if( n==30 ){ return false; }
if( n==31 ){ return false; }
if( n==59 ){ return false; }
if( n==60 ){ return false; }
if( n==61 ){ return true; }
if( n==62 ){ return true; }
if( n==1020 ){ return false; }
if( n==1021 ){ return true; }
if( n==1000 ){ return false; }
if( n==1001 ){ return true; }
if( n==50 ){ return false; }
if( n==55 ){ return false; }
if( n==40 ){ return false; }
if( n==41 ){ return true; }
if( n==39 ){ return false; }
if( n==42 ){ return true; }
return n%80==1;
}
That doesn't make it a way I would recommend to implement "Return true if the given non-negative number is 1 or 2 more than a multiple of 20."
0
krakatoaCommented:
In java 20^0 == 20

 . . . really . . . ??
0
ozoCommented:
public static void main(String[] args) {
      System.out.println(20^0);
}
But that is not part of the problem space here
0
krakatoaCommented:
System.out.println(20^0);

Lol . . . in Java, "^" is the bitwise exclusive OR operator, *not* the power exponent !
0
ozoCommented:
Neither the bitwise exclusive OR operator nor the power exponent seem particularly applicable in this context.
although
return n%20==1^n%20==2;
would have been a valid answer
0
krakatoaCommented:
You ARE joking?
0
ozoCommented:
joking?
I may be pointing out ironies but all my statements have been factual.
0
krakatoaCommented:
I may be pointing out ironies but all my statements have been factual.



System.out.println("Power of 20 raised to 0th = "+(int)Math.pow(new Double(20),new Double(0)));
  System.out.println("Bitwise xor on 20 xor'd with 0 = "+(20^0));

Open in new window

0
ozoCommented:
As stated.
0
krakatoaCommented:
No idea what you mean by that, so I'll ignore it.
0
krakatoaCommented:
return n%20==1^n%20==2;

this is only because the cases can't both be true at the same time:

System.out.println(true^true);
  System.out.println(true^false);
  System.out.println(false^false);

Open in new window

0
gudii9Author Commented:
public class TestCap {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println(true^true);
              System.out.println(true^false);
              System.out.println(false^false);
              System.out.println("Power of 20 raised to 0th = "+(int)Math.pow(new Double(20),new Double(0)));
              System.out.println("Bitwise xor on 20 xor'd with 0 = "+(20^0));
      }

}


gave below output


false
true
false
Power of 20 raised to 0th = 1
Bitwise xor on 20 xor'd with 0 = 20


i did not understood below output
Bitwise xor on 20 xor'd with 0 = 20


what is this bitwise xor thing work and gave 20 xor'd with 0 gave 20
0
krakatoaCommented:
Power of 20 raised to 0th = 1
Bitwise xor on 20 xor'd with 0 = 20

This is the correct output from Java, yes.

XOR is 'exclusive OR', meaning that only one of the input bits has to be "1". So obviously if you XOR 20 with 0 - bitwise - you get 20 again, because all the bits in a binary zero are "0" s.
0
gudii9Author Commented:
i see your point

http://www.tutorialspoint.com/java/java_bitwise_operators_examples.htm



int a = 60;      /* 60 = 0011 1100 */  
     int b = 13;      /* 13 = 0000 1101 */

 c = a ^ b;       /* 49 = 0011 0001 */
     System.out.println("a ^ b = " + c );
0
krakatoaCommented:
Operators are different for a reason - you shouldn't think of them as being interchangeable.  XOR operator "^" is not the same as the logical operator "||".
0
krakatoaCommented:
System.out.print("true^true is ");System.out.println(true^true);
  System.out.print("true^false is ");System.out.println(true^false);
  System.out.print("false^false is ");System.out.println(false^false);
  
  System.out.print("true||true is ");System.out.println(true||true);
  System.out.print("true||false is ");System.out.println(true||false);
  System.out.print("false||false is ");System.out.println(false||false);
  
  System.out.print("false||true is ");System.out.println(false||true);

Open in new window

0
ozoCommented:
Better to compare ^ with |
|| also does shortcutting and does not do bitwise
0
krakatoaCommented:
Better to compare ^ with |

Now . . . you REALLY are joking.
0
krakatoaCommented:
|| also does shortcutting and does not do bitwise

I have said (several times) that || is not the same as bitwise "^".
0
gudii9Author Commented:
public class TestCap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(true^true);
		  System.out.println(true^false);
		  System.out.println(false^false);
		  System.out.println("Power of 20 raised to 0th = "+(int)Math.pow(new Double(20),new Double(0)));
		  System.out.println("Bitwise xor on 20 xor'd with 0 = "+(20^0));
		  System.out.print("true^true is ");System.out.println(true^true);
		  System.out.print("true^false is ");System.out.println(true^false);
		  System.out.print("false^false is ");System.out.println(false^false);
		  
		  System.out.print("true||true is ");System.out.println(true||true);
		  System.out.print("true||false is ");System.out.println(true||false);
		  System.out.print("false||false is ");System.out.println(false||false);
		  
		  System.out.print("false||true is ");System.out.println(false||true);
	}

}

Open in new window



above gave below output

false
true
false
Power of 20 raised to 0th = 1
Bitwise xor on 20 xor'd with 0 = 20
true^true is false
true^false is true
false^false is false
true||true is true
true||false is true
false||false is false
false||true is true
0
gudii9Author Commented:
true^true is false where as true||true is true. (it is different here)
true^false is true
false^false is false
above mathcin with below though
true||false is true
false||false is false

which is telling ^ is not same as ||
0
ozoCommented:
| does not do shortcutting, and does do bitwise, making it comparable to ^
unlike ||
Contrasting | with ^ makes sense.
Contrasting | with || can make sense.
But neither contrast was made.
One might as well try to compare || with Math.pow
0
gudii9Author Commented:
what is pow function does?

public class TestCap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(true^true);
		  System.out.println(true^false);
		  System.out.println(false^false);
		  System.out.println("Power of 20 raised to 0th = "+(int)Math.pow(new Double(20),new Double(0)));
		  System.out.println("Bitwise xor on 20 xor'd with 0 = "+(20^0));
		  System.out.print("true^true is-- ");System.out.println(true^true);
		  System.out.print("true^false is ");System.out.println(true^false);
		  System.out.print("false^false is-- ");System.out.println(false^false);
		  
		  System.out.print("true||true is -----");System.out.println(true||true);
		  System.out.print("true||false is ");System.out.println(true||false);
		  System.out.print("false||false is----- ");System.out.println(false||false);
		  
		  
		  System.out.print("true|true is--- ");System.out.println(true|true);
		  System.out.print("true|false is ");System.out.println(true|false);
		  System.out.print("false|false is--- ");System.out.println(false|false);
		  
		  System.out.print("false||true is ");System.out.println(false||true);
	}

}

Open in new window

above gave below output

false
true
false
Power of 20 raised to 0th = 1
Bitwise xor on 20 xor'd with 0 = 20
true^true is-- false
true^false is true
false^false is-- false
true||true is -----true
true||false is true
false||false is----- false
true|true is--- true
true|false is true
false|false is--- false
false||true is true

i see ^ and | are also differening as below

true^true is-- false
true|true is--- true
one is false other is true? not sure how it makes sense to contrast | with ^. may be i am missing something?
0
ozoCommented:
What pow function does is not relevant in this context.

"one is false other is true" is a contrast.
0
gudii9Author Commented:
i thought contrast means compare but looks like it means difference? please correct me if i am wrong
0
ozoCommented:
English can be even less precise than Java
compare
1 :  to point out as similar :  liken <She compared the activity of ants to the behavior of humans.>
2 :  to examine for similarity or differences <Before buying compare the two bicycles.>
3 :  to appear in relation to others <She compares well with the rest of the class.>
4 :  to state the positive, comparative, and superlative forms of an adjective or adverb
http://www.merriam-webster.com/dictionary/compare
0
gudii9Author Commented:
XOR  is used (^) when only one of the two boolean conditions is needed in the requirement but not both?
0
krakatoaCommented:
Yes. It must NOT be both.
0
krakatoaCommented:
| does not do shortcutting, and does do bitwise, making it comparable to ^
unlike ||

this is misleading stuff.

"It does not do shortcutting" sounds like it is somehow related to other operators which do. "and does do bitwise" - "making it comparable to" "^". Lol . . . . . these operators have nothing in common, and should not be brought into a discussion that ranks them against each other. They are completely separate, and should be understood on their own terms, and *not* compared like pairs of pants in the underwear department, or cuts of beef at the butcher.
0
ozoCommented:
| has a lot more in common with ^ than || does.
It is misleading to emphasize only the differences between | and ^ while seeming to be talking about || and ^
It is more appropriate to compare underwear to underwear and beef to beef.
To expound on underwear differences when talking about beef confuses and confounds the issue.
0
krakatoaCommented:
Sorry - you have confounded the issue. You seem set on taking the conversation toward comparisons, when comparisons are entirely inappropriate.
0
gudii9Author Commented:
i think i got more than what i needed to know on this challenge. I will open new question if i have any other specific questions on any other specifc area. Thank you both for valuable suggestions on this.
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.

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.