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: 3861145 • 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. Use a hash table with initial capacity of 100 and initial load factor of 0.75 for the primality check. Use the Java "File" class for IO to files and to the standard input/output devices. 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). 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

package com.mkyong.csv;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

class TwinPrimeRange

{       

     boolean isPrime(int n) //funton for checking prime

        {

            int count=0;

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

                {

                    if(n%i == 0)

                        count++;

                }

            if(count == 2)

                return true;

             else

                return false;

        }

    public static void main(String[] args) {

        String csvFile = " D:prime.csv";

        String line = "";

        String cvsSplitBy = ",";

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {

                // use comma as separator

                String[] prime= line.split(cvsSplitBy);

// lower and upper ranges prime[1],prime[2]

             

            if(prime[1] > prime[2])

                System.out.println("Invalid Range !");

            else

            {

                System.out.println("nThe Twin Prime Numbers within the given range are : ");

                for(int i=p; i<=(q-2); i++)

                {

                    if(ob.isPrime(i) == true && ob.isPrime(i+2) == true)

                    {

                        System.out.print("("+i+","+(i+2)+") ");

                    }

                }

            }                

        }

           

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

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