caughtSpeeding java challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p157733

public int caughtSpeeding(int speed, boolean isBirthday) {
int result;

if(speed<=65){
if(speed<=60&& isBirthday==true){
return result=0;
}
else {
return result=0;
}
}
  
else if(speed<=85){
if(speed<=61&& isBirthday==true){
return result=1;
}
else {
return result=1;
}
}

return result=2;
  
}

Open in new window


Expected      Run            
caughtSpeeding(60, false) → 0      0      OK         
caughtSpeeding(65, false) → 1      0      X         
caughtSpeeding(65, true) → 0      0      OK         
caughtSpeeding(80, false) → 1      1      OK         
caughtSpeeding(85, false) → 2      1      X         
caughtSpeeding(85, true) → 1      1      OK         
caughtSpeeding(70, false) → 1      1      OK         
caughtSpeeding(75, false) → 1      1      OK         
caughtSpeeding(75, true) → 1      1      OK         
caughtSpeeding(40, false) → 0      0      OK         
caughtSpeeding(40, true) → 0      0      OK         
caughtSpeeding(90, false) → 2      2      OK         
other tests
X         

i am failing two test cases as above. 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.

ozoCommented:
public int caughtSpeeding(int speed, boolean isBirthday) {
  if( isBirthday ){ speed -= 5; }
  return speed<=60?0:speed<=80?1:2;
}
0
ozoCommented:
caughtSpeeding(65, false) → 1
but your code executes
if(speed<=65){ // true
 if(speed<=60&& isBirthday==true){  // false
 ...
}
else {
  return result=0;  // wrong
}
}


caughtSpeeding(85, false) → 2
but your code executes
if(speed<=65){ // false
...
}
else if(speed<=85){ // true
if(speed<=61&& isBirthday==true){ // false
 ,,,
}
else {
return result=1; // wrong
}
}
0
gurpsbassiCommented:
public int caughtSpeeding(int speed, boolean isBirthday) {

  final int NO_TICKET = 0;
  final int SMALL_TICKET = 1;
  final int BIG_TICKET = 2;
  
  int allowableTolerance = 0;
  
  if(isBirthday){
    allowableTolerance = 5;
  }
  
  
  if(speed <= 60 + allowableTolerance){
    return NO_TICKET;
  }
  else if(speed >= 61 && speed <= 80 + allowableTolerance){
    return SMALL_TICKET;
  }
  
    return BIG_TICKET;
}

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

ozoCommented:
17:   else if(speed >= 61 && speed <= 80 + allowableTolerance){
  speed >= 61 &&
is unnecessary
0
gurpsbassiCommented:
speed >= 61 &&
is unnecessary

Mathematically, you are absolutely correct. However I prefer to write it in this way since I believe it expresses the requirement clearly.
0
ozoCommented:
If you are trying to express the requirements, shouldn't it be
  speed >= 61+allowableTolerance
0
gurpsbassiCommented:
you "could" write it that way if you wanted to.
However tolerance is applicable to upper limit of the range.

Its purely debatable, depends on individual coding style.
0
ozoCommented:
The requirements say 5 higher in all cases so it should also apply to the lower range.
Had the tests been reversed,
 if( speed >= 61+ allowableTolerance &&  speed <= 80 + allowableTolerance){
    return SMALL_TICKET;
  }
  if(speed <= 60 + allowableTolerance){
    return NO_TICKET;
  }
would still give correct results, but
  if( speed >= 61 &&  speed <= 80 + allowableTolerance){
    return SMALL_TICKET;
  }
  if(speed <= 60 + allowableTolerance){
    return NO_TICKET;
  }
would give incorrect results.
The only reason you can get away with a test that does not express the requirement is that the order of the tests makes it unnecessary.
A test that is both incorrect and unnecessary is not expressing the requirement clearly
1
gurpsbassiCommented:
excellent
0
gudii9Author Commented:
public int caughtSpeeding(int speed, boolean isBirthday) {
		
		int result = 0;
		
		if(speed<60||(isBirthday&&speed<=65)){
			
			result=0;
		}
		

		else if((speed>=61&&speed<=80)||(isBirthday&&(speed>=61&&speed<=85))){
			
			result=1;
		}
		
	else if(speed>=81||(isBirthday&&speed>=85)){
			
			result=2;
		}
		
		return result;
		

}

Open in new window

i changed my approach bit as above and passing all tests. please advise on any improvements?
0
gurpsbassiCommented:
people have already posted improved solutions. take a look.
0
Jim CakalicSenior Developer/ArchitectCommented:
public int caughtSpeeding(int speed, boolean isBirthday) {
  int small = limit(60, isBirthday);
  int big = limit(80, isBirthday);
  
  return speed <= small ? 0 : speed <= big ? 1 : 2;
}

private int limit(int speed, boolean isBirthday) {
  return isBirthday ? speed + 5: speed;
}

Open in new window

1
gudii9Author Commented:
 if( isBirthday ){ speed -= 5; }
  return speed<=60?0:speed<=80?1:2;

not clear on above solution.

if isBirthday is true then speed=speed-5 then returning ternary(speed<=60?0:speed<=80?1:2;)

ternary is speed is less then or equal to 60 then 0 ticket if false then check speed <=80 if true then 1 if false ie beyond 80 then 2 ticket right?
what is the use of

speed=speed-5

Please advise
0
ozoCommented:
"on that day, your speed can be 5 higher in all cases"
0
gudii9Author Commented:
 return speed <= small ? 0 : speed <= big ? 1 : 2;

Open in new window


how to comprehend statement with more than two , three ternary statements as above.
I got true part.
I did not get false part.
If false we are saying return
speed <= big ? 1 : 2;//here again checking if true 1 and false 2.. i am not clear here.
please advise
0
gurpsbassiCommented:
how to comprehend statement with more than two , three ternary statements as above.

Try putting brackets in place if it helps your understanding.
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.