Q: Write a program that will determine if a number is even. The program should ask for a number, say X and printout whether "X is even" or " X is not even". This program must use a method with the signature "public static boolean isEven(int i)".

Recommended Answers

All 6 Replies

Q: Write a program that will determine if a number is even. The program should ask for a number, say X and printout whether "X is even" or " X is not even". This program must use a method with the signature "public static boolean isEven(int i)".

OK, I wrote it. Now what?

Use the modulus operator, %. If you type modulus in google you can learn everything you need to know about it, and you'll see why you need to use it, so I won't explain further.

VernonDozier can you please tell me how to do it?

Hi
I get the feeling that you are very new at programming Java and feel totally lost?
I will not give you the code to this problem because then you do not learn anything. But I will give you some clues of what to think about.

First make a class like "public class MyCalculation" whit a main inside like
"public static void main(String[]args)"

Now think of what variables do you need outside the method " public static boolean isEven(int i) " ?

Maybe an int i that can take some kind of input maybe from System.in ?
Maybe a boolean that can take the return from the method ?

Look in Java API and think about why your method has a boolean as return.
A boolean can be either false or true. How does the computer read if it is false or true?

Inside your method "public static boolean isEven(int i)" you can calculate your inputed int i whit help of modulus operator, % as suggested before. The result of this operation is what you shall return. Observe that the returning value is expected to be a boolean while the input parameter is expected to be an int.

Think of what the result of the modulus operator is if the number is even?
Do not forget to write the return statement.

When you have got the result I suppose you want to write it out, maybe whit System.out.println(); ?

Here you can see an example of a static method to learn from : http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

Good luck =)

commented: Good first post. +21
commented: Helpful and a nice attitude about helping. Nice job bud +1

VernonDozier can you please tell me how to do it?

We don't give out the full code here. We give advice and help debug. Read posts 3 and 5. I'll give you a rough skeleton. Between it and posts 3 and 5, you'll definitely have enough to at least make an attempt.

// Place any import commands here

public class MyCalculation
{
    public static void main (String args[])
    {
        int x = 4; // change this so user is asked for a number!
        boolean xIsEven = isEven (x);
        if (xIsEven)
        {
             System.out.println ("x is even.");
        }
        else
        {
             System.out.println ("x is odd.");
        }
    }


    public static boolean isEven (int number)
    {
        return true;  // change this to test whether number is even!
    }
}

There. That's a lot of code, probably more than I should give out. Change it to your needs.

Thank you all I really appreciate it... I am new to this language and I guess this is a great place for learning and help :) Thanks again :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.