13 Replies - 3196 Views - Last Post: 02 August 2013 - 08:48 AM Rate Topic: -----

#1 Nick_C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-July 13

Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 11:52 AM

Hopefully I am doing this post right, I am having problems counting the even's and odd's in this code. I have tried a method (which I have taken out of this code) but I can't get that to work. Thanks!



import java.util.Random;

public class EvenOdd {

public static void main(String[] args) {

Random number = new Random();


System.out.println("Testing the number of Odd and Even in a list of 100" + "\n" + "randomly generated values in the range of 1-1000.");

for (int i = 0; i < 100; i++) {
System.out.println();
}

int countEven = 0;
int countOdd = 0;

if ((number % 2) == 0){
countEven++;
}
else {
countOdd++;
}

System.out.println("Even's: " + countEven);
System.out.println("Odd's: " + countOdd);
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Counting Even and Odd numbers from a random list of numbers

#2 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 153
  • Joined: 19-July 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 12:06 PM

Hey, welcome to the forums :) To use the code tags you need the beginning code tag (the one without the /) in front of the code and the other behind the code.
Now, what you are creating is a random object. This object can be used to generate random numbers so trying to divide this object would not work. Instead you should create an array and cycle through it assigning random integers to each slot in the array. You could even have a random array length if you wanted. It would look something like this:
int [] integers = new int [number.nextInt()];
for(int i = 0; i < integers.length; i++)
{
   integers[i] = numbers.nextInt();
   //to make the code more concise you could even do the counting in this loop
   if(integers[i] % 2 == 0)
     countEven++;
   else
     countOdd++;
}
//your even and odd count printing
//if you wanted you could make another loop to print out the string of numbers by looping through the array and printing each number followed by a comma, for example


Hope this helps :)

Btw, if you want the random object to only generate numbers within a certain range use random.nextInt(someInt). The generated number would be between 0 and the number, including 0 and excluding someInt.

This post has been edited by StrongJoshua: 01 August 2013 - 12:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 Nick_C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-July 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 12:17 PM

Yes that makes a lot of sense, thank you very much! I just looked my assignment again and I need to do a method named isEven that accepts an int argument. Here is what I have for that, which, of course doesn't work.

//I don't know what to use for the parameters 
public static int isEven (int ????){

if((number % 2) == 0) {
	
	}
	return even++;
	
else if((number % 2) != 0)
	}
	return odd++; 

System.out.println("Even's: " + even);
System.out.println("Odd's: " + odd);


Was This Post Helpful? 0
  • +
  • -

#4 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 03:20 PM

Are you sure you're supposed to use a method that returns an integer instead of a boolean? Usually something that sounds like isEven or isOdd sounds like it would be a boolean.

i.e.

boolean isEven(int num) {
    if((num % 2) == 0)
        return true;
    return false;
}



Then you'd cycle through the array and use,

for(int i = 0; i < array.length; i++) {
    if(isEven(array[i]))
        evenCount++;
    else
        oddCount++;
}


Was This Post Helpful? 1
  • +
  • -

#5 Nick_C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-July 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 09:19 PM

I know this forum isn't for doing homework but here is my assignment as arrays and the boolean wasn't specified. Thanks for the reply, I can't even get a response from her to help most of the time.

Part 1: Odd or Even
Write a program with a method named isEven that accepts an int argument. The method should return true if the argument is even, or false if itís odd. The programís main method should use a loop to generate 100 random integers in the range of 1 - 1000. It should use the isEven method to determine whether each random number is even, or odd. When the loop is finished, the program should display the number of even numbers that were generated, and the number of odd numbers.

Testing the number of Odd and Even in a list of 100 randomly generated values in the range of 1 Ė 1000.

Evenís: 57

Odds: 43


You can use the following logic to determine whether a number is even or odd:

if ((number % 2) == 0)
{
// The number is even.
}
else
{
// The number is odd.
}


Once you have your program working, I want you to run it 10 times in a loop and display the output for each loop along with the totals for all 10 iterations.
Your output should closely resemble: (i.e. this is the level of detail I want to see. Yours does not have to look exactly like this as long as it should the results of each iteration AND the totals.
Was This Post Helpful? 0
  • +
  • -

#6 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 01 August 2013 - 09:32 PM

View PostNick_C, on 02 August 2013 - 04:19 AM, said:

Part 1: Odd or Even
Write a program with a method named isEven that accepts an int argument. The method should return true if the argument is even, or false if itís odd.



a Boolean will return true or false. an integer won't :)
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1933
  • View blog
  • Posts: 4,015
  • Joined: 11-December 07

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 05:59 AM

Schutzzz, let me rewrite your method slightly differently:

boolean isEven(int num) {
    boolean answer = ((num % 2) == 0);
    if(answer)
        return true;
    return false;
}


The if basically says "If true then return true; it false return false."
Why not just return the value of answer, or even this:

boolean isEven(int num) {
    return ((num % 2) == 0);
}

Was This Post Helpful? 1
  • +
  • -

#8 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 06:52 AM

View Postcfoley, on 02 August 2013 - 12:59 PM, said:

The if basically says "If true then return true; it false return false."


Good point. Hadn't entirely thought that one through as I was typing it.
Was This Post Helpful? 0
  • +
  • -

#9 Nick_C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-July 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 07:27 AM

Thanks everyone for the replies! I will work on it today.

Nick
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1359
  • View blog
  • Posts: 2,995
  • Joined: 05-April 11

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:28 AM

You don't even need an else
int count = 100;
int even, odd;

for	(int i = 0; i < count; i++) {
	if (i % 2 == 0)
		even++;
}

odd = count - even;


Was This Post Helpful? 3
  • +
  • -

#11 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1933
  • View blog
  • Posts: 4,015
  • Joined: 11-December 07

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:35 AM

You don't even need an if:

for(int i : array) {
  odd += (i % 2);
}
even = array.length - odd;

Was This Post Helpful? 2
  • +
  • -

#12 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 135
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:39 AM

View PostCasiOo, on 02 August 2013 - 03:28 PM, said:

You don't even need an else
int count = 100;
int even, odd;

for	(int i = 0; i < count; i++) {
	if (i % 2 == 0)
		even++;
}

odd = count - even;



on the other hand, he mentioned needing a Boolean
Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1933
  • View blog
  • Posts: 4,015
  • Joined: 11-December 07

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:46 AM

Yes, a method, isEven is even is a much better way to do it.

This post has been edited by cfoley: 02 August 2013 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1359
  • View blog
  • Posts: 2,995
  • Joined: 05-April 11

Re: Counting Even and Odd numbers from a random list of numbers

Posted 02 August 2013 - 08:48 AM

nvm

This post has been edited by CasiOo: 02 August 2013 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1