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

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...
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> {     //...
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?
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;         //...
NO COPY/PASTE. You are a task leader for a 3-person (yourself and two others) team that...
NO COPY/PASTE. You are a task leader for a 3-person (yourself and two others) team that specialize in mobile app development, both Swift for iOS and Android. Your PM has given you a project to develop a mobile app with the goal of activating home appliances in “smart homes”, with a timeline of 6 months to final deliverable. Pick one SDLC methodology, and outline the entire process from start to finish. Be detailed. HINT Think how IoT (Internet of Things)...
Answer the below question in your own words please do not copy and paste. 4. What...
Answer the below question in your own words please do not copy and paste. 4. What is an industry that is in the maturity phase of the life cycle? How intense is the competition? 5. What is an example of an industry in the decline stage? What strategies are the current firms following? 6. Can you think of a firm that has completed a successful (or unsuccessful) turnaround?
Need IN JAVA Please DO NOT COPY AND PASTE old solutions WILL UPVOTE The Babysitters Club...
Need IN JAVA Please DO NOT COPY AND PASTE old solutions WILL UPVOTE The Babysitters Club Employment Agency has decided to computerize their payroll. A babysitter earns a specific fee per hour until 9:00 PM (while the children are awake and active), a lower rate between 9:00 PM and midnight (after bedtimes and the babysitter can study), and a higher fee after midnight (when babysitting cuts into prime sleeping time). They maintain two datafiles are one for the employees and...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible. Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT