maxMod5 challenge

Hi,

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

Open in new window


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
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:
 return a==b?0:(a%5)==(b%5)?Math.min(a,b):Math.max(a,b);

Open in new window

gudii9Author Commented:
can i write else if without condition similar to else?
krakatoaCommented:
Apart from Venn diagrams, I think you'll eventually need to grasp narratively-presented logic as in the present challenge.

"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.
Your Guide to Achieving IT Business Success

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.

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

Open in new window

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

Open in new window


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

Open in new window


please advise
krakatoaCommented:
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);
 
 }
 
}

Open in new window

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

Open in new window


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
krakatoaCommented:
What you need to think about is when the ints are the same and you run the mod test first.
gudii9Author Commented:
What you need to think about is when the ints are the same and you run the mod test first.

Open in new window


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

Open in new window


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.
krakatoaCommented:
Ask yourself what happens when a==10 and say b==20.
gudii9Author Commented:
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);
  }

Open in new window

gudii9Author Commented:
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)
gudii9Author Commented:
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);
 
}
 
 
 }
 
 
 
 
if code as above i see why below is failing

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?
krakatoaCommented:
Yes, understanding the wording is important.

But it looks like you can see the reason now.
gudii9Author Commented:
yes kind of 90%
krakatoaCommented:
So you must then know the 10% that you still don't know . . . so what is that last 10% ?
gudii9Author Commented:
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
krakatoaCommented:
"Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the [sic] return the smaller value. However, in all cases, if the two values are the same, return 0. "

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.
gudii9Author Commented:
However means BUT and EXCEPT
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?
gudii9Author Commented:
i am getting after trying 5-10 times that some sentence is most particular? But not first time or just before start attacking the challenge?
krakatoaCommented:
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.
"
gudii9Author Commented:
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?
krakatoaCommented:
I thought you might list the tests, in the order they need to be made, to decide what action to take.
krakatoaCommented:
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.
gudii9Author Commented:
I too like flow chart than anything else
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.