Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Texas04

macrumors 6502a
Original poster
Jul 2, 2005
886
1
Texas
:confused: :confused: 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!
 

HexMonkey

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
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.
 

Texas04

macrumors 6502a
Original poster
Jul 2, 2005
886
1
Texas
Dude Your Amazing


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?
 

desertknight

macrumors newbie
Dec 27, 2002
6
0
// 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.
 

Littleodie914

macrumors 68000
Jun 9, 2004
1,813
8
Rochester, NY
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? :confused:

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!
 

desertknight

macrumors newbie
Dec 27, 2002
6
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.