Question: CODE IMPLEMENTATION import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; /** * PrimesAndComposites is a program that will compute prime numbers using the sieve of Eratosthenes algorithm.

CODE IMPLEMENTATION import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; /** * PrimesAndComposites is a program that will compute prime numbers using the sieve of Eratosthenes algorithm. * * @version 10/25/2016 */ public class PrimesAndComposites { private ArrayList composites; private ArrayList primes; public PrimesAndComposites(ArrayList candidates) { this.composites = new ArrayList(); this.primes = new ArrayList(); setPrimesAndComposites(candidates); } public void setPrimesAndComposites(ArrayList candidates) { Integer foundPrime; Integer maybeComposite; Iterator candidatesIter = candidates.iterator(); System.out.println("in setPrimesAndComposites method - implement me"); // fills the primes and composites lists // when the method is finished the candidates list is empty // scans the candidates list with an iterator and removes elements from the candidates list with // an iterator's remove method } public void display() { System.out.println("The primes list is " ); Iterator iter = this.primes.iterator(); while(iter.hasNext()) { System.out.print(iter.next() + " "); } System.out.println(); System.out.println("The composites list is "); iter = this.composites.iterator(); while(iter.hasNext()) { System.out.print(iter.next() + " "); } System.out.println(); } public static void main(String args[]) { ArrayList candidates; Scanner keyboard = new Scanner(System.in); final int DEFAULT_MAX = 10; int max; System.out.println("Enter the maximum value to test for primes" + " It should be an integer value greater than or equal to 2."); try { max = keyboard.nextInt(); if (max out.println(max + " is smaller than 2. Will use " + DEFAULT_MAX + " as the default value."); max = DEFAULT_MAX; } } catch(NumberFormatException e) { System.out.println("Could not convert input to an integer."); System.out.println(e.getMessage()); System.out.println("Will use " + DEFAULT_MAX + " as the default value."); max = DEFAULT_MAX; } catch(Exception e) { System.out.println("There was an error with System.in"); System.out.println(e.getMessage()); System.out.println("Will use " + DEFAULT_MAX + " as the default value."); max = DEFAULT_MAX; } System.out.println(" ====> Constructing list of candidates up to " + max); candidates = new ArrayList(); for(int i=2; inew Integer(i)); System.out.println("The candidates list is " + candidates); PrimesAndComposites pac= new PrimesAndComposites(candidates); pac.display(); } } IV. Primes Write a program that will compute the list of primes and composites from the given set of numbers, using Sive of Eratosthenes algorithm Your Task: The skeleton of the program is provided for you. You need to give implementation for setPrimesAndComposites method using appropriate Iterator objects A sample run of the program and a UML diagram are provided Make sure that the output is correct (see Sample Runs below) 1. 2. 3. Sample Run: RUN 1 Enter the ma x irnum value Lo est for primes It should be an integer value greater than or equal to 2 17 Constructing list of candidates up to 17 The cand i datet. is [2, 3, 1, 5, 6, 7, 1, 9, 10, 11, 12, 13, 11, 15, 16, 171 Found the prime 2 Found tho composito 4 Found the composite 6 the -=-=> Found tho composito 8 ====> Found the composite 10 Found the composite 12 Found the composite 14 Found the composite 16 >Found the primo 3 Found the composite 9 Found the composite 15 > Found the prime 5 Found the prime 7 Found the prime 11 Found the prime 13 Fond the prime 17 The primes list is 2 3 5 7 11 13 17 The composites list is 4 6 8 10 12 14 16 9 15 composites Arraylist
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
