Java Forum / General / February 2007
a problem
bharath539@gmail.com - 10 Feb 2007 14:15 GMT 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 operators,if else ladders...."
Gordon Beaton - 10 Feb 2007 14:18 GMT > 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 > operators,if else ladders...." Yes, there is. Problem solved!
/gordon
 Signature [ 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
bharath - 10 Feb 2007 16:32 GMT > > any one of the group ,please solve my problem > > "is there is any method to find given number is even [quoted text clipped - 8 lines] > [ 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 then post me the solution.Mr.Beaton
Andrew Thompson - 10 Feb 2007 17:14 GMT > > > 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 > > > operators,if else ladders...." > > > Yes, there is. Problem solved! ..
> then post me the solution.Mr.Beaton (chuckle) More effective than making demands of G.B. (or just about any other member of these groups), might be to answer the question I asked you, or at least to consider what it means, and reply to it.
After all, we are having a discussion, are we not?
Andrew T.
Gordon Beaton - 10 Feb 2007 18:17 GMT > then post me the solution.Mr.Beaton I already answered the question you asked.
However if you want to know *how* to determine the parity of a number without using the mechanisms you mentioned earlier, then I'll give you a hint: how would *you* (not a computer) decide whether a given number was even or odd?
/gordon
 Signature [ 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
Andrew Thompson - 10 Feb 2007 14:27 GMT On Feb 11, 1:15 am, bharath...@gmail.com wrote:
> hi, > any one of the group ,please solve my .. ..homework? What good would that do you?
Andrew T.
bharath - 10 Feb 2007 16:30 GMT On Feb 10, 7:15 pm, bharath...@gmail.com 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 > operators,if else ladders...." Mr.Beaton,please send me the solution.
Jeff - 10 Feb 2007 22:37 GMT > On Feb 10, 7:15 pm, bharath...@gmail.com wrote: > [quoted text clipped - 5 lines] > > Mr.Beaton,please send me the solution. 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.
Chris Uppal - 11 Feb 2007 00:47 GMT > 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 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
John - 11 Feb 2007 02:59 GMT > I would use a bitwise operator. But that's just me. It's not my > assignment, and I always hate assignments that artificially limit [quoted text clipped - 3 lines] > 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...
Gordon Beaton - 11 Feb 2007 09:07 GMT > 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
 Signature [ 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
Lew - 11 Feb 2007 04:45 GMT > Mr.Beaton,please send me the solution. I'm afraid it doesn't work that way.
- Lew
Flo 'Irian' Schaetz - 10 Feb 2007 20:00 GMT And thus spoke bharath539@gmail.com...
> 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 > operators,if else ladders...." "==" is allowed? Then it's easy to do.
Flo
Faton Berisha - 11 Feb 2007 10:20 GMT On Feb 10, 3:15 pm, bharath...@gmail.com 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 > operators,if else ladders...." 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; boolean[] answer = {true,false}; System.out.println(answer[n % 2]); // System.out.println(answer[n & 1]);
Faton Berisha
Andrew Thompson - 11 Feb 2007 10:48 GMT > On Feb 10, 3:15 pm, bharath...@gmail.com wrote: > [quoted text clipped - 6 lines] > First of all, you should really solve your own homework; > this is it's purpose. Why should the OP do that? When they can instead.. - get people like you to do it. - get a job at your firm and steal your code. - get a job as your boss.
Andrew T.
Lew - 11 Feb 2007 17:51 GMT bharath...@gmail.com 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 >>> operators,if else ladders...." "Faton Berisha" wrote:
>> First of all, you should really solve your own homework; >> this is it's purpose.
> Why should the OP do that? > When they can instead.. > - 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
Faton Berisha - 13 Feb 2007 04:12 GMT > > First of all, you should really solve your own homework; > > this is it's purpose. [quoted text clipped - 6 lines] > > Andrew T. I am afraid I (respectfully) disagree. The purpose of the newsgroup is helping the ones who ask for help. Hints are, of course, very useful for learning, but if a poster insists that he/she needs the solution instead, keeping with unusable posts might be perceived as cynical. Simply ignoring the message (or indeed the entire thread) is, I think, a better choice. After all, this is what I quite often do myself; at the very moment, I believe, I had some time to spare.
Faton Berisha
Lew - 13 Feb 2007 04:48 GMT >>> First of all, you should really solve your own homework; >>> this is it's purpose. "Andrew Thompson" wrote:
>> Why should the OP do that? >> When they can instead.. >> - get people like you to do it. >> - get a job at your firm and steal your code. >> - get a job as your boss.
> I am afraid I (respectfully) disagree. With which statement are you disagreeing? That people who don't do their own homework are likely to continue to rely on others' efforts throughout their careers? That such people are dishonest? That coddling them tends to enable their behavior? That one might have the experience of working with such a person by enabling such behavior? That that experience would be unpleasant? That it would be poetic justice? That you were enabling such behavior? That you were culpable for doing so?
These assertions were gleaned from Andrew's compact message. I may have missed a few. All are arguable and debatable.
FWIW, probably less than .2 €, I'd agree that homework laziness is bad. You might have been enabling, but I don't know. You are not culpable if you were. You obviously thought about what you were doing and I respect that.
For the OP and others with "homework-sounding" questions, the oft-repeated advice for Usenet participation is "Make an effort!", and describe that effort in the request for feedback. Show or tell the specific approach and detailed difficulties.
Too many times people pose academic-style questions with demands for whole solutions in terse, haughty-sounding messages - no mention of prior effort or specific problems. Too many times, these people react in aggrieved tones to the "do your own [home]work" response chorus, rather than shifting to more appropriate modes. Just take the heat in good nature and provide details; all will instantly be forgiven and the larks will sing.
So many, like Faton, are so eager to help; it behooves seekers to show respect for that voluntary commitment.
- Lew
nukleus - 13 Feb 2007 11:06 GMT >>>> First of all, you should really solve your own homework; >>>> this is it's purpose. [quoted text clipped - 37 lines] >So many, like Faton, are so eager to help; it behooves seekers to show respect >for that voluntary commitment. So, what is YOUR problem, sire, if someone is willing to help others for whatever reason he thinks?
You see, it is YOUR rigid mind that has ALL sorts of prejudices, thinking that the entire planet Earth is to turn around its axis according to HIS command.
What does this model remind you, oh wisest one of all the wisest?
>- Lew Oliver Wong - 13 Feb 2007 15:55 GMT [On the topic of doing other people's homework for them. nukleus seems to be addressing Lew's message in particular]
> So, what is YOUR problem, sire, > if someone is willing to help others > for whatever reason he thinks? Sometimes, a person's intentions are good, but the effects of their actions are bad. I'm guessing Lew considers doing someone else's homework for them to be an example of such a scenario (and I don't disagree with this). The idea is that while you may have good intentions, by doing another person's homework for them, you are likely causing more harm than good in the long term. Lew (and others) are thus pointing out the problem with this approach so that those who are truly trying to help others will realize that doing the homework is not the best approach for helping.
- Oliver
Arne Vajhøj - 14 Feb 2007 01:35 GMT > [On the topic of doing other people's homework for them. nukleus seems to be > addressing Lew's message in particular] [quoted text clipped - 10 lines] > approach so that those who are truly trying to help others will realize that > doing the homework is not the best approach for helping. I don't think that argument holds water.
There were already several posts mentioning homework.
So it should be obvious from both the question and and the previous posts that it was likely home work.
If someone knowing that decide to answer anyway, then it must be their decision.
What people want to help with in news groups must be their decision based on their view of the world.
I consider it inappropriate for someone to try an impose their own answering norms on others.
Arne
Oliver Wong - 14 Feb 2007 14:31 GMT >> [On the topic of doing other people's homework for them. nukleus seems to >> be addressing Lew's message in particular] [quoted text clipped - 17 lines] > So it should be obvious from both the question and and the previous > posts that it was likely home work. Right, but a person can know that this post is homework, and yet not know why it might not be a good idea to do someone else's homework. I wasn't focusing on Lew (and others) pointing out that this is homework, but rather on Lew (and others) pointing out why it might not be a good idea to do other people's homework for them.
> If someone knowing that decide to answer anyway, then it must > be their decision. [quoted text clipped - 4 lines] > I consider it inappropriate for someone to try an impose > their own answering norms on others. I didn't see this thread having much "imposition" (is that the correct noun form of "impose"?), but rather it's a discussion just like any other (though slightly off topic). Instead of people saying "I think getters and setters are good" and others saying "I think getters and setters are bad", it's "I think doing other people's homework is acceptable" and "I think doing other people's homework is unacceptable". It's off topic in that we're not discussing Java, but ethics, but it wouldn't be the first time a thread drifted off topic in CLJP.
Personally, I take a pragmatic view on the matter. I don't like to do other people's homework for them, but I won't chide others who decide to do that, as I don't think it'll do much good anyway: There are too many avenues for cheating available on the internet. There are sites (e.g. RentACoder) whose business model revolves primarily around doing other people's homework for them.
- Oliver
Giles - 19 Feb 2007 04:30 GMT > In article <2OqdnQ-q2e6e3kzYnZ2dnUVZ_vShn...@comcast.com>, Lew > [quoted text clipped - 55 lines] > > >- Lew Your mother's diseased c.nt?
nukleus - 19 Feb 2007 07:46 GMT >> In article <2OqdnQ-q2e6e3kzYnZ2dnUVZ_vShn...@comcast.com>, Lew >> [quoted text clipped - 74 lines] > >Your mother's diseased c.nt? Oh, this dong beatle again?
Digging up zome old shite from someone elses a.s?
Did you get the magnifying glass, suxy?
You forgot to post the headers, dummy. Especially this part: X-Complaints-To: newsmaster@ukr.net
Zo...
What da funk is yerr royal problem, suckazoid?
There isn't enough of top grade reading to crack that empty scull of yours?
You need to dig deeper, yerr royal suckiness.
The best sh.t is hidden deep inside the dead rat's arse, you know...
May be you should get yerself a brand new microscope?
Alex Hunsley - 12 Feb 2007 01:47 GMT > On Feb 10, 3:15 pm, bharath...@gmail.com wrote: >> hi, [quoted text clipped - 5 lines] > 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]
[slaps head]
Nooooo! You had it the right point above, then you spoiled it by.... going and doing his homework for him!
lex
susauparna@gmail.com - 11 Feb 2007 13:25 GMT On Feb 10, 7:15 pm, bharath...@gmail.com 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 > operators,if else ladders...." 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
Lew - 11 Feb 2007 17:52 GMT bharath...@gmail.com 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 >> operators,if else ladders...."
> 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
John - 11 Feb 2007 19:02 GMT > bharath...@gmail.com wrote: > [quoted text clipped - 16 lines] > > - 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 :-)
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.
My 2 cents FWIW.
Lew - 11 Feb 2007 19:34 GMT > 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 [quoted text clipped - 6 lines] > 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
John - 11 Feb 2007 21:19 GMT >> Seriously though... something that makes me very nervous about >> answering questions like the OP's is, how far into their study of the [quoted text clipped - 16 lines] > > - 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.
John - 11 Feb 2007 13:59 GMT > 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 > operators,if else ladders...." 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, so when you asked your question, I simply did a google searh on "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
http://groups.google.ca/group/comp.lang.java.help/browse_thread/thread/56fa67445 42154f9/f3a348bfaba195c6?hl=en#f3a348bfaba195c6
I would suggest that you read this, adopt it at your creedo, memorize 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 - 11 Feb 2007 17:05 GMT [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]
> 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
John - 11 Feb 2007 18:55 GMT > [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 [quoted text clipped - 9 lines] > > -- 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 integers called returnParity(). It would have made the task much easier.
Lew - 11 Feb 2007 19:43 GMT > 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 - 11 Feb 2007 21:14 GMT >> 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 [quoted text clipped - 20 lines] > > - 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.
Gordon Beaton - 12 Feb 2007 07:32 GMT > 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
 Signature [ 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
John - 11 Feb 2007 19:03 GMT > [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 [quoted text clipped - 9 lines] > > -- chris BTW - your reply showed up on my server. I was just wondering the purpose for myself..
blmblm@myrealbox.com - 12 Feb 2007 16:28 GMT > [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 [quoted text clipped - 7 lines] > boolean-valued expressions, as something more general than the XXX that goes in > an if (XXX) test. That sounds very possible. My experience teaching second-semester programming in Java suggests that students often don't really "get" Boolean expressions as expressions (i.e., as expressions similar to arithmetic expressions, but evaluating to true/false rather than to something numeric). I'm not sure how to explain this except by example. It seems not to occur to them that the following code (untested):
static boolean isEven(int x) { if ((x % 2) == 0) return true; else return false; }
could be replaced by
static boolean isEven(int x) { return ((x % 2) == 0); }
and they seem a little uncomfortable with the second version even when it's suggested to them. Forcing them to come up with something like the second version -- yeah, that might have pedagogical value.
 Signature B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.
Alex Hunsley - 12 Feb 2007 01:48 GMT > 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 > operators,if else ladders...." Yes actually, there is.
Oliver Wong - 12 Feb 2007 17:35 GMT > Hint: you can't come and get us to do your interview for you. Or your job. But see http://thedailywtf.com/Articles/Interview_by_Proxy.aspx
- Oliver
Proton Projects - Moin - 13 Feb 2007 09:16 GMT On Feb 10, 7:15 pm, bharath...@gmail.com 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 > operators,if else ladders...." Hi Bharath,
Hope this program will help u
public class OddEven { public static void main(String[] args) { String[] array = new String[]{"No","Yes"}; int one = 1; int two = Integer.parseInt(args[0]); int result = one&two; System.out.println("Is the given number is odd, the result is " +array[result]); } }
Regards Moin
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|