The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Hi,

I am working on below challenge

http://codingbat.com/prob/p115384

I tried my code as below

I am getting below result

how to improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?

Please advise

I am working on below challenge

http://codingbat.com/prob/p115384

I tried my code as below

```
public int maxMod5(int a, int b) {
if(a>b){
return a;
}
else{
if( (a==b)){
return 0;
}
else if( (b%5==a%5)){
return 0;
}
}
return 1;
}
```

I am getting below result

Expected Run

maxMod5(2, 3) → 3 1 X

maxMod5(6, 2) → 6 6 OK

maxMod5(3, 2) → 3 3 OK

maxMod5(8, 12) → 12 1 X

maxMod5(7, 12) → 7 0 X

maxMod5(11, 6) → 6 11 X

maxMod5(2, 7) → 2 0 X

maxMod5(7, 7) → 0 0 OK

maxMod5(9, 1) → 9 9 OK

maxMod5(9, 14) → 9 0 X

maxMod5(1, 2) → 2 1 X

other tests

OK

how to improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?

Please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
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.

"Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0."

So deal with the clear instruction "However, in all cases, if the two values are the same, return 0." first. This gives you 'a==b?0;' Now you have two other conditions to assess. You have to deal with the mod (remainder) case first, because comparing the two ints just based on their size at this stage, is not good enough, as the two ints *could* be something like 15 and 500, both of which mod by 5, leaving 0 remainder in both cases. So you go 'a%5 == b%5 ?Math.min(a,b)' and you have the second part of your return statement. That just leaves the clean-up comparison, which is a simple size issue -> return the largest of a and b.

```
if(a==b){
return 0;
}
else if(a%5==b%5){
return Math.min(a,b);
}
return Math.max(a,b);
```

above worked perfect?

My question is which condition to put in top if block

and which condition to put in middle else if block

and which condition to put in bottom else block.

when i interachage like below if with else if condition it failed one

```
public int maxMod5(int a, int b) {
if(a%5==b%5){
return Math.min(a,b);
}
else if( (a==b)){
return 0;
}
else {
return Math.max(a,b);
}
}
```

please advise

```
public int maxMod5(int a, int b) {
if(a==b){return 0;}
if(a%5==b%5){return Math.min(a,b);}
else {return Math.max(a,b);
}
}
```

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 int maxMod5(int a, int b) {
if(a%5==b%5){
return Math.min(a,b);
}
else if( (a==b)){
return 0;
}
else {
return Math.max(a,b);
}
}
```

why above failed one test as below when i interchange if and else if conditions as above?

Expected Run

maxMod5(2, 3) → 3 3 OK

maxMod5(6, 2) → 6 6 OK

maxMod5(3, 2) → 3 3 OK

maxMod5(8, 12) → 12 12 OK

maxMod5(7, 12) → 7 7 OK

maxMod5(11, 6) → 6 6 OK

maxMod5(2, 7) → 2 2 OK

maxMod5(7, 7) → 0 7 X

maxMod5(9, 1) → 9 9 OK

maxMod5(9, 14) → 9 9 OK

maxMod5(1, 2) → 2 2 OK

other tests

X

Correct for more than half the tests

```
What you need to think about is when the ints are the same and you run the mod test first.
```

but we are doing reverse right as below

```
public int maxMod5(int a, int b) {
if(a==b){return 0;}
if(a%5==b%5){return Math.min(a,b);}
else {return Math.max(a,b);
}
}
```

as above we are instead doing as below

What you need to think about is when the ints are the same and you run the mod test second or next or later??

please advise.

Ask yourself what happens when a==10 and say b==20.

mods same as % gives 0 and 0 which are equal

first if skipped goes to second one i.e else if

as below

```
if(a==b){
return 0;
}
else if(a%5==b%5){
return Math.min(a,b);
}
```

However, in all cases,in the challenge i have to read above more clearly.

If it says however in all cases means it comes at top if loop right? (not in else if /else)

public int maxMod5(int a, int b) {if code as above i see why below is failing

if(a%5==b%5){

return Math.min(a,b);

}

else if( (a==b)){

return 0;

}

else {

return Math.max(a,b);

}

}

maxMod5(7, 7) → 0 7 X

as the % is 2 and other % also 2 so goes to first above if loop prints min of 7, 7 which is 7.

But challenge says there is a deviation by saying 'However in all cases'

which means this one is like overriding all other instructions and comes as high priority number one thing(not two, three) to take care.

please advise my understanding is correct?

understanding the wording is important.

which comes first which comes second which comes third and last and also i am sometimes confusing between || and && which makes up rest of 10% i feel

Is the Question.

Here is another way of writing that :

Given two int values, return whichever value is larger, BUT if the two values have the same remainder when divided by 5, then return the smaller value, EXCEPT in all cases where the two values are the same, in which case return 0.

So you need to look at the particular cases first, (edges) and then after that you can deal with the remaining cases, which cover everything else - and "everything else" in this question means : "the larger of the two ints". So cases that should return "0" are the most particular, so deal with them first, then the mod cases, and then, as I said above.

identifying the edges or edge case important.

So cases that should return "0" are the most particular, so deal with them first

how do i conclude some sentence(out of given 3 sentences) is most particular than other?

how do i conclude some sentence(out o . . . . .

"

When it rains, I don't go out at all, HOWEVER I do still go to the end of the street if it's a Wednesday, HOWEVER, if it's after 3 o'clock, then I just go into the garden.

"

HOWEVER, if it's after 3 o'clock

above is most particular...no matter rain or which day of week you go out i.e to garden.

Is my understanding is correct?

Is my understanding is correct?

Yes.

-------------------------

I'm not saying that was the best or even a very good example, but you should look at each "clause" in a problem, and see how it impacts the other clauses or terms in the statement, and build the logic based on that. As you touched on in earlier questions, perhaps sometimes a Venn diagram could help, but that would be your personal preference to use or not. I would personally prefer a flow chart approach, but then again, I don't know what the production experts here would say to that these days.

In short, just think things through, several times over, and usually deal with special cases before anything else.

Java EE

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

Open in new window