# Java Programming Homework Question

Discussion in 'Mac Programming' started by Texas04, Oct 25, 2006.

1. ### Texas04 macrumors 6502a

Joined:
Jul 2, 2005
Location:
Texas
#1
Very confused teacher is not a very good one at school, but if you could help me with my homework that would be great
Its all just Computer Science 1K
Thank you!

The question is:
For each of the following problems write the method exactly as described and than write a valid method call either assigning the reluts to a variable or displaying the results in a print statement.

Problem:
3. Write a method called isEven that recieves and integer argument and returns boolean true if the argument is an even number, false otherwise.

I am truly confused as how to create a boolean that is equal to only even numbers... if anyone knows how to do this it would be great! A speedy response would be appreciated!

### Staff Member

Joined:
Feb 5, 2004
Location:
New Zealand
#2
You might find the % (modulus) operator useful. It gives you the remainder when you divide the number on the left of it by the number on the right.

Some examples:

Code:
```anInt = 11 % 4; //anInt becomes 3, because 3 is the remainder when you divide 11 by 4
anInt = 5 % 3; //anInt becomes 2
anInt = 6 % 2; //anInt becomes 0, because there is no remainder when dividing 6 by 2```
Now think about exactly what it means for a number to be even.

Hopefully this will be enough to put you on the right track.

3. ### Texas04 thread starter macrumors 6502a

Joined:
Jul 2, 2005
Location:
Texas
#3

Edit: So how would I write the second part where I display the results in a print statement this is what I have thus far

public boolean isEven(int a)
{
int b = a%2;
return b == 0;
}
System.out.print(isEven(a));

//But im pretty sure that doesnt work... how would i do a print statement?

4. ### desertknight macrumors newbie

Joined:
Dec 27, 2002
#4
// Returns a boolean variable
public boolean isEven(int a)
{
return ((a%2)==0);
}

//Prints result
public void isEven(int a){
if((a%2)==0)
System.out.println(a + " is even.");
else
System.out.println(a + " is odd.");
}

That should work for what you are doing.

5. ### Littleodie914 macrumors 68000

Joined:
Jun 9, 2004
Location:
Rochester, NY
#5
Hmm... @desertknight, are you sure you can do that? I know you can overload a method by creating two versions of it that take different parameters, but I didn't know you could create two of the exact same method that simply return different types of values. Perhaps I'm wrong?

And I'm pretty sure when the OP talks about making a method call that displays the results, it would be something like...

public boolean isEven(int a) {
//code
}

System.out.println("The value you input was even...? " + isEven(input));

Hope this helps!

6. ### desertknight macrumors newbie

Joined:
Dec 27, 2002
#6
Texas04 wrote "either assigning the reluts to a variable or displaying the results in a print statement".He could implement either one of the methods depending on which way he prefers.