• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

isODD method: Should be returning a boolean and require an Int number - what's wrong?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,

I am working on a basic challenge wich requires a Boolean Method in Java.
The code looks like this:


This should be used here - in the main method. Take a look:




Question:
It shows unexpecxted data value. I can agree with JVM in that, but in this case how could I send an input INT data type into this method?

How can I make it workable? I know there are other "methods" of this method - but I am curious the explanation of this.

Any thoughts?
isODD method
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never use '== true' or '== false' to test a boolean state. If you mistakenly use '=' then you introduce a bug. Use
If you want to test for it being 'false' then use '!'. As in
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a question for you to think about your isOdd method.
Are all odd numbers less than zero?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Afraid there is more you can do to improve that method. Going back to the old old Sun Style Guide, we see it says not to write if (something) return true; else return false; So we can reduce your method to this:-Note that I haven't removed the redundant () and I haven't corrected any errors.

There is a quicker way to do thatThat may look too scary for the beginning forum, but it tests whether the last bit is 1 or 0. You can get that sort of thing to work for powers of 2: 1, 2, 4, 8, 16, etc., but not other numbers. You can also shorten your second code block:-
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

Some brain food.

Please explain why you did not have such version?

Would that work same as?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is a quicker way to do thatThat may look too scary for the beginning forum, but it tests whether the last bit is 1 or 0.


It may be quicker but it's also arguably more cryptic. Prefer clear code to clever code.

More people understand using the % operator when checking for odd or even. Even I had to do a double take when I saw the & operator even though I have seen that used in the past. It's just not something you expect to see, so the Principle of Least Astonishment (POLA) is definitely tested by that little variance.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:It's just not something you expect to see, so the Principle of Least Astonishment...


If you read Java Puzzlers (Campbell suggested to me some time ago as for a good read to learn better practices) you'll figure out you get over astonished sooner than later having said that, I learned ... & 1 from there. Agree that 9 out of 10 programmers probably never will figure out in their career about such thing.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . arguably more cryptic. . . .

So it is too scary for this forum.
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys!

Thank you very much

This



or this



for me a surpise and this kind of tricks and suggestions I looked after. So I found my ranch, the exact page what I need




This was a task and there was asking to be larger than 0. But just without that the code is working - so thank you very much!


Thank you!
1_original.jpg
[Thumbnail for 1_original.jpg]
:)
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Barbara Fischer wrote:Hey guys!

Thank you for the cow (←link) which I presume is to be shared between us all

. . . to be larger than 0. . . . Thank you!

But your first post had less than 0 in.
 
reply
    Bookmark Topic Watch Topic
  • New Topic