If you are trying to express the requirements, shouldn't it be
speed >= 61+allowableTolerance
gurpsbassi
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
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
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;}
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
"on that day, your speed can be 5 higher in all cases"
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
gurpsbassi
how to comprehend statement with more than two , three ternary statements as above.
Try putting brackets in place if it helps your understanding.
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
}
}