Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this exercise you will be examining the behavior of the Set data structure. FOR ALL EXERCISES: Make sure you place a comment block at

In this exercise you will be examining the behavior of the Set data structure. FOR ALL EXERCISES: Make sure you place a comment block at the top of the code with your name and your partner's name. Create a ClosedLab07 project folder. Then import the above file into that ClosedLab07 project folder. As a reminder, you can do this by selecting File -> Import, then expanding the General folder and selecting "File System". Then browse to the directory where you saved the two files above. (More detailed instructions can be found in the CSE 1223 Closed Lab 01 assignment). Once you have the code imported, check to make sure that there are no errors. This file should import "cleanly" so if there are errors you may have had a problem importing it. Once the errors are taking care of, try running SetTest and see what it does. Trace through the code to make sure that the program is doing what you think it should be doing. Notice that halfway down the code there is a Set that is declared as a HashSet. Change it to a TreeSet. Does the code behave any differently? Your first task is to modify this piece of code so that instead of just putting numbers into a Set and then taking them back out again, it implements the sieve of Eratosthenes algorithm. The sieve of Eratosthenese is an ancient algorithm for finding prime numbers. The basic algorithm is: 

Pick a final boundary number - a positive integer n

Fill a set with all of the integer values between 2 and n. The sieve will find all of the prime numbers between 2 and n.

Check each number in the Set and remove anything that is evenly divisible by 2 except for 2 itself.

Now check each number in the Set and remove anything that is evenly divisible by 3 except for 3 itself.

Continue checking each number in the Set, incrementing your check value by 1 each time and removing anything that is evenly divisible by your check value until your check value is larger than n.

When you are done, your Set will contain all of the prime numbers between 2 and n.

Modify the SetTest code so that it performs this algorithm. You have the basic operations in the code already - the ability to add elements to the Set, iterate over the Set, and remove elements from the Set. You just need to add the tests to see if the elements of the Set are divisible by other numbers or not. When you are done, display the elements of your set one to a line. (NOTE: You can actually stop the sieve after your check value is larger than the square root of n instead of n. Once you get it working going all the way to n, see if you can modify it by using the Math.sqrt() method for your check value.) import java.util.*; public class SetTest { public static void main(String[] args) { // prompt the user for an upper bound and make sure // it is a positive number int bound = 0; Scanner keyboard = new Scanner(System.in); while (bound <=0) { System.out.print("Enter a positive integer: "); bound = keyboard.nextInt(); if (bound <=0) { System.out.println("ERROR! Upper bound must be positive!"); } } // add all values between 1 and the upper bound input // by the user to the test set. Include the upper bound Set testSet = new HashSet(); //Set testSet = new TreeSet(); int i=1; while (i<=bound) { testSet.add(i); i=i+1; } // display the elements of the testSet as a debugging // step. System.out.println("Set contains: "+testSet); // iterate over all of the elements of the testSet. // Since this is a set we must use an iterator to // perform this task Iterator iter = testSet.iterator(); while (iter.hasNext()) { int value = iter.next(); System.out.println("Value in set is: "+value); iter.remove(); } // display the final values in the testSet as a debugging // step. This should always be an empty set. System.out.println("Set contains: "+testSet); } } 

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

Students also viewed these Databases questions

Question

What is Aufbau's rule explain with example?

Answered: 1 week ago