caughtSpeeding java challenge

gudii9
gudii9 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
public int caughtSpeeding(int speed, boolean isBirthday) {
  if( isBirthday ){ speed -= 5; }
  return speed<=60?0:speed<=80?1:2;
}
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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
}
}
Top Expert 2015
Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
17:   else if(speed >= 61 && speed <= 80 + allowableTolerance){
  speed >= 61 &&
is unnecessary
Top Expert 2015

Commented:
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.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
If you are trying to express the requirements, shouldn't it be
  speed >= 61+allowableTolerance
Top Expert 2015

Commented:
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.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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
Top Expert 2015

Commented:
excellent

Author

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?
Top Expert 2015

Commented:
people have already posted improved solutions. take a look.
Jim CakalicSenior Engineer

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

Author

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
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
"on that day, your speed can be 5 higher in all cases"

Author

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
Top Expert 2015

Commented:
how to comprehend statement with more than two , three ternary statements as above.

Try putting brackets in place if it helps your understanding.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial