Coding Bat – in3050

This exercise requires that the program return the value of true if both of two numbers entered are between 30 and 40 (inclusive) or between 40 and 50 inclusive.

Here is the code that you start with:

public boolean in3050(int a, int b) {
  
}

As you can see, we have two integer variables named “a” and “b”.

public boolean in3050(int a, int b) {
  if (a >= 30 && a <= 40 && b >= 30 && b <= 40) {
    return true;
  }
  if (a >= 40 && a <= 50 && b >= 40 && b <= 50) {
    return true;
  }
  return false;
}

In the solution provided, we have a Boolean that returns true if A is any of the following values:

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

AND if B is any of the following values:

30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

We have a second conditional statement that returns true if A is any of the following values:

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

AND if B is any of the following values:

40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50

Otherwise, a false is returned.

An alternate solution could also be:

public boolean in3050(int a, int b) {
  if ((a >= 30 && a <= 40 && b >= 30 && b <= 40) || (a >= 40 && a <= 50 && b >= 40 && b <= 50)) {
    return true;
  }
  return false;
}

Here, we have combined the else if portion of the original solution into a BOOLEAN or statement.

Leave a Reply

Your email address will not be published. Required fields are marked *