File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having problem with Even/odd program

 
Abdulmajeed Alroumi
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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:


 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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)
 
Greg Ferguson
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston - Thanks, I deleted the unnecessary part.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, the program has been compiled successfully
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic