I know this is probably seem to be very easy for you guys. I have been trying to figure this out for three hours. The requirement ask me to write a method determine whether an integer is even or not. If it is true, return true. If it is false, return false.

here is my code, I think I did it right.

public class IsEven
{
	public boolean evenNumber( int x)
	{
		int even = x;
		
		if ( even%2 == 0)
		
		return true;
		
		else
		
		return false;

	}

}

Then incorporate the method into an application that inputs some random integer numbers(a sequence of number) and determines whether it is odd or even. So I know I need to use method call here and generate the random by using java.util. But for some reason I stuck on method call and pass random numbers I general to the method I wrote.

here is the code I wrote so far.

import java.util.Random;

public class DetermineEven
{
	public static void main(String[] args)
	{
		Random randomNumber = new Random();
		IsEven evenNumber = new IsEven();
		
		
		for (int counter=0; counter<20; counter++)	
		{

			int x = 1+ randomNumber.nextInt(10);
			IsEven.evenNumber();
			System.out.println(even);
			
		}
	}

}

Thank you so much.

Recommended Answers

All 2 Replies

You almost there

for (int counter=0; counter<20; counter++)	
		{

			int x = 1+ randomNumber.nextInt(10);
			if(even.evenNumber(x))
			  System.out.println("Number is even");
			else
			  System.out.println("Number is odd");
			
		}

Thank you so much. Just another quick question, I already read the book. But I still don't quite understand when do I need to use static. When I put static in IsEven method, I can just write IsEven.evenNumber(); to call the method. But without the static, I need to write IsEven i = new IsEven(); then call the method with i.evenNumber(); Do you might to explain to me or show me some links that explain better than my book? Thank you so much again.

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.