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.

Hi,

I am trying below challenge

I tried as below

I am getting below result

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

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;
}
```

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

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

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.

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;

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

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```
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;
}
```

below did not work. Of course negative scenario is kind of redundant here but just want to learn approach?

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

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

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

And read this link too, it has another interesting extra mod operator effect. Mod operator

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?

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

but neither would pass http:#a41241765

that's ok, because they wouldn't need to - 1 and 2 are both less than 20. :0)

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.

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.

That's just a mathematical nicety for convenience, and is not applicable in this context.

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.

not part of the problem space hereIndeed, 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."

System.out.println(20^0);

}

But that is not part of the problem space here

System.out.println(20^0);

Lol . . . in Java, "^" is the bitwise exclusive OR operator, *not* the power exponent !

although

return n%20==1^n%20==2;

would have been a valid answer

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));
```

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);
```

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(true^tr

System.out.println(true^fa

System.out.println(false^f

System.out.println("Power of 20 raised to 0th = "+(int)Math.pow(new Double(20),new Double(0)));

System.out.println("Bitwis

}

}

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

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.

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 );

```
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);
```

|| also does shortcutting and does not do bitwise

I have said (several times) that || is not the same as bitwise "^".

```
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);
}
}
```

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

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

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

```
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);
}
}
```

above gave below outputfalse

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?

i thought contrast means compare but looks like it means difference? please correct me if i am wrong

comparehttp://www.merriam-webster.com/dictionary/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

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

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.

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.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.