0

Hi I'm new to Java and I need a little help.The problem is from coding bat:

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.

 public int caughtSpeeding(int speed, boolean isBirthday) {
      Integer int2 = 0;
      if (speed <= 60){
        int2 = 0;
      }
      if (isBirthday = true){
       if (speed >=61 && speed <= 85){
        int2 = 1;
       }
       if (speed >= 86){
         int2 = 2;
       }
      }
      if (isBirthday = false){
        if (speed >=61 && speed <=80){
          int2 = 1;
        }
        if (speed >= 81){
          int2 = 2;
        }
      }
      return int2; 
    }

I'm getting caughtSpeeding(65, true) should be 0 when my code runs to = 1 and caughtSpeeding(85, false) should be 2 when my code runs to = 1 again.

Thanks

Monty
  • 107
  • 1
  • 3
  • 9
  • `=` assignment, `==` equality – Guy Jun 07 '17 at 12:58
  • you said you are a beginner, so on a side note: there is no reason to use the `Integer` type instead of the primitive `int` for `int2`. If you can choose, use the primitive type as you save yourself the autoboxing. see [int vs Integer](https://stackoverflow.com/questions/8660691/what-is-the-difference-between-integer-and-int-in-java/8660812#8660812) – Rhayene Jun 07 '17 at 13:15

5 Answers5

0

For starters, this line:

if (isBirthday = true){

should be changed to:

if (isBirthday == true){

The equality operator that checks if two expressions are equal is == and not =. After fixing this, there's still something else you have to check in your code. Read the problem carefully again and make sure your logic is returning the correct output in all possible cases.

0

Change your isBirthday if statements slightly.

= // 'make this left-side value equal something'. 
== // 'comparison of two values'

so for isBirthday you could have

if(isBirthday) // is 'isBirthday' true?
if(isBirthday == true) // same as above

if(!isBirthday) // is 'isBirthday' not true (false)?
if(isBirthday == false) // same as above
Ben Brookes
  • 419
  • 4
  • 15
0
    public int caughtSpeeding(int speed, boolean isBirthday)
    {
    if(speed<=60||(isBirthday&&speed<=65))
    return 0;
    else if(speed>=61&&speed<=80||(isBirthday&&speed<=85))
    return 1;
    else
    return 2;
    }
-1

Fixed it:

public int caughtSpeeding(int speed, boolean isBirthday) {
      Integer int2 = 0;
      if (speed <= 60){
        int2 = 0;
      }
      if (isBirthday == true){
       if (speed <=65){
         int2 = 0;
       }
       if (speed >=66 && speed <= 85){
        int2 = 1;
       }
       if (speed >= 86){
         int2 = 2;
       }
      }
      if (isBirthday == false){
        if (speed >=61 && speed <=80){
          int2 = 1;
        }
        if (speed >= 81){
          int2 = 2;
        }
      }
      return int2; 
    }
Monty
  • 107
  • 1
  • 3
  • 9
-1

you can try this too

public static int caughtSpeeding(int speed, boolean isBirthday) {

    int extra = 0;
    if (isBirthday) {
        extra = 5;
    }
    if (speed <= (60 + extra)) {
        return 0;
    } else if (speed >= (61 + extra) && speed <= (80 + extra)) {
        return 1;
    } else {
        return 2;
    }
}