Java > Logic-1 > caughtSpeeding (CodingBat Solution)

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;
}


14 comments :

  1. public int caughtSpeeding(int speed, boolean isBirthday) {
    int tmp = isBirthday?5:0;
    if(speed>=81+tmp)
    return 2;
    else if(speed<=60+tmp)
    return 0;
    else
    return 1;
    }

    ReplyDelete
  2. public int caughtSpeeding(int speed, boolean isBirthday) {

    int 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;

    ReplyDelete
  3. public int caughtSpeeding(int speed, boolean isBirthday) {
    int 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;
    }

    ReplyDelete
  4. public int caughtSpeeding(int speed, boolean isBirthday) {
    if(isBirthday) speed -= 5;
    if(speed<61) return 0;
    if(speed>80) return 2;
    return 1;

    }

    ReplyDelete
  5. public int caughtSpeeding(int speed, boolean isBirthday) {
    int ticket = 0;
    if (isBirthday) {
    speed -= 5;
    }
    if (speed > 60 && speed <= 80) {
    ticket = 1;
    }
    else if (speed > 80) {
    ticket = 2;
    }
    return ticket;
    }

    ReplyDelete
  6. public int caughtSpeeding(int speed, boolean isBirthday) {
    return (speed - ((isBirthday) ? 5 : 0) > 60) ? (speed - ((isBirthday) ? 5 : 0) > 80) ? 2 : 1 : 0;
    }

    One line boys!

    ReplyDelete
    Replies
    1. I'm slow, I didn't realize this could be done with a one-liner.
      I'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?

      Delete
    2. 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.

      Delete
  7. public int caughtSpeeding(int speed, boolean isBirthday) {
    if(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!

    ReplyDelete
  8. public int caughtSpeeding(int speed, boolean isBirthday) {
    if(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;



    }

    ReplyDelete
  9. public int caughtSpeeding(int speed, boolean isBirthday) {
    if (speed <= 60 || speed <= 65 && isBirthday){
    return 0;
    }
    if (speed <= 80 || speed <= 85 && isBirthday){
    return 1;
    }
    return 2;
    }

    ReplyDelete
  10. public int caughtSpeeding(int speed, boolean isBirthday) {
    if((speed>80&&!isBirthday)||(speed>85&&isBirthday))
    return 2;
    else if ((speed>60&&!isBirthday)||(speed>65&&isBirthday))
    return 1;
    else
    return 0;
    }

    ReplyDelete
  11. public int caughtSpeeding(int speed, boolean isBirthday) {
    while(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;
    }

    ReplyDelete
  12. public int caughtSpeeding(int speed, boolean isBirthday) {
    int i=isBirthday? 5 : 0;
    return speed<=60+i?0:(speed>60+i && speed<=80+i)?1:2;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact