Problem:
You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
caughtSpeeding(60, false) → 0
caughtSpeeding(65, false) → 1
caughtSpeeding(65, true) → 0
Solution:
public int caughtSpeeding(int speed, boolean isBirthday) { if (!(isBirthday)) { if (speed <= 60) return 0; if (speed > 60 && speed <= 80) return 1; else return 2; } else if (speed <=65) return 0; else if (speed > 65 && speed <= 85) return 1; else return 2; }
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteint tmp = isBirthday?5:0;
if(speed>=81+tmp)
return 2;
else if(speed<=60+tmp)
return 0;
else
return 1;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteint result=0;
if(isBirthday){
speed=speed-5;
}else{
speed=speed;
}
if(speed<=60){
result=0;
}else if(speed>=61 && speed <=80){
result=1;
}else if(speed>80){
result=2;
}
return result;
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteint ticket = 0;
if (isBirthday) speed -= 5;
if (speed <= 60) ticket = 0;
if (speed >60 && speed <= 80) ticket = 1;
if (speed > 80) ticket = 2;
return ticket;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteif(isBirthday) speed -= 5;
if(speed<61) return 0;
if(speed>80) return 2;
return 1;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteint ticket = 0;
if (isBirthday) {
speed -= 5;
}
if (speed > 60 && speed <= 80) {
ticket = 1;
}
else if (speed > 80) {
ticket = 2;
}
return ticket;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeletereturn (speed - ((isBirthday) ? 5 : 0) > 60) ? (speed - ((isBirthday) ? 5 : 0) > 80) ? 2 : 1 : 0;
}
One line boys!
I'm slow, I didn't realize this could be done with a one-liner.
DeleteI'm curious as to how you're able to say "speed - ((isBirthday)? 5:0) > 60"?
speed is an int, and isBirthday is a boolean value, how are you able to subtract a boolean value from an interger then compare it to 60? Can you explain this thought process if you have time?
I am also late but he/she is not subtracting an int with a bool. Look closely: 'isBirthday' is nested within the outer parenthesis, therefore, that is evaluated first. If 'isBirthday' is true, it returns 5 and 5 gets substracted with speed, otherwise it's 0.
Deletepublic int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteif(speed <= 60 && !isBirthday || speed <= 65 && isBirthday) return 0;
return (speed >= 61 && speed <= 80 && !isBirthday) ||(speed >= 66 && speed <= 85 && isBirthday) ? 1:2;
}
But that one liner posted earlier though!
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteif(isBirthday && (speed-5)<61 || !isBirthday && speed<61) return 0;
if(isBirthday && (speed-5)>60 && (speed-5)<81 || !isBirthday && speed>60 && speed<81)
return 1;
else return 2;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteif (speed <= 60 || speed <= 65 && isBirthday){
return 0;
}
if (speed <= 80 || speed <= 85 && isBirthday){
return 1;
}
return 2;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteif((speed>80&&!isBirthday)||(speed>85&&isBirthday))
return 2;
else if ((speed>60&&!isBirthday)||(speed>65&&isBirthday))
return 1;
else
return 0;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeletewhile(isBirthday){
if(speed<=65) return 0;
if(speed<=85) return 1;
else return 2;}
while(!isBirthday){
if(speed<=60) return 0;
if(speed<=80) return 1;
else return 2;
}
return 2;
}
public int caughtSpeeding(int speed, boolean isBirthday) {
ReplyDeleteint i=isBirthday? 5 : 0;
return speed<=60+i?0:(speed>60+i && speed<=80+i)?1:2;
}