Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Primes The skeleton of the Primes class already exists and is in Primes.java. Step 1. Look at the skeleton in Primes.java. Compile Primes. Run the

Primes The skeleton of the Primes class already exists and is in Primes.java. Step 1. Look at the skeleton in Primes.java. Compile Primes. Run the main method in Primes. Checkpoint: If all has gone well, the program will run and accept input. It will then end. The goal now is to create the list of candidates. Step 2. In main declare and create the candidates list. Add in the values. Step 3. Print out the candidates list. Checkpoint: Compile and run the program. Enter 7 for the maximum value. You should see the list { <2> <3> <4> <5> <6> <7> }. The next goal is to do a single round finding a prime in the candidates list. Step 4. In main declare and create the primes and composites lists. Step 5. Remove the first value from the primes list and remember it in a variable. Step 6. Print out the prime that was discovered. Step 7. Add it to the primes list. Step 8. Print out all three lists. Checkpoint: Compile and run the program. Enter 7 for the maximum value. The value 2 should be removed from the candidates list and added to the primes. Now all values that are divisible by the prime should be removed from the candidates list and added to the composites list. Next, this procedure will be encapsulated in the method getComposites(). Step 9. Refer to the pre-lab exercises and complete the getComposites() method. To determine if one integer value is divisible by another, you can use the modulus operator (% in Java). Step 10. Between the code from steps 7 and 8, call getComposites(). Lab Manual for Data Structures and Abstractions with Java 171 Checkpoint: Compile and run the program. Enter 15 for the maximum value. Compare the results with the pre-lab exercises. Reconcile any differences. Just as in the counting game, a loop will be used to do the rounds. Step 11. Wrap the code from steps 5 through 8 in a while loop that continues as long as the candidates list is not empty. Final checkpoint: Compile and run the program. Enter 15 for the maximum value. Compare the results with the pre-lab exercises. Reconcile any differences. Run the program with 100 as the maximum value. Carefully verify your results.

-------------------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*;

/** * Primes is a program that will compute prime numbers using the sieve of Eratosthenes. * * @author Charles Hoot * @version 4.0 */

public class Primes {

public static void main(String args[]) {

int max; System.out.println("Please enter the maximum value to test for primality"); max = getInt(" It should be an integer value greater than or equal to 2."); // COMPLETE THE MAIN } /** * getComposites - Remove the composite values from possibles list and * put them in the composites list. * * @param candidates A list of integers holding the possible values. * @param composites A list of integers holding the composite values. * @param prime An Integer that is prime. */ public static void getComposites(ListInterface candidates, ListInterface composites, Integer prime) { // COMPLETE THIS METHOD } /** * Get an integer value. * * @return An integer. */ private static int getInt(String rangePrompt) { Scanner input; int result = 10; //Default value is 10 try { input = new Scanner(System.in); System.out.println(rangePrompt); result = input.nextInt(); } catch(NumberFormatException e) { System.out.println("Could not convert input to an integer"); System.out.println(e.getMessage()); System.out.println("Will use 10 as the default value"); } catch(Exception e) { System.out.println("There was an error with System.in"); System.out.println(e.getMessage()); System.out.println("Will use 10 as the default value"); } return result; } }

-------------------------------------------------------------------------------------------------------------------------- Write in java

VERY IMPORTANT CODE BELOW!!!!

https://codeshare.io/2jKx0v <---- AList.java

https://codeshare.io/24R4e3 <------ ListInterface

^ Those code are already done you need these classes

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions