Avatar of gudii9
gudii9
Flag 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
Java EEJavaJSPProgramming Languages-OtherProgramming

Avatar of undefined
Last Comment
gurpsbassi

8/22/2022 - Mon
SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ozo

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
gurpsbassi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ozo

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

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ozo

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gurpsbassi

excellent
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?
gurpsbassi

people have already posted improved solutions. take a look.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Jim Cakalic

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

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
ozo

"on that day, your speed can be 5 higher in all cases"
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
gurpsbassi

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

Try putting brackets in place if it helps your understanding.