Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29613

Re: in1To10

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Mike Amling <mamling@chaff.us>
Newsgroups comp.lang.java.programmer
Subject Re: in1To10
Date Sun, 10 Aug 2014 00:29:29 -0500
Lines 65
Message-ID <c4oe9kFrkf1U1@mid.individual.net> (permalink)
References <0c8de66a-429a-4697-b575-35f37a7b6882@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net wHf0vGNmGHOdGR0Oq43Ykw/ivXWMy9VgLoXBD635jKQ3aGtFjm
Cancel-Lock sha1:TzlTVkCILxKCZW4KJsTBeAwfjOU=
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <0c8de66a-429a-4697-b575-35f37a7b6882@googlegroups.com>
Xref csiph.com comp.lang.java.programmer:29613

Show key headers only | View raw


On 8/9/14 2:12 PM, cdalten@gmail.com wrote:
> from the url
>
> http://codingbat.com/prob/p137365
>
> The problem states: "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."
>
> Here is what I came up with...
>
> public class In1to10 {
> 	static boolean in1To10(int n, boolean outsideMode) {
> 		
> 		if (outsideMode == false){
> 			if (n >= 1 && n <=10) {
> 				return true;
> 			}
> 		} else if (outsideMode == true){
> 			if (n <=1 || n >=10) {
> 				return true;
> 			}
> 		}
> 		
> 		return false;
> 	}
> 	public static void main(String[] args) {
> 		System.out.println(in1To10(5, false)); //true
> 		System.out.println(in1To10(11, false)); //false
> 		System.out.println(in1To10(11, true)); //true
> 	}//end main
> }
>
>
> Now upon googling the solutions, I saw some people use
> if(outsideMode == false)
> if(outsideMode == true)
>
> instead of using if(outsideMode == false)/else(outsideMode == true).
>
> Would both catch all the cases?

Yes. A Java boolean variable, parameter or expression is always either 
true or false. There is no null or other value beyond those two.

There's also no point in coding (<some boolean expression> == true), as 
this is necessarily identical to (<some boolean expression>), so you 
could safely reduce your if statements to

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

or even

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

Note: outsideMode is a (primitive) boolean, not to be confused with an 
instance of java.lang.Boolean, which could be null.

--Mike Amling
Qm9vbGVhbiBkZWNpc2lvbj1udWxsOyBpcyBmb3IgZmVuY2Utc2l0dGluZy4=

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

in1To10 cdalten@gmail.com - 2014-08-09 12:12 -0700
  Re: in1To10 Jeff Higgins <jeff@invalid.invalid> - 2014-08-09 15:58 -0400
  Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-09 16:13 -0400
  Re: in1To10 Marcel Mueller <news.5.maazl@spamgourmet.org> - 2014-08-09 22:56 +0200
    Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-09 17:02 -0400
      Re: in1To10 Marcel Mueller <news.5.maazl@spamgourmet.org> - 2014-08-09 23:43 +0200
        Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-09 19:49 -0400
        Re: in1To10 Torsten Kirschner <torsten@kirschner.no> - 2014-08-10 16:59 +0200
          Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-10 11:33 -0400
            Re: in1To10 Torsten Kirschner <torsten@kirschner.no> - 2014-08-11 22:23 +0200
            Re: in1To10 Gene Wirchenko <genew@telus.net> - 2014-08-12 11:10 -0700
              Re: in1To10 Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 16:54 -0400
                Re: in1To10 Gene Wirchenko <genew@telus.net> - 2014-08-20 10:36 -0700
            Re: in1To10 Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 16:53 -0400
  Re: in1To10 cdalten@gmail.com - 2014-08-09 18:35 -0700
    Re: in1To10 Jeff Higgins <jeff@invalid.invalid> - 2014-08-09 22:13 -0400
    Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-09 22:23 -0400
    Re: in1To10 Jeff Higgins <jeff@invalid.invalid> - 2014-08-10 12:07 -0400
  Re: in1To10 Mike Amling <mamling@chaff.us> - 2014-08-10 00:29 -0500
  Re: in1To10 "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-10 13:53 +0100
    Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-10 09:48 -0400
      Re: in1To10 "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-10 15:01 +0100
        Re: in1To10 Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-10 10:32 -0400
          Re: in1To10 "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-08-10 15:53 +0100
  Re: in1To10 lipska the kat <lipskathekat@example.com> - 2014-08-10 14:07 +0100
  Re: in1To10, a simple test plan lipska the kat <lipskathekat@example.com> - 2014-08-10 16:43 +0100
    Re: in1To10, a simple test plan Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-10 12:01 -0400
      Re: in1To10, a simple test plan lipska the kat <lipskathekat@example.com> - 2014-08-10 17:41 +0100
      Re: in1To10, a simple test plan Arne Vajhøj <arne@vajhoej.dk> - 2014-08-19 16:57 -0400
  Re: in1To10 Roedy Green <see_website@mindprod.com.invalid> - 2014-08-12 02:59 -0700

csiph-web