Java > Logic-1 > in1To10 (CodingBat Solution)

Problem:

Given a number n, return true if n is in the range 1..10, inclusive. Unless "outsideMode" is true, in which case return true if the number is less or equal to 1, or greater or equal to 10.

in1To10(5, false) → true
in1To10(11, false) → false
in1To10(11, true) → true


Solution:

public boolean in1To10(int n, boolean outsideMode) {
  if (!outsideMode){
    if (n >= 1 && n <= 10) {
      return true;
    } else {
        return false;
      }
  } else {
      if (n <= 1 || n >= 10) {
        return true;
      } else {
          return false;
        }
    }
}


13 comments :

  1. public boolean in1To10(int n, boolean outsideMode) {

    if(!outsideMode) return n >= 1 && n <= 10 ? true : false;
    return n <= 1 || n >= 10 ? true : false;

    }

    ReplyDelete
  2. Even less confusing and simpler is:

    public boolean in1To10(int n, boolean outsideMode) {

    if(outsideMode) return n <= 1 || n >= 10;
    return n >= 1 && n <= 10;
    }

    ReplyDelete
  3. public boolean in1To10(int n, boolean outsideMode) {
    if(outsideMode) return n <= 1 || n >= 10;
    return n >= 1 && n <= 10;
    }

    ReplyDelete
  4. public boolean in1To10(int n, boolean outsideMode) {

    return ((!outsideMode&&(n>=1&&n<=10))||(outsideMode&&(n<=1||n>=10)));
    }

    ReplyDelete
  5. public boolean in1To10(int n, boolean outsideMode) {

    return outsideMode ? n <= 1 || n >= 10 : n >= 1 && n <= 10;

    }

    ReplyDelete
  6. public boolean in1To10(int n, boolean outsideMode) {

    if (!outsideMode)
    return n >= 1 && n <= 10;
    else
    return n <= 1 || n >= 10;
    }

    ReplyDelete
  7. //IMDAD MOMIN :-
    return (outsideMode? (n>=10 || n<=1) : (n>=1 && n<=10));

    ReplyDelete
  8. public boolean in1To10(int n, boolean outsideMode) {
    if(outsideMode==true && (n<=1 || n>=10))
    return true;
    else if(!(outsideMode) && (n>=1 && n<=10))
    return true;
    else return false;
    }

    ReplyDelete
  9. public boolean in1To10(int n, boolean isOutsideMode) {
    return (n == 1) != (n == 10) || isOutsideMode != (1 <= n && n <= 10);
    }

    ReplyDelete
  10. I don't understand why mine doesn't work:

    public boolean in1To10(int n, boolean outsideMode) {
    if ((n >= 1) && (n<=10) && (outsideMode == false)){
    return true;
    }
    else if ((n <= 1) || (n >= 10) && (outsideMode == true)) {
    return true;
    }
    return false;
    }

    ReplyDelete
  11. if(!outsideMode) return(n >= 1 && n <= 10);
    else return(n <= 1 || n >= 10);

    ReplyDelete
  12. public boolean in1To10(int n, boolean outsideMode) {
    if(!outsideMode) {
    if(n>=1 && n<=10) {
    return true;
    }
    }else if(outsideMode){
    if(n<=1 || n>=10) {
    return true;
    }
    }
    return false;
    }

    ReplyDelete
  13. public boolean in1To10(int n, boolean outsideMode) {
    return (n>0 && n<=10) && outsideMode==false? true : outsideMode==true && (n<=1 || n>=10) ? true : false;
    }

    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