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

The integers n and k are twin primes if both integers are primes and n = k + 2 o

ID: 3860784 • Letter: T

Question

The integers n and k are twin primes if both integers are primes and n = k + 2 or n = k - 2. The file primes.csv contains the prime numbers between 2 and 10,000 numbered according to their position in the sequence of prime numbers. Develop an interactive program that accepts an integer n between 3 and 1229 (referred to as a query). The program finds the n'th prime number, then checks if it is a member of a set of twin primes. If it is a member of a set of twin primes the program outputs n and it's one or two twins. If it is not a member of a set of twin primes the program outputs n. Use a hash table to check for primality. Store the primes in a hash table based on their sequence index. Given a key 1 lessthanorequalto k lessthanorequalto 1229 the program finds the corresponding prime n (using the hash table). Next, the program uses the hash table to check if n is a member of set of twin primes and produces an appropriate output. If the integer entered (k) is out of the range specified above the program generates an exception and terminates. The program keeps a log of all the detected twin primes in a CSV file where each line contains a query and its results each of which is a different column in the file. Set of prime twins detected. Specific Instructions: 1) Use a hash table with initial capacity of 100 and initial load factor of 0.75 for the primality check. 2) Use the Java "File" class for IO to files and to the standard input/output devices. 3) The GUI should include a menu option to get the names of the input CSV file and output CSV files. Additionally, prompt the user via a non-editable text-field widget to enter the integers to be checked for being twin primes. Use a non-editable text-box (choose any text box) widget to notify the user concerning the result of each query. Finally, use an editable text-box to get the user input (k). 4) On a termination via exception the program provides the user with sufficient details concerning the nature of the exception, outputs the system stack, and halts execution.

Explanation / Answer

import java.io.*;

class FindTwinPrimeNumbers

{

boolean isGivenNumberAPrimeNumber(int number)

{

int countUntillNumber = 0;

for(int i=1; i<= number; i++)

{

if(number %i == 0)

countUntillNumber ++;

}

if(countUntillNumber == 2)

return true;

else

return false;

}

  

public static void main(String args[]) throws IOException

{

FindTwinPrimeNumbers object = new FindTwinPrimeNumbers();

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the N'th Number : ");

int p = Integer.parseInt(br.readLine());

if(p<3 || p>1229)

System.out.print("Invalid number: Please enter between 3 and 1229");

if(ob.isGivenNumberAPrimeNumber(p) == true && ob.isGivenNumberAPrimeNumber(p+2) == true)

System.out.print("Twin Prime pair: " + p + " " + p+2);

else if(!ob.isGivenNumberAPrimeNumber(p+2))

System.out.print(p + " is not a part of a twin prime pair");

}   

}

}

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