Question

list. Then create another ArrayList L2, with all the prime numbers in the list L1. Use an enhanced for-loop instead of an ordCode in Java to be typed in Eclipse.

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

//RandomList.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class RandomList {

   public static void main(String[] args) {
      
       Scanner s = new Scanner(System.in); //Scanner object to get input N from user
       System.out.println("Enter number of random Integers to generate: ");
       int n = s.nextInt();
       //Creating ArrayLists
       ArrayList<Integer> L1 = new ArrayList<Integer>();
       ArrayList<Integer> L2 = new ArrayList<Integer>();
       //Creating an object to generate random numbers
       Random r = new Random();
       for(int i=0; i<n; i++)   // This will add N random integers to the list L1.
       {
           L1.add(r.nextInt(1000));       //r.nextInt(1000) will generate random integers between 0 and 1000, and add them to list L1
       }
       //using enhanced for loop to add prime numbers from L1 to L2
       for(int num : L1)
       {
           int i = 2;
           boolean prime = true; // a flag to check for prime
           //If the generated random numbers are 1 or 2, adding 1 and 2 to the prime numbers list L2
           if(num==1)
               L2.add(num);
           else if(num==2)
               L2.add(num);
           else
           {
               while(i<=num/2)  
               {
                   if(num%i == 0)        //checking if the number is divisible, i.e. if it is a prime number or not
                   {
                       prime = false;
                       break;
                   }
                   i++;
               }
               if(prime)
               {
                   L2.add(num);   //add the number to List L2 if it is a prime number;
               }
           }
       }
       System.out.println("List L1");
       System.out.println(L1);
       System.out.println("List L2 Prime");
       System.out.println(L2);
   }
}

Add a comment
Know the answer?
Add Answer to:
Code in Java to be typed in Eclipse. list. Then create another ArrayList L2, with all...
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
  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU....

    THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU. Program 1 Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as the volume and the surface area of a three-dimensional shape. Then make classes and subclasses that implement various shapes such as cube, cylinders and spheres. Place common behavior in superclasses whenever possible, and use abstract classes as appropriate. Add methods to the...

  • in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an...

    in java eclipse, Create a program “FibonacciFifteen.java” and a method called “double[] getFibonacci()” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array by adding the prior two elements.

  • Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object con...

    Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object consists of the following fields: int rollno String name String address Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method and place your code in a separate Java source file. Do not use a sort method from the Java collections library.

  • JAVA Create an ArrayList called list of type Character 2. Add the following five elements |...

    JAVA Create an ArrayList called list of type Character 2. Add the following five elements | < \ / > (4 points) 3. Print the elements of the list, with a label (3 points) 4. Delete the last element of the list (4 points) 5. Use the method size to determine how many elements there are in the list (no hard-coded numbers). Print the number of elements. (3 points) 6. Add a plus sign ( + ) to the list...

  • *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add...

    *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For example, if the list originally contained 2, 4, -3, then it should contain 3, 5, -2 after the lines of code execute. Just write the necessary lines of code (not a whole method or program). Assume we already created the ArrayList and filled it with Integers.

  • Create a Java program that analyzes a program and produces the output into another file. The...

    Create a Java program that analyzes a program and produces the output into another file. The program must retrieve the path to the .java file. Once the path to the file is found the program must figure out the length of the longest line is in the program. For example, working with a program called MainFile.java. Once it figures out the longest line in the code it will put the output into MainFile.java.stats and it will be located in the...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Hello, this is java code. I'm practicing my Java and add comments if possible for explanation....

    Hello, this is java code. I'm practicing my Java and add comments if possible for explanation. Question 1: Create an java program that has a class called Square. Create a function called squareParameter that calculates the parameter of the sides of the square. Write test methods in main() to test for different output. Question 2: Create a class called CarYearMade that contains a function called yearMade. This function receives an arraylist of different years. The function should return another arraylist...

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