Velocity Reviews > Java > a problem

# a problem

John
Guest
Posts: n/a

 02-11-2007
Jeff wrote:

>
>
> I would use a bitwise operator. But that's just me. It's not my
> assignment, and I always hate assignments that artificially limit
> choices - if teaching Java, why give an assignment that makes it so
> you can't use normal Java constructs? I teach in a medical school and
> this would be like asking how a student would diagnose pneumonia
> without xray or stethoscope, and they can only ask two questions...
> Useless.
>

If you were to use the result of modulus as an index to an array of
"state".... well that's what I tried... it ain't pretty...

Lew
Guest
Posts: n/a

 02-11-2007
bharath wrote:
> Mr.Beaton,please send me the solution.

I'm afraid it doesn't work that way.

- Lew

Gordon Beaton
Guest
Posts: n/a

 02-11-2007
On Sun, 11 Feb 2007 02:59:52 GMT, John wrote:
> If you were to use the result of modulus as an index to an array of
> "state".... well that's what I tried... it ain't pretty...

The result of the modulus only needs to be compared with one of two
possible values, and it's about as straightforward as it gets.

/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

Faton Berisha
Guest
Posts: n/a

 02-11-2007
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.

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

System.out.println(n % 2 == 0);

or, a fancier one,

System.out.println((n & 1) == 0);

In the other case, i.e., if == is not allowed,
it becomes more cumbersome:

int n = 15;

Faton Berisha

Andrew Thompson
Guest
Posts: n/a

 02-11-2007
On Feb 11, 9:20 pm, "Faton Berisha" <(E-Mail Removed)> 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.

Why should the OP do that?
- get people like you to do it.
- get a job as your boss.

Andrew T.

susauparna@gmail.com
Guest
Posts: n/a

 02-11-2007
On Feb 10, 7: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

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

John
Guest
Posts: n/a

 02-11-2007
http://www.velocityreviews.com/forums/(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
>

As you can probably tell, there are a multitude of ways to address your
problem. If you walk slowly through the various responses to your
initial posting, I am sure you will find something that will help you.
However, I think that most people will be willing to help if you can
show that you have done some initial research on your own. Tell us what
you have tried, show us code and output. I'm not exactly experience in
"java method even odd" and looked at what google told me.

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". Unfortunantly, we don't know what your level of knowledge is,
so if we were to prescribe a solution that makes use of bitwise
operators (beyond me right now) and you present it as your solution,
your professor might penalize you thinking that you have cheated in some
way. So perhaps you can tell us briefly what you know about in Java so
that if we can help you, we won't get you in trouble

However, if you are so lazy that you want someone else to do your
homework for you, then you are asking in the wrong forum. People here
do not take kindly to posters who ask for them to do their homework
assignments, it kind of defeats the purpose of learning on your own.

Finally, there is a wonderful web page/usenet posting

it, sleep with it under your pillow, what ever it takes to make it your
modus operendi when posting. It will make your experience here much
more enjoyable.

Chris Uppal
Guest
Posts: n/a

 02-11-2007
[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

Lew
Guest
Posts: n/a

 02-11-2007
(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

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

Andrew Thompson wrote:
> Why should the OP do that?
> - get people like you to do it.
> - get a job at your firm and steal your code.
> - get a job as your boss.

I think someone like the OP has done that last to me a number of times.

- Lew

Lew
Guest
Posts: n/a

 02-11-2007
(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

(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