In: Computer Science
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code.
import java.util.*;
public class OddManOut {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many random Integers to produce?");
int num = sc.nextInt();
ArrayList<Integer> randomInts = createRandomList(num);
System.out.println("The random list is: ");
System.out.println(randomInts);
removeOdds( randomInts );
System.out.println("The random list with only even numbers:
");
System.out.println(randomInts);
}
public static ArrayList<Integer> createRandomList(int
num)
{
... YOUR CODE HERE ...
}
public static void removeOdds(ArrayList<Integer> list)
{
... YOUR CODE HERE ...
}
}
The program first asks you how many random numbers to produce (variable num). The program then calls createRandomList() which returns an ArrayList of Integers that contain random numbers between 1 and 20. The number of random numbers to produce depends on the variable num (i.e: if the user enters 50 then this method returns an ArrayList of Integers that contain 50 random numbers between 1 and 20). Once this list is displayed, the program calls removeOdds() which removes all numbers from the list that are odd leaving only the even numbers. Finally the program displays the modified list showing only even numbers.
Example output shown below:
How many random Integers to produce?
31
The random list is:
[6, 19, 8, 12, 13, 4, 12, 7, 16, 7, 21, 3, 16, 4, 17, 2, 4, 14, 2, 1, 3, 5, 7, 18, 17, 13, 10, 20, 8, 18, 20]
The random list with only even numbers:
[6, 8, 12, 4, 12, 16, 16, 4, 2, 4, 14, 2, 18, 10, 20, 8, 18, 20]
DELIVERABLES:
Upload your program below for marking. Note, for full marks you MUST NOT modify the starting code (code in red) in any way. Your task is only to fill in the "...your code here..." part of the two methods.
Source Code:
Output:
Code in text format (See above images of code for indentation):
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
/*class definition*/
public class OddManOut
{
/*main method*/
public static void main(String[] args)
{
/*Scanner object to read input from the user*/
Scanner sc=new Scanner(System.in);
/*read num from user*/
System.out.println("How many random Integers to produce?");
int num=sc.nextInt();
/*method call to creaete a arraylist*/
ArrayList<Integer> randomInts = createRandomList(num);
/*print random array list*/
System.out.println("The random list is: ");
System.out.println(randomInts);
/*method call to remove odd numbers*/
removeOdds(randomInts);
/*print arraylist after remove odd numbers*/
System.out.println("The random list with only even numbers: ");
System.out.println(randomInts);
}
/*method definition*/
public static ArrayList<Integer> createRandomList(int num)
{
int i,n;
/*declare an array list*/
ArrayList<Integer> rnums=new ArrayList<Integer>();
for(i=0;i<num;i++)
{
/*generate random number and add to arraylist*/
n=ThreadLocalRandom.current().nextInt(1,20);
rnums.add(n);
}
/*return arraylist*/
return rnums;
}
/*method definition to remove odd numbers*/
public static void removeOdds(ArrayList<Integer> list)
{
/*remove odd numbers*/
list.removeIf(i->i%2!=0);
}
}