• 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

Having problem with Even/odd program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there everyone,
I hope everyone is doing great.

I have tried to create isEven program using the .Operator but I kept on having problems with it. Can you guys please check it and provide with an explanation if possible.
Thanks,

Here is the program:


 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Can you guys please check it and provide with an explanation if possible.

far better for you to explain:
the problem/s you're having
the result you expect
the result you get
 
Abdulmajeed Alroumi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not providing you with enough information

Okay, I have tried with this program many ways, each time I have to initialize x with a number and then it decides if it's odd or even which is I am not looking for. The output I am looking for is when I typed down a number it shows if it's odd or even. So, I said let's give it a though and try public boolean isEven(int num){}; method. It showed me two errors, one x has to be initialize with a number. Two this method must return a result of type boolean.

Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> It showed me two errors, one x has to be initialize with a number.

so, try doing what the message says

> Two this method must return a result of type boolean.

OK, this is your method
public boolean isEven(int num){return};

what do you think you should test?
and how would you return the result?
 
Abdulmajeed Alroumi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The this is that if I assign x with number, it will not work as the way I wanted...I am asking if there is a way to compile the program without assigning x with a number...because what I am trying to do is that if I need to have the number check it first if it's odd or even...but it will not work in this way if I assigned x with a number...as for the boolean...from my understanding you can only use return once in a method...and what I am trying to return to see the value if it's true of false
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I am asking if there is a way to compile the program without assigning x with a number

you could have x as a class field, but it will have a default value of 0, so, give it 0.
if you want to give x a value during the program, it will change from 0.

> from my understanding you can only use return once in a method

you can only return once, but you can determine 'when' it is returned
e.g


in the method, don't have the test as an if-else (try it and see what happens)
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abdulmajeed,
Learn how to use the modulus "%" operator and you will easily be able to determine whether a number is even or odd.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Buenny - Just a style tip: You don't need to include an entire quote (especially containing code) for a 1-line reply.

Winston
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston - Thanks, I deleted the unnecessary part.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdulmajeed Alroumi wrote:The this is that if I assign x with number, it will not work as the way I wanted...I am asking if there is a way to compile the program without assigning x with a number


You have to assign a value to x but there are a number of ways of doing this.
You can assign it a literal value
You can pass a value as a command line argument and convert it to an int
You can ask the user to type a value and then read it in. You use System.out.println() to print a message and then Scanner or an InputStream attached to System.in to read the value. Do a Google search for java i/o tutorial

You can write a simple GUI application that allows the user to enter a number. Do a Google search for java swing tutorial if you want to try this.
 
Abdulmajeed Alroumi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, the program has been compiled successfully
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic