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


Groups > comp.lang.java.programmer > #29597 > unrolled thread

in1To10

Started bycdalten@gmail.com
First post2014-08-09 12:12 -0700
Last post2014-08-12 02:59 -0700
Articles 20 on this page of 30 — 11 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

Page 1 of 2  [1] 2  Next page →


#29597 — in1To10

Fromcdalten@gmail.com
Date2014-08-09 12:12 -0700
Subjectin1To10
Message-ID<0c8de66a-429a-4697-b575-35f37a7b6882@googlegroups.com>
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? If not, which one would?

Chad

[toc] | [next] | [standalone]


#29598

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-09 15:58 -0400
Message-ID<ls5uh6$45h$1@dont-email.me>
In reply to#29597
On 08/09/2014 03: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? If not, which one would?

The Java Tutorial explains quite well the difference between
if-then and if-then-else statements. Also what happens when
a branching statement is encountered.
<http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html>
<http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html>

>
> Chad
>

[toc] | [prev] | [next] | [standalone]


#29599

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-09 16:13 -0400
Message-ID<ls5vdf$b83$1@dont-email.me>
In reply to#29597
On 8/9/2014 3: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? If not, which one would?

     Since a `boolean' can represent only the two values `true'
and `false', it follows that

     - If it isn't `true' it must be `false', and

     - If it isn't `false' it must be `true'.

Therefore, you can write

	if (outsideMode == true) {
	    // Handle the `true' case
	} else {
	    // Look, Ma: No test!
	    // Handle the `false' case
	}

... using only one `if' test.  If the test succeeds you know
`outsideMode' is `true'.  If the test fails you know `outsideMode'
is *not* `true', hence (because of the limited possibilities for
a `boolean') it must be `false'.  Alternatively, you could write

	if (outsideMode == false) {
	    // Handle the `false' case
	} else {
	    // Look, Ma: No test!
	    // Handle the `true' case
	}

... by the same reasoning: If it's not `false', it must be `true'.

     A few additional hints:

     - It's seldom if ever worth while to compare a boolean value
       to a boolean constant.  Instead of `if (outsideMode == true)'
       just write `if (outsideMode)'.  Or, if you want to be perverse,
       write `if (outsideMode == true == true == true)' ...

     - Writing `if (something) return true; else return false;' is
       kind of silly, since `return something;' does the same thing.
       Similarly, `if (something) return false; else return true;'
       can be written as `return !something;'.

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29600

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2014-08-09 22:56 +0200
Message-ID<ls61to$agh$1@news.albasani.net>
In reply to#29597
On 09.08.14 21.12, cdalten@gmail.com wrote:
> 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? If not, which one would?

There is almost always more than one solution for a given problem.
Since booleans can only be true or false the second if (after else) is 
superfluous. And you also could simply write
   return (n >= 1 && n <= 10) ^ outsideMode;

The solutions might differ in execution speed or code length and style 
but as long this is not part of the task it makes no difference.


Marcel

[toc] | [prev] | [next] | [standalone]


#29601

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-09 17:02 -0400
Message-ID<ls62a5$4gr$1@dont-email.me>
In reply to#29600
On 8/9/2014 4:56 PM, Marcel Mueller wrote:
> On 09.08.14 21.12, cdalten@gmail.com wrote:
>> 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? If not, which one would?
>
> There is almost always more than one solution for a given problem.
> Since booleans can only be true or false the second if (after else) is
> superfluous. And you also could simply write
>    return (n >= 1 && n <= 10) ^ outsideMode;
>
> The solutions might differ in execution speed or code length and style
> but as long this is not part of the task it makes no difference.

     Unfortunately, this cleaned-up version does not solve the
problem as specified.  (Hint: What is `in1To10(1,true)'?)

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29602

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2014-08-09 23:43 +0200
Message-ID<ls64li$gf1$1@news.albasani.net>
In reply to#29601
On 09.08.14 23.02, Eric Sosman wrote:
> Unfortunately, this cleaned-up version does not solve the
> problem as specified. (Hint: What is `in1To10(1,true)'?)

Oops, you are right, I read a bit fast. 1 and 10 should always return true.


Marcel

[toc] | [prev] | [next] | [standalone]


#29603

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-09 19:49 -0400
Message-ID<ls6c22$pk9$1@dont-email.me>
In reply to#29602
On 8/9/2014 5:43 PM, Marcel Mueller wrote:
> On 09.08.14 23.02, Eric Sosman wrote:
>> Unfortunately, this cleaned-up version does not solve the
>> problem as specified. (Hint: What is `in1To10(1,true)'?)
>
> Oops, you are right, I read a bit fast. 1 and 10 should always return true.

     If it's any comfort, I very nearly made the same mistake.
Long ago I took a class in "Speed Reading," and I've been
struggling ever since to un-learn its lessons ...

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29627

FromTorsten Kirschner <torsten@kirschner.no>
Date2014-08-10 16:59 +0200
Message-ID<c4pfluF3slmU1@mid.individual.net>
In reply to#29602
Den 09.08.2014 23:43, skrev Marcel Mueller:
> On 09.08.14 23.02, Eric Sosman wrote:
>> Unfortunately, this cleaned-up version does not solve the
>> problem as specified. (Hint: What is `in1To10(1,true)'?)
> 
> Oops, you are right, I read a bit fast. 1 and 10 should always return true.

So...

   static boolean in1To10(int n, boolean outsideMode) {

        if (n == 1 || n == 10)
            return true;

        return (n > 1 && n < 10) ^ outsideMode;
    }

[toc] | [prev] | [next] | [standalone]


#29629

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-10 11:33 -0400
Message-ID<ls83bt$42j$1@dont-email.me>
In reply to#29627
On 8/10/2014 10:59 AM, Torsten Kirschner wrote:
> Den 09.08.2014 23:43, skrev Marcel Mueller:
>> On 09.08.14 23.02, Eric Sosman wrote:
>>> Unfortunately, this cleaned-up version does not solve the
>>> problem as specified. (Hint: What is `in1To10(1,true)'?)
>>
>> Oops, you are right, I read a bit fast. 1 and 10 should always return true.
>
> So...
>
>     static boolean in1To10(int n, boolean outsideMode) {
>
>          if (n == 1 || n == 10)
>              return true;
>
>          return (n > 1 && n < 10) ^ outsideMode;
>      }

     Looks right to me.  It could be abbreviated:

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

... but to my eye the clearest expression would be something like:

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

     (I've seen coding standards that discourage the ?: operator on the
grounds that "it's confusing," but to me that seems bogus.  People who
write such standards must hold a poor opinion of the people who will
have to follow them, and I don't want to work with people who have such
a poor opinion of me.  Hiring a Java programmer who's confused by ?:
would be like hiring a tone-deaf violinist.)

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29638

FromTorsten Kirschner <torsten@kirschner.no>
Date2014-08-11 22:23 +0200
Message-ID<c4sn2lFopo9U1@mid.individual.net>
In reply to#29629
Den 10.08.14 17:33, skrev Eric Sosman:
> On 8/10/2014 10:59 AM, Torsten Kirschner wrote:
>> Den 09.08.2014 23:43, skrev Marcel Mueller:
>>> On 09.08.14 23.02, Eric Sosman wrote:
>>>> Unfortunately, this cleaned-up version does not solve the
>>>> problem as specified. (Hint: What is `in1To10(1,true)'?)
>>>
>>> Oops, you are right, I read a bit fast. 1 and 10 should always return
>>> true.
>>
[...]
>     Looks right to me.  It could be abbreviated:
> 
>     return n == 1 || n == 10 || ((n > 1 && n < 10) ^ outsideMode);

:-)

> ... but to my eye the clearest expression would be something like:
> 
>     return outsideMode
>         ? (n <= 1 || n >= 10)
>         : (n >= 1 && n <= 10);

Concise yet fully self-explanatory code for the problem, nice.

[toc] | [prev] | [next] | [standalone]


#29646

FromGene Wirchenko <genew@telus.net>
Date2014-08-12 11:10 -0700
Message-ID<d1mku9ltiu5cpmrd689o59tn1u40m2sjd1@4ax.com>
In reply to#29629
On Sun, 10 Aug 2014 11:33:17 -0400, Eric Sosman
<esosman@comcast-dot-net.invalid> wrote:

[snip]

>     (I've seen coding standards that discourage the ?: operator on the
>grounds that "it's confusing," but to me that seems bogus.  People who
>write such standards must hold a poor opinion of the people who will
>have to follow them, and I don't want to work with people who have such
>a poor opinion of me.  Hiring a Java programmer who's confused by ?:
>would be like hiring a tone-deaf violinist.)

     On the other side, some people will cascade them into something
that is difficult to follow.  ?: is not the only thing.  I have seen
plenty of questions of how to do something using only one SQL
statement with the poster already having something that works but is
two statements.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#29741

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-08-19 16:54 -0400
Message-ID<53f3b9a3$0$293$14726298@news.sunsite.dk>
In reply to#29646
On 8/12/2014 2:10 PM, Gene Wirchenko wrote:
> On Sun, 10 Aug 2014 11:33:17 -0400, Eric Sosman
> <esosman@comcast-dot-net.invalid> wrote:
>>      (I've seen coding standards that discourage the ?: operator on the
>> grounds that "it's confusing," but to me that seems bogus.  People who
>> write such standards must hold a poor opinion of the people who will
>> have to follow them, and I don't want to work with people who have such
>> a poor opinion of me.  Hiring a Java programmer who's confused by ?:
>> would be like hiring a tone-deaf violinist.)
>
>       On the other side, some people will cascade them into something
> that is difficult to follow.  ?: is not the only thing.  I have seen
> plenty of questions of how to do something using only one SQL
> statement with the poster already having something that works but is
> two statements.

It may not be purely aesthetic reasons. Database round trips do come
with some overhead.

Arne

[toc] | [prev] | [next] | [standalone]


#29770

FromGene Wirchenko <genew@telus.net>
Date2014-08-20 10:36 -0700
Message-ID<61n9v991ctvftve2mvg2gjpqcj42b2hn8l@4ax.com>
In reply to#29741
On Tue, 19 Aug 2014 16:54:53 -0400, Arne Vajhøj <arne@vajhoej.dk>
wrote:

>On 8/12/2014 2:10 PM, Gene Wirchenko wrote:
>> On Sun, 10 Aug 2014 11:33:17 -0400, Eric Sosman
>> <esosman@comcast-dot-net.invalid> wrote:
>>>      (I've seen coding standards that discourage the ?: operator on the
>>> grounds that "it's confusing," but to me that seems bogus.  People who
>>> write such standards must hold a poor opinion of the people who will
>>> have to follow them, and I don't want to work with people who have such
>>> a poor opinion of me.  Hiring a Java programmer who's confused by ?:
>>> would be like hiring a tone-deaf violinist.)
>>
>>       On the other side, some people will cascade them into something
>> that is difficult to follow.  ?: is not the only thing.  I have seen
>> plenty of questions of how to do something using only one SQL
>> statement with the poster already having something that works but is
>> two statements.
>
>It may not be purely aesthetic reasons. Database round trips do come
>with some overhead.

     So does modifying complex SQL.  I have some reports that I sure
hope I never have to make more than cosmetic changes to, because I
would have to take apart the code to figure out how to generate the
report cursors I need.

     Write-only code is awkward.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#29740

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-08-19 16:53 -0400
Message-ID<53f3b955$0$293$14726298@news.sunsite.dk>
In reply to#29629
On 8/10/2014 11:33 AM, Eric Sosman wrote:
> On 8/10/2014 10:59 AM, Torsten Kirschner wrote:
>> Den 09.08.2014 23:43, skrev Marcel Mueller:
>>> On 09.08.14 23.02, Eric Sosman wrote:
>>>> Unfortunately, this cleaned-up version does not solve the
>>>> problem as specified. (Hint: What is `in1To10(1,true)'?)
>>>
>>> Oops, you are right, I read a bit fast. 1 and 10 should always return
>>> true.
>>
>> So...
>>
>>     static boolean in1To10(int n, boolean outsideMode) {
>>
>>          if (n == 1 || n == 10)
>>              return true;
>>
>>          return (n > 1 && n < 10) ^ outsideMode;
>>      }
>
>      Looks right to me.  It could be abbreviated:
>
>      return n == 1 || n == 10 || ((n > 1 && n < 10) ^ outsideMode);
>
> ... but to my eye the clearest expression would be something like:
>
>      return outsideMode
>          ? (n <= 1 || n >= 10)
>          : (n >= 1 && n <= 10);

I would tend to think that by far the clearest is two methods - one
for inner and one for outer.

:-)

Arne

[toc] | [prev] | [next] | [standalone]


#29604

Fromcdalten@gmail.com
Date2014-08-09 18:35 -0700
Message-ID<7583c754-bd0d-4de1-bd91-0e84fc458d2c@googlegroups.com>
In reply to#29597
Okay, so if I listen to the guy that also hangs out on comp.lang.c, then I get something like...


public class In1to10 {
	static boolean in1To10(int n, boolean outsideMode) {
		boolean mode = false;
		
		if (outsideMode == false){
			if (n >= 1 && n <=10) {
				mode = true;
			}
		} else {
			mode = true;
		}
		
		return mode;
	}
	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
}

With that, I am going to also wander over to comp.lang.c and see if Kenny and crew is arguing with Keith and crew.

Chad

[toc] | [prev] | [next] | [standalone]


#29606

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-09 22:13 -0400
Message-ID<ls6kh5$1u8$1@dont-email.me>
In reply to#29604
On 08/09/2014 09:35 PM, cdalten@gmail.com wrote:
> Okay, so if I listen to the guy that also hangs out on comp.lang.c, then I get something like...
>
>
> public class In1to10 {
> 	static boolean in1To10(int n, boolean outsideMode) {
> 		boolean mode = false;
> 		
> 		if (outsideMode == false){
> 			if (n >= 1 && n <=10) {
> 				mode = true;
> 			}
> 		} else {
> 			mode = true;
> 		}
> 		
> 		return mode;
> 	}
> 	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
> }
>
> With that, I am going to also wander over to comp.lang.c and see if Kenny and crew is arguing with Keith and crew.
>
Oops. Happy trails.

[toc] | [prev] | [next] | [standalone]


#29607

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-08-09 22:23 -0400
Message-ID<ls6l34$4fp$1@dont-email.me>
In reply to#29604
On 8/9/2014 9:35 PM, cdalten@gmail.com wrote:
> Okay, so if I listen to the guy that also hangs out on comp.lang.c,

     Not Recommended.

> then I get something like...
>
> public class In1to10 {
> 	static boolean in1To10(int n, boolean outsideMode) {
> 		boolean mode = false;
> 		
> 		if (outsideMode == false){
> 			if (n >= 1 && n <=10) {
> 				mode = true;
> 			}
> 		} else {
> 			mode = true;
> 		}
> 		
> 		return mode;
> 	}

     You've "simplified" yourself into an error.  Observe what
happens when `outsideMode' is `true': The method returns `true'
for *every* `n': 1, 5, 10, 42, -1073741824, you name it.

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#29632

FromJeff Higgins <jeff@invalid.invalid>
Date2014-08-10 12:07 -0400
Message-ID<ls85cs$i7j$1@dont-email.me>
In reply to#29604
<http://www.i-mockery.com/minimocks/starwars-holiday/24.gif>

[toc] | [prev] | [next] | [standalone]


#29613

FromMike Amling <mamling@chaff.us>
Date2014-08-10 00:29 -0500
Message-ID<c4oe9kFrkf1U1@mid.individual.net>
In reply to#29597
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=

[toc] | [prev] | [next] | [standalone]


#29621

From"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Date2014-08-10 13:53 +0100
Message-ID<M-6dnTFSjNeO9nrOnZ2dnUVZ8rednZ2d@bt.com>
In reply to#29597
cdalten@gmail.com wrote:

> 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."

Not really addressing the question, but nobody (I've seen) has mentioned what I 
would consider the most direct encoding of the problem which has IMO the most 
"eloquent" use of Boolean-valued variables.  It also, IMO, illustrates the need 
for /very carefully/ considered identifiers[*] (which for some reason seems to 
be at its most acute for Booleans -- I have no idea why, anyone ?).

Not tested. Some might prefer more brackets.

static boolean testAgainst1to10(int i, boolean trueIfOutside)
{
    boolean isOutside = i < 1 || i > 10;
    return isOutside == trueIfOutside;
}

    -- chris

[*] Not that I'm claiming that my choices of names /are/ especially good -- on 
the contrary I claim that there cannot /be/ any really good names for such a 
wildly artificial challenge (but I've done the best I can). 

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.java.programmer


csiph-web