Question

Java Programming Write a method named isEven that accepts an integer argument. The method should return...

Java Programming

Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the result. After the loop has completed, the total number of even and odd integers should be displayed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;
import java.util.Scanner;

public class RandomEvenCheck {
  public static boolean isEven(int n){
    return n%2 == 0;
  }
  
  public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    System.out.print("How many random numbers? ");
    int n = scanner.nextInt();
    
    int evenCount = 0, oddCount = 0;
    Random random = new Random();
    
    int var;
    for(int i = 0;i<n;i++){
      var = random.nextInt(n);
      if(isEven(var)) {
        System.out.println(var + "is even");
        evenCount++;
      }
      else{
        System.out.println(var + "is odd");
        oddCount++;
      }
    }
    System.out.println("Number of even numbers generated: "+evenCount);
    System.out.println("Number of odd numbers generated: "+oddCount);
  }
}

Please up vote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Java Programming Write a method named isEven that accepts an integer argument. The method should return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • For Python debugExercise12.py is a recursive function that accepts an integer argument, n, and prints the...

    For Python debugExercise12.py is a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. By debugging these programs, you can gain expertise in program logic in general and the Python programming language in particular. def main(): # Local variable number = 0 # Get number as input from the user. number = int(input('How many numbers to display? ')) # Display the numbers. print_num(number) # The print_num function is a a recursive function #...

  • Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum...

    Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program. The program should be called SumOfNumbers.java.

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even...

    In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even digits with zero and returns a new integer. The new integer could have less digits. If the given integer is not a 4-digit integer or it is a negative integer the method returns zero. Examples: changeEvens(1000)-> 1000 changeEvens(-1000)-> 0 changeEvens(8764)-> 700 changeEvens(5829)-> 5009 changeEvens(1012)-> 1010

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • I am supposed to a pseudocode function named max that accepts two integer values as arguments...

    I am supposed to a pseudocode function named max that accepts two integer values as arguments and returns the value that is greater of the two. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is greater of the two. Is everything correct and complete? //Pseudocode //This function takes two integers as parameters: x and y Begin : Max(x,y) If(x>y) then MAX=x Else MAX=y End if Return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT