Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My program is doing everything that it should except that when it runs through test.showPrimes the second time, it does not print the sexy primes

My program is doing everything that it should except that when it runs through test.showPrimes the second time, it does not print the sexy primes that fall between the user's low and upper boundaries that they specified. I don't understand why. I've tried everything that I can think of. I cannot separate the method into two methods so that I can return values. It has to be done with the one method that sets the boundaries.

-----------------------------------------------

import java.util.Scanner;

public class Sieve {

/** * This is the main method, it defines variables used throughout the * program and creates a new scanner for user input. * * @return nothing */

public static void main (String args[]) {

}//end main

/** * This method (processSieve) creates an array with prime numbers * * @param boolean[] primes, int highnum * @return nothing */

public void processSieve (boolean[] primes, int highnum) {

for (int i=2; i<=highnum; i++) { primes[i] = true; }

for (int i=2; i*i<=highnum; i++ ) { if (primes[i]) { for (int z =i; i*z<=highnum; z++) { primes [i*z] = false; } } } }//end processSieve

/** * This method (intro) prints information about the Sieve of Eratosthenes to the screen * * @return nothing */

public void intro () {

System.out.println (" The Sieve of Eratosthenes! The Sieve of Eratosthenes " + "is an ancient algorithm for finding all prime numbers up " + "to a specific number from the set of integers. We use this " + "to find a second mathematical series called Sexy Twins or " + "Sexy Primes (from the Latin for six) or sets of prime numbers " + "which are only 6 digits apart like the numbers 7 and 13. You " + "will need to enter an upper and lower boundary between 1 and " + "50000. We'll give you the Sexy Primes between them. ");

System.out.print ("Please enter a lower boundary and an upper boundary and I will" + " print all of the sexy prime pairs between those boundaries. ");

}//end intro

/** * This method (getBoundaries) sets the low boundary from user input * * @param int lowNum, int highNum * @return nothing */

public void getBoundaries (int lowNum, int highNum) {

Scanner in = new Scanner (System.in);

do {

do {

System.out.print ("Please enter the lower boundary (between 1 and 50000): ");

lowNum = in.nextInt();

} while (lowNum<=0 || lowNum>50000);

do { System.out.print ("Please enter the upper boundary (between 1 and 50000): ");

highNum = in.nextInt();

while (highNum

System.out.print (" Your upper boundary cannot be smaller than your lower boundary ");

System.out.print ("Please enter the lower boundary (between 1 and 50000): ");

lowNum = in.nextInt();

System.out.print ("Please enter the upper boundary (between 1 and 50000): ");

highNum = in.nextInt(); } } while (highNum<=0 || highNum >50000);

} while (lowNum == highNum);

}//end getBoundaries

/** * This method (showPrimes) prints the sexy primes and tallies them * * @param boolean[] primes, int lowNum, int highNum * @return nothing */

public void showPrimes (boolean[] primes, int lowNum, int highNum) {

int count = 0;

for (int i=lowNum; i

if (primes[i]==true && primes [i+6]==true) {

++count; System.out.println (i + " and " + (i+6) ); } }

System.out.println (" There were " + count + " sexy prime pairs displayed between " + lowNum + " and " + highNum);

}//end showPrimes }//end class

-----------------------------------------------

public class SieveTest { public static void main (String args []) {

final int endNum = 50000; boolean[] primes = new boolean [endNum +1]; int lowNum = 1; int highNum = endNum;

Sieve test = new Sieve();

test.processSieve(primes, highNum); //run the Sieve algorithm test.showPrimes(primes, lowNum, highNum); //show entire set of sexy pairs, 1419 of them test.intro(); //show the introductory remarks test.getBoundaries(lowNum, highNum); //get low and high boundary from the user test.showPrimes(primes, lowNum, highNum); //now shows sexy pairs between lower and upper

} }

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

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions