Write a program with a method named isEven that accepts an int argument. The met
ID: 3538522 • Letter: W
Question
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%u2019s odd. The program%u2019s 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.
You can use the following logic to determine whether a number is even or odd:
Testing the number of Odd and Even in a list of 100 randomly generated values in the range of 1 %u2013 1000.
Even%u2019s: 57
Odds: 43
{
// 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.
Here is what I have, but it does not run the inner loop 10 time like it shouls and I can not figure our why?
public class OddOrEven1 {
public static void main (String [] args){
int r = 0;
int k = 0;
int iseven = 0;
int isodd =0;
System.out.println("iterations" + " even" + " odd");
//loop to run 10 iterations
for (r = 1; r <= 10; r++){
//loop to generate random numbers
for (int j = 1; j <= 10; j++){
int n = (int)(Math.random() * 1000 + 1);
int even = isEven(n);
if (even == 1){
iseven++;
}
else
isodd++;
}
System.out.println(r + " " + iseven + " " + isodd);
}
}
public static int isEven(int number){
if ((number % 2) == 0)
return 1;
else
return 2;
}
}
It starts to run the loop once then goes twice....it needs to run 10 times every loop and then sort out the odd and even.
Testing the number of Odd and Even in a list of 100 randomly generated values in the range of 1 %u2013 1000.
Even%u2019s: 57
Odds: 43
Explanation / Answer
You are written the program will. I have did minor changes on you program to work properly.
I have give the comment on the program.
Please test it.
public class OddOrEven {
public static void main (String [] args){
int r = 0;
int k = 0;
int iseven = 0;
int isodd =0;
System.out.println("iterations" + " even" + " odd");
//loop to run 10 iterations
for (r = 1; r <= 10; r++){
//loop to generate random numbers
for (int j = 1; j <= 100; j++){
int n = (int)(Math.random() * 1000 + 1);
boolean even = isEven(n);
if (even){
iseven++;
}
else{
isodd++;
}
}
System.out.println(r + " " + iseven + " " + isodd);
//You have missed this
//This will reset the even and odd count for next iteration.
//I think this is the problem.
//remain fine.
iseven = 0;
isodd = 0;
}
}
public static boolean isEven(int number){
return number % 2 == 0;
}
}
Output :
iterations even odd
1 58 42
2 42 58
3 51 49
4 44 56
5 57 43
6 49 51
7 48 52
8 59 41
9 45 55
10 55 45
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.