Question

In: Computer Science

Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...

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.

Solutions

Expert Solution

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);

    }

}


Related Solutions

Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste...
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste the whole solution I was left with. Thank you in advance ! Lab 5 – Class Statistics Write a program which will uses the file Lab5Data.txt containing student names and the points they had earned at the end of the class. The program should use one or more arrays to create a report with the following information: -A table containing the student name, total...
R studio questions Write up your answers and paste the R code Copy and paste all...
R studio questions Write up your answers and paste the R code Copy and paste all plots generated. First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
TASK: ( answers should be computerized and in details - please do not copy and paste...
TASK: ( answers should be computerized and in details - please do not copy and paste - about 1500 words ) Select an organization of your choice and carry out the following tasks. Conduct a research on the marketing and promotional strategies of the organization selected. Your Report should include the following: 1. Introduction 2. Summarize the various promotional strategies used by the organization in implementing an Integrated Marketing Strategy. Identify the risks associated with promotional campaigns and discuss how...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Part 2– R work (must be done in R) Copy and paste your R code and...
Part 2– R work (must be done in R) Copy and paste your R code and output into a word document, along with your written answers to the questions, and upload to Canvas.   Follow these instructions to import the necessary dataset: Before opening the dataset needed for this problem, you’ll need to call the “car”package.  Run the following line of code: > library(car) Now you can import the “Prestige” dataset and use it to answer the question below. Name the data...
JAVA Copy the attached code into your IDE or an online compiler and test an additional...
JAVA Copy the attached code into your IDE or an online compiler and test an additional type with the generic class. Submit your code and execution display. JAVA The test cases for Integer and String is given in the code. Please add test cases for Character, Boolean and Double type etc. // A Simple Java program to show working of user defined // Generic classes    // We use < > to specify Parameter type class Test<T> {     //...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program // This program uses a function that returns a value. #include <iostream> using namespace std; // Function prototype int sum(int num1, int num2); int main() {    int value1 = 20,   // The first value        value2 = 40,   // The second value        total;         //...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any...
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program and post it for other students to debug. To be able to receive your full discussion points, you need to submit the following. Following is your check list and rubric       Attach your .cpp file/s with an implemented bug - 20pnts       Describe what the code...
It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such...
It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such an approach?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT