Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in java one and need help with this code. please follow the directions give

ID: 3682855 • Letter: I

Question

I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required. I will apreciate your help. thank you.

In mathematics, a sexy prime is a pair (p, p + 6) of prime numbers that differ by six. The name "sexy prime" stems from the Latin word for six: sex. -- Definition taken from http://en.wikipedia.org/wiki/Sexy_prime.

Some examples of sexy prime pairs are 5 and 11, 7 and 13, and 461 and 467.

For this program, you are first going to calculate the prime numbers between 1 and 50000 using an algorithm known as the Sieve of Eratosthenes and then display the sexy prime pairs within a range specified by the user.

TEMPLATE TO START YOUR PROGRAM:


To get you started for this assignment, you will be given a main() method. You will need to type this in as you see it here, without modifications other than to put in your on NetID and leave out the comments if you don’t want to type them in.

For this assignment, you will be creating the supporting methods in addition to main(). The discussion of each of the supporting methods is described on the following pages.

public class NetID_Sieve {

public static void main ( String args[] )

{

final int HOWMANY = 50000;   // The range of numbers
boolean[] sieve = new boolean[HOWMANY+1]; //Boolean array of true/false

int lower = 1, upper = HOWMANY; // Setting initial boundaries

// the following method call will implement the Sieve algorithm processSieve( sieve );

// the following method call will print all 1419 sexy pairs of primes showPrimes( sieve, lower, upper );

// the following method call gets the lower boundary for printing lower = getLower( HOWMANY );

// the following method call gets the upper boundary for printing upper = getUpper( lower, HOWMANY );

// the following method call prints sexy pairs in the new lower-upper range showPrimes( sieve, lower, upper );

}

// implement method processSieve here // implement method showPrimes here // implement method getLower here
// implement method getUpper here

}

Implementing processSieve()

/** *

* Method processSieve will implement the Sieve of Eratosthenes algorithm on the array.

* When the method is done, all elements in the Boolean array that are true are prime numbers

* (i.e., the index of a true value in the array is a prime number) while all elements in the

* array with a false value are not prime numbers. *

* @param theArray the boolean array to process

* @return nothing, void */

--Here is an overview of the algorithm for implementing the Sieve of Eratosthenes in method processSieve():

1.Work through the entire array, from beginning to end, and set all elements to true.

2.Then set elements 0 and 1 to false, since those positions are not prime numbers.

3. Set up a loop starting at position 2 through the end of the array, incrementing by 1 each time.

a. Set up an inner loop that starts at twice the value of the outer loop’s variable, incrementing by the value of the outer loop’s value each time.

i. Set the element at the position of the innermost loop to false, as this element is a multiple of the outer loop’s value and cannot be a prime number.

For example, since element 2 has a value of true stored in it, your nested loop will identify (set to false) all elements from 3 through 50000 that are multiples of 2 (including 4, 6, 8, 10, 12, etc).

Then, when you move to element 3 in the outer loop, all elements beyond 3 in the array that are multiples of 3 (6, 9, 12, 15, 18, 21) will be identified (changed to false), and so on as the outer loop runs from 2 through the end of the array.

When you are done, the subscripts of all elements with true as values will be prime numbers.

See page five of this assignment for an example of how the Sieve algorithm works.

As a measure of whether or not your algorithm is implemented correctly and efficiently, the actual Sieve of Eratosthenes algorithm should execute in two or three seconds, not two or three minutes.

Implementing getLower()

/** * * Method getLower will simply ask the user to enter a number between 1 and the value of the parameter that is sent in to it as an argument. * @param maximum Value the largest value to allow

* @return an integer value, which is the lower boundary */

This method simply continuously prompts the user for a lower boundary between 1 and the value sent in as a parameter. So, with the example main() method provided, it will end up prompting the user for a valid value between 1 and 50000. It should not allow the user to proceed until a number within the appropriate range has been entered.

Then, the method will return the value read from the user.

Implementing getUpper()

/** * * Method getUpper will simply ask the user to enter a number between the first and second arguments that are sent to it. The first parameter that it receives is a lower boundaryand the second * parameter is the maximum value allowed. *   * @param minValue * @param maxValue * @return */

This method simply continuously prompts the user for an upper boundary between the first parameter’s value (the minimum value allowed) and the second parameter’s value (the maximum value allowed). It should not allow the user to proceed until a number within the appropriate range has been entered.

Then, the method will return the value read from the user.

Implementing showPrimes()

/** * * Method showPrimes will display the sexy prime pairs in an array between a lower and an upper boundary, provided as parameters to the method. *   * @param theArray the boolean array to process   * @param bottom the bottom position of the range to display * @param top the top position of the range to display * @return   none, void   */

--This method’s job is to display all of the sexy prime pairs starting at the lower boundary and running all the way up through the upper boundary, one pair of numbers per line.

--It should also display a count after all pairs have been displayed about how many prime pairs were displayed in that range.

--This method assumes that the boolean array that it receives has already been processed. This method should not determine the prime numbers, nor should it call the processSieve() method.

Goals

?^ Write a moderately difficult program that uses a one-dimensional array and methods.

?^ Get practice passing arrays to methods and declaring local variables inside those methods that will assist in accomplishing the desired task of the method.

?^ Using the javadoc documentation for each method and the example main() method provided, determine how to write correct and accurate method headers.

?^ Print out the prime sexy prime pairs in a specific range in the specified format.

Points to Think About

?^ The processSieve() method doesn’t need to do any input or output.

?^ The getLower() and getUpper() methods, by their nature, will need to do both input and output. You will need to create Scanner objects in each method.

?^ The showPrimes() method should only display the sexy prime pairs between the lower and upper boundaries.

?^ Do not alter the implementation of the main() method in any significant way. This will assist you in creating your method headers correctly

Grading Notes

^ You must implement the Sieve of Eratosthenes as outlined in this handout and use the array you create as outlined in order to have the program graded; programs not implementing the Sieve algorithm will not be graded. ^ Method documentation in Javadoc format must be present. See the Program Coding Standards handout for details. ^? Do NOT declare any class-level variables. The array MUST be passed to each method.

^? When printing the sexy prime pairs, you must print them using the sieve array to determine which numbers satisfy the requirements. In other words, you must use the array that you created and processed with the Sieve algorithm to print the primes and not just call a method or use some other method to determine if a pair of numbers is prime.

?^ Your output of the pairs should be as specified earlier in the assignment.

^? You should output a final count of how many pairs were printed after displaying all of the requested pairs themselves.

Sample Program Run (user input is underlined)

NOTE: The first display of 1419 pairs are not displayed here for brevity.

Please enter the lower boundary (between 1 and 50000): 0

Please enter the lower boundary (between 1 and 50000): 50001 Please enter the lower boundary (between 1 and 50000): 150

Please enter the upper boundary (between 150 and 50000):0

Please enter the upper boundary (between 150 and 50000): 200

151 and 157
157 and 163
167 and 173 173 and 179 191 and 197 193 and 199

There were 6 sexy prime pairs displayed.

Sample Program Run (user input is underlined)

NOTE: The first display of 1419 pairs are not displayed here for brevity.

Please enter the lower boundary (between 1 and 50000): 1 Please enter the upper boundary (between 1 and 50000): 42

Here are all of the sexy prime pairs in the range 1 to 42, one pair per line: 5 and 11
7 and 13
11 and 17 13 and 19 17 and 23 23 and 29 31 and 37

There were 7 sexy prime pairs displayed.

Comment

EXAMPLE OF PROCESSING THE SIEVE OF ERATOSTHENES THROUGH THE FIRST 4 ELEMENTS OF A SMALL 20 ELEMENT ARRAY INITIALARRAY (elements with T [representing true here] are assumed to be prime now): 17 AFTER PROCESSING ELEMENT 2 (multiples of 2 are set to false; 2 is left with a T indicating it's a prime number): [0] 12][3][4]15 [678] 9][10] 1112] [13] 14]15] 16] 17 1][19] AFTER PROCESSING ELEMENT 3 (multiples of 3 are set to false; 3 is left with a T indicating it's a prime number): You would continue on, processing elements 4 through 19 in the same way. There are some shortcuts to the Sieve to make this process even more efficient, but the way we're approaching it is a satisfactory way to implement it At this point, all elements with true as their values are considered to be prime and all elements with false are considered to not be prime. In this array of numbers from 1 through 19, the prime numbers are: 2, 3, 5, 7, 11, 13, 17 and 19. The numbers themselves come from the subscripts of those elements with true as their values.

Explanation / Answer

import java.util.Scanner;

public class NetID_Sieve {
public static void main(String args[]) {
final int HOWMANY = 50000;
boolean[] sieve = new boolean[HOWMANY + 1];
int lower = 1, upper = HOWMANY;

processSieve(sieve);
showPrimes(sieve, lower, upper);
lower = getLower(HOWMANY);
upper = getUpper(lower, HOWMANY);
showPrimes(sieve, lower, upper);
}

/**
*
* Method processSieve will implement the Sieve of Eratosthenes algorithm on the array.
* When the method is done, all elements in the Boolean array that are true are prime numbers
* (i.e., the index of a true value in the array is a prime number) while all elements in the
* array with a false value are not prime numbers.
*
* @param theArray the boolean array to process
* @return nothing, void
*/

public static void processSieve(boolean[] sieve) {
for (int x = 1; x < sieve.length; x++) {
sieve[x] = true;
}


for (int i = 4; i <= 50000; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {

sieve[i] = false;
break;
}

}

}


}


public static int getLower(int HOWMANY) {

int lower = 0;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the lower boundary (between 1 and 50000): ");
lower = input.nextInt();
while ((lower < 1) || (lower > 50000)) {
System.out.println(" Please enter the lower boundary (between 1 and 50000): ");

lower = input.nextInt();
}
return lower;
}

/**
*
* Method getUpper will simply ask the user to enter a number between the first and second
* arguments that are sent to it. The first parameter that it receives is a lower boundary
* and the second parameter is the maximum value allowed.
*
* @param minValue the smallest value to allow
* @param maxValue the largest value to allow
* @return an integer value, which is the upper boundary
*/

public static int getUpper(int lower, int HOWMANY) {
Scanner input = new Scanner(System.in);
int upper = 0;
System.out.printf("Please enter the upper boundary (between %d and 50000): ", lower);
upper = input.nextInt();
while ((upper <= lower) || (upper > 50000) || (upper < 1)) {
System.out.printf("Please enter the upper boundary (between %d and 50000): ", lower);
upper = input.nextInt();
}
return upper;
}
/**
*
* Method showPrimes will display the sexy prime pairs in an array between a lower and an
* upper boundary, provided as parameters to the method.
*
* @param theArray the boolean array to process
* @param bottom the bottom position of the range to display
* @param top the top position of the range to display
* @return none, void
*/

public static void showPrimes(boolean[] sieve, int lower, int upper) {
int counter = 0;
System.out.printf("Here are all of the sexy prime pairs in the range %d and %d, one per line: ", lower, upper);
int i = 0;
if(lower == 1) i = 5;
else i = lower;
for (; i < upper - 6; i++) {
if (sieve[i] == true && sieve[i + 6] == true) {
System.out.printf("%d and %d ", i, i + 6);
counter++;
}
}
System.out.printf("There were %d sexy prime pairs displayed. ", counter);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote