Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
}
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
17:   else if(speed >= 61 && speed <= 80 + allowableTolerance){
  speed >= 61 &&
is unnecessary
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.
If you are trying to express the requirements, shouldn't it be
  speed >= 61+allowableTolerance
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.
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
excellent
Avatar of gudii9

ASKER

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?
people have already posted improved solutions. take a look.
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

Avatar of gudii9

ASKER

 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
"on that day, your speed can be 5 higher in all cases"
Avatar of gudii9

ASKER

 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
how to comprehend statement with more than two , three ternary statements as above.

Try putting brackets in place if it helps your understanding.