Velocity Reviews > Java > a problem

# a problem

John
Guest
Posts: n/a

 02-11-2007
Chris Uppal wrote:
> [I sent this yesterday, but it doesn't seem to be showing up on my server.
> I'll try one more time. Apologies to anyone who sees it twice -- doubly so to
> John if the reason is that I emailed it to him instead of posting it]
>
> John wrote:
>
>
>>Personally, I don't see why your professor would restrict you in how you
>>can do your code, unless he/she was trying to get you to "think outside
>>the box".

>
>
> I suspect that a large part of this particular exercise is about understanding
> boolean-valued expressions, as something more general than the XXX that goes in
> an if (XXX) test.
>
> -- chris
>
>

It took me a while to get to the point where I could think "outside the
box" and realize there were other ways to accomplish what was being
asked besides using a conditional. I was actually quite impressed with
the fact that I was able to find another way without having to look too
far into it. I'm rather curious why the jdk doesn't have a method for

John
Guest
Posts: n/a

 02-11-2007
Lew wrote:
> (E-Mail Removed) wrote:
>
>>> hi,
>>> any one of the group ,please solve my problem
>>> "is there is any method to find given number is even
>>> or odd without using if ,else ,for,while,switch,conditional

>
>
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
>> a number is odd if it is not divisible by 2 or the last bit of the
>> number is not set .
>> so just do this ..
>> number & 1 i.e operate bit wise and with number and 1 if its 0 then
>> number is even else odd

>
>
> And someone who can't figure that out without your help needs to change
> their major to toenail clipping.
>
> Why are we spoon-feeding this homework solution to them?
>
> - Lew

I'm not far enough into Java at this point to have been able to see the
relationship between the last bit and it's parity, but you make a good
and valid point. I guess I should change my profession from IT Support
Technician to toenail clipper

questions like the OP's is, how far into their study of the language are
they? If they are only past conditionals, are they at the point where
they are able to use bitwise operations? I think a good idea in
situations like this, besides suggesting they make some effort first, is
to point them in a few right directions, like bitwise operations, array
indexing (like my idea) but don't tell them how to code it. At least
that way they will be working within the boundaries of what they know
about and won't be afraid of getting penalized if the instructor thinks
they have pilfered the solution.

My 2 cents FWIW.

John
Guest
Posts: n/a

 02-11-2007
Chris Uppal wrote:
> [I sent this yesterday, but it doesn't seem to be showing up on my server.
> I'll try one more time. Apologies to anyone who sees it twice -- doubly so to
> John if the reason is that I emailed it to him instead of posting it]
>
> John wrote:
>
>
>>Personally, I don't see why your professor would restrict you in how you
>>can do your code, unless he/she was trying to get you to "think outside
>>the box".

>
>
> I suspect that a large part of this particular exercise is about understanding
> boolean-valued expressions, as something more general than the XXX that goes in
> an if (XXX) test.
>
> -- chris
>
>

BTW - your reply showed up on my server. I was just wondering the
purpose for myself..

Lew
Guest
Posts: n/a

 02-11-2007
John wrote:
> questions like the OP's is, how far into their study of the language are
> they? If they are only past conditionals, are they at the point where
> they are able to use bitwise operations? I think a good idea in
> situations like this, besides suggesting they make some effort first, is
> to point them in a few right directions, like bitwise operations, array
> indexing (like my idea) but don't tell them how to code it. At least
> that way they will be working within the boundaries of what they know
> about and won't be afraid of getting penalized if the instructor thinks
> they have pilfered the solution.

Normally I would agree with you, but the OP is the one who kept demanding

> then post me the solution.Mr.Beaton

and showed less than zero willingness to put in even a nanoskootch of their
own effort. For that they should be deprived of answers altogether, perhaps
even ridiculed or held as objects of contempt.

- Lew

Lew
Guest
Posts: n/a

 02-11-2007
John wrote:
> It took me a while to get to the point where I could think "outside the
> box" and realize there were other ways to accomplish what was being
> asked besides using a conditional. I was actually quite impressed with
> the fact that I was able to find another way without having to look too
> far into it. I'm rather curious why the jdk doesn't have a method for
> integers called returnParity(). It would have made the task much easier.

It has an operator, %, that obviates the need for such a method. It has
another, &, that also works.

BTW, no method should have "return" as part of its name. "get", perhaps.

if ( Integer.getParity( num ) == 1 )

is, in real life,

if ( num % 2 == 1 ) // if ( num & 1 == 1 )

One might argue in favor of the former, but not that it is necessary given the
latter. Many, myself included, would argue that the latter is actually preferable.

- Lew

John
Guest
Posts: n/a

 02-11-2007
Lew wrote:
> John wrote:
>
>> It took me a while to get to the point where I could think "outside
>> the box" and realize there were other ways to accomplish what was
>> being asked besides using a conditional. I was actually quite
>> impressed with the fact that I was able to find another way without
>> having to look too far into it. I'm rather curious why the jdk
>> doesn't have a method for integers called returnParity(). It would

>
>
> It has an operator, %, that obviates the need for such a method. It has
> another, &, that also works.
>
> BTW, no method should have "return" as part of its name. "get", perhaps.
>
> if ( Integer.getParity( num ) == 1 )
>
> is, in real life,
>
> if ( num % 2 == 1 ) // if ( num & 1 == 1 )
>
> One might argue in favor of the former, but not that it is necessary
> given the latter. Many, myself included, would argue that the latter is
> actually preferable.
>
> - Lew

So is there a way to test the parity of an integer using % which will
not invoke the use of a conditional such as the OP's restrictions? I
guess the real issue is the real requirements of the OP. For example,
if someone knows that by doing a %2 on an integer and having it return
0, it would be easy enough to write a statement like this
System.out.println("Parity is : " + selectedNumber % 2); But that
precludes knowledge that the recipient may NOT have. So my question to
the OP would be... when you determine if a number is odd or even, what
are you going to do with that knowledge? Are you going to use it in an
ouput, write it to a file or what? If you don't need the word "odd" or
"even" then how you determine the parity can be as simple as doing a
modulus on it and spit out the result. If you do need the words, then
you have to come up with some way of equating the result of the mod with
a word without using a conditional.

John
Guest
Posts: n/a

 02-11-2007
Lew wrote:
> John wrote:
>
>> Seriously though... something that makes me very nervous about
>> answering questions like the OP's is, how far into their study of the
>> language are they? If they are only past conditionals, are they at
>> the point where they are able to use bitwise operations? I think a
>> good idea in situations like this, besides suggesting they make some
>> effort first, is to point them in a few right directions, like bitwise
>> operations, array indexing (like my idea) but don't tell them how to
>> code it. At least that way they will be working within the boundaries
>> of what they know about and won't be afraid of getting penalized if
>> the instructor thinks they have pilfered the solution.

>
>
> Normally I would agree with you, but the OP is the one who kept demanding
>
>> then post me the solution.Mr.Beaton

>
>
> and showed less than zero willingness to put in even a nanoskootch of
> their own effort. For that they should be deprived of answers
> altogether, perhaps even ridiculed or held as objects of contempt.
>
> - Lew

Now see, here's where I disagree at least in part. I don't mind giving
suggestions and hints on the direction to take, but I don't agree with
the second part of your statement. Perhaps he did do due diligence and
only came here as a last resort. We don't know. His demanding nature
may only be due to his writing style, if his first language is not
English. Furthermore, the main reason I disagree with you (respectfully
of course) is that I would not want to be "...ridiculed or held as
objects of contempt", unless it appears absolutley obvious (and in this
case I do not believe it is) that he or she is completly unwilling to
learn the correct way of doing things. Of course we may never know
because the initial request and 3 pleas for help (that's how I read
them..so sue me ) this person has not said anything else.

Alex Hunsley
Guest
Posts: n/a

 02-12-2007
Faton Berisha wrote:
> On Feb 10, 3:15 pm, (E-Mail Removed) wrote:
>> hi,
>> any one of the group ,please solve my problem
>> "is there is any method to find given number is even
>> or odd without using if ,else ,for,while,switch,conditional

>
> First of all, you should really solve your own homework;
> this is it's purpose.

Yes!

> However, if the == operator is allowed, then the solution is simple

[snip soln]

Nooooo! You had it the right point above, then you spoiled it by....
going and doing his homework for him!

lex

Alex Hunsley
Guest
Posts: n/a

 02-12-2007
(E-Mail Removed) wrote:
> hi,
> any one of the group ,please solve my problem

This isn't the homework drop-in service. Try attempting your homework
yourself.
Hint: you can't come and get us to do your interview for you. Or your job.

> "is there is any method to find given number is even
> or odd without using if ,else ,for,while,switch,conditional

Yes actually, there is.

Gordon Beaton
Guest
Posts: n/a

 02-12-2007
On Sun, 11 Feb 2007 21:14:36 GMT, John wrote:
> So is there a way to test the parity of an integer using % which will
> not invoke the use of a conditional such as the OP's restrictions?

It's one thing to determine the parity of the number, and another to
make a decision based on that information.

For the former, none of the forbidden operations are necessary, all
that's needed is an expression. I can come up with at least one
"creative" solution for the latter, but I'm not convinced that's the
problem the OP needs to solve.

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e