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: 3859987 • 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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class TwinPrimes {
//check twin prime
public static boolean checkTwin(int num1, int num2) {
if(Math.abs(num2-num1) == 2) {
return true;
}
return false;
}
public static void main(String args[]) throws FileNotFoundException {
//scanner object to read user input
Scanner sc = new Scanner(System.in);
ArrayList<Integer> primes = new ArrayList<>();
//reading primes from csv
String fileName= "primes.csv";
File file= new File(fileName);
Scanner inputStream;

try{
inputStream = new Scanner(file);
while(inputStream.hasNext()){
String line= inputStream.next();
String[] prime_values = line.split(",");
// storing prime numbers
for (String value : prime_values) {
primes.add(Integer.parseInt(value));
}
}
inputStream.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
  
boolean twinPrimeSet = false;
// writer to print data to csv
PrintWriter pw = new PrintWriter(new File("output.csv"));
StringBuilder res = new StringBuilder();
res.append("Num1");
res.append(',');
res.append("Num2");
res.append(',');
res.append("Result");
res.append(' ');
// processing data
while(true) {
//reading user input
System.out.print("Enter an number between 3 and 1229 : ");
int n = sc.nextInt();
if( n >= 3 && n<= 1229) {
//check n is part of twin primes set
for(int i=0;i<primes.size();i++) {
res.append(""+primes.get(i));
res.append(',');
res.append(""+primes.get(n));
res.append(',');
  
if(checkTwin(primes.get(i),primes.get(n))) {
System.out.println("It is member of set of twin primes..");
twinPrimeSet = true;
res.append("True");
break;
}
else{
res.append("False");
}
res.append(' ');
}
if(!twinPrimeSet) {
System.out.println("It is not a member of set of twin primes..");
}
break;
}
}
pw.write(res.toString());
pw.close();
}
}

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