Question

java

1. Write an application that generates n random integers in the range provided the user. Use method genrate Even to generate even numbers only and generateOdd to generate odd numbers void generateOdd(int n, int r1, int r2); void generateEven(int n, int r1, int r2); Sample run: How many integers you have?5 What are your number ranges? 3 10 The generated even numbers are 6 4 8 10 6 The generated odd numbers are 37 9 35

0 0
Add a comment Improve this question Transcribed image text
Answer #1

RandomNumber java-CAUsers\ Shubham Desktop HomeworkLib1 Atom File EditView Selection Find Packages Help RandomNumberjava untitled 1 import java.util. 17 For using inbuilt Scanner class and Random class 2 class RandomNumber public static void main(String args[) main) method 6 Scanner scan- new Scanner(System.in); //Creating the object of Scanner class. 7 8 9 System.out.print(How many integers you have? );//Printing Message to the user int n -scan.nextInt I/ Taking input of the number of integers System.out.print(What are you ranges?);//Printing Message to the user int min scan.nextInt); //Taking the lower bound int max scan.nextInt(); //Taking the upper bound 10 12 System.out.print(The generated odd numbers are ) 13 generateodd(n,min, max);//Calling the generateOdd funtion 14 15 System.out.print(The generated even numbers are) 16 17 18 public static void generate0dd(int n,int r1,int r2) 19 20 21 Random random- new Random(); //Creating the Object of Random class System.out.println()1/ NextLine. generateEven(n,min,max); I/Calling the generateEven) funtion int count-1; while(count<-n) //The Loop will execute n times 23 24 25 26 27 28 int randomNum-random .nextInt((r2+1)-r1)+r1; //Generating Random Numbers between r1 and r2 both İnclusive if(randonllum%2 ! 0) //checking if the random number genrated is odd or not. stem.out.print (randomNum+)s //f the number is odd we print it and increase the counter count++ Activate Windows Go to Settings to activate Windows Sy av 17:4 CRLF UTF-8 Java O files

The Code Used:-

import java.util.*;
class RandomNumber
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("How many integers you have? ");
int n = scan.nextInt();
System.out.print("What are you ranges? ");
int min = scan.nextInt();
int max = scan.nextInt();
System.out.print("The generated odd numbers are ");
generateOdd(n,min,max);
System.out.println();
System.out.print("The generated even numbers are ");
generateEven(n,min,max);
}
public static void generateOdd(int n,int r1,int r2)
{
int count=1;
Random random = new Random();
while(count<=n)
{
int randomNum = random.nextInt((r2+1)-r1)+r1;
if(randomNum%2 != 0)
{
System.out.print(randomNum+" ");
count++;
}
}
}
public static void generateEven(int n,int r1,int r2)
{
int count=1;
Random random = new Random();
while(count<=n)
{
int randomNum = random.nextInt((r2+1)-r1)+r1;
if(randomNum%2 == 0)
{
System.out.print(randomNum+" ");
count++;
}
}
}
}

I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
java 1. Write an application that generates n random integers in the range provided the user....
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
  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write a Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

  • Java Programming - Write a program that generates two random integers, both in the range 25...

    Java Programming - Write a program that generates two random integers, both in the range 25 to 75, inclusive. Use the Math class. Print both integers and then display the positive difference between the two integers, but use a selection. Do not use the absolute value method of the Math class.

  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • A. Create a java program that generates 1000 random integers. Write the 1000 random integers to...

    A. Create a java program that generates 1000 random integers. Write the 1000 random integers to a file using the Formatter class. B. Create a second java program that opens the file with the 1000 integers using the Scanner class. Read in the integers and find and print out the largest, smallest and the average of all 1000 numbers. C. Repeat A from above but use the BufferedWriter class B. Repeat B from above but use the BufferedReader class.

  • Part II. Write a programming segment that generates 25 random integers between 0 and 99. Place...

    Part II. Write a programming segment that generates 25 random integers between 0 and 99. Place the even numbers in an array called EVEN and then place the odd numbers in an array called ODD. Print the EVEN array and Print the ODD array

  • Write   a   program in Visual C# that   generates   1000   random   numbers   with   a   user   provided   seed...

    Write   a   program in Visual C# that   generates   1000   random   numbers   with   a   user   provided   seed and upper   limit   on   the   random   numbers   generated. The numbers should be stored in a text file, each appearing on a newline, using   the   Windows   Save   File   dialog.   Each   number   should   be   separated   by   a   newline.         

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