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

Prime.csv file Assignment Instructions: The integers k and n are twin primes if

ID: 3865787 • Letter: P

Question

Prime.csv file Assignment Instructions: The integers k and n are twin primes if both integers are primes and k = n + 2 or k = n-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 k between 3 and 1229 (referred to as a query). The program finds the k'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 the k'th prime number and it's one or two twins. If it is not a member of a set of twin primes the program outputs the k'th prime number Use a hash table to check for primality. Store the primes in a hash table based on their sequence index. Given a key 1 s k s 1229 the program finds the corresponding the k'th prime number (using the hash table). Next, the program uses the hash table to check if the k'th prime number 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 works on one integer at a time and keeps a log of all the detected twin primes ina CSV file where each line contains a query and its results each of which is a different column in the file Specific Instructions: 1) Use a hash table with initial capacity of 100 and initial load factor of 0.75 for the 2) Use Java File IO (c.g., InputStream and OutputStream) to write to files and to the 3) The GUI should include a menu bar option to get the names of the input CSV file and primality check standard input /output devices 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). The GUI should include an explict exit button 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

Solution=================================================

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Hashtable;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class PrimeTwins {
  
   private Hashtable<Integer,Integer> primes;
   private String inputFile;
   private String outFile;
  
  
   public PrimeTwins(){
       //Initial capacity 100, load factor 0.75,
       primes = new Hashtable<>(100,0.75f);
   }
  
   public void setPrimesFromFile() throws FileNotFoundException{
       File file = new File(inputFile);

       Scanner inFile = new Scanner(file);
           int index=1;
           //Taking input from file
           while(inFile.hasNextInt()){
               primes.put(index, inFile.nextInt());
               index++;
           }
          
           inFile.close();
          
  
   }
  
  
   String twinPrimalityQuery(int index) throws Exception{
      
               if(index<3 || index>1229){
               throw new Exception("Wrong Index Range");
               }
               else{
                  
               Writer writer = new FileWriter(outFile);
               int num=primes.get(index);
               int preNum=primes.get(index-1);
               int postNum=-1;
              
               if(index!=1229){
               postNum=primes.get(index+1);
               }
               System.out.println("l");
              
               boolean detected=false;
              
               int results[] = {num,-1,-1};
               String str=num+"";
              
               if(num+2==postNum){
                   detected=true;
                   results[1]=postNum;
                   str=str+" "+postNum;
               }
              
               if(num-2==preNum){
                   detected=true;
                   results[2]=preNum;
                   str=str+" "+preNum;
               }
              
               //Write in log file only if twin(s) found
               if(detected){
                   writer.append(str+" ");
               }
              
               writer.close();
              
               return str;
           }
       }
  
   void guiController(){
       JFrame frame= new JFrame("Prime");
       JButton queryBtn = new JButton("Query");
       JButton exitBtn = new JButton("Exit");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,150);
      
        JMenuBar mb = new JMenuBar();
        JMenu m1 = new JMenu("FILE");
        JMenuItem fileSetup = new JMenuItem("File Setup");
        fileSetup.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              String inName=JOptionPane.showInputDialog(frame, "Provide Input File Name");
              String outName=JOptionPane.showInputDialog(frame, "Provide output File Name");
           
                  inputFile=inName;
                  outFile=outName;
                  try{
                  setPrimesFromFile();
              
                  }catch(Exception e1){
                   JOptionPane.showMessageDialog(frame, e1.getMessage(), "ERROR!",JOptionPane.ERROR_MESSAGE);
                   e1.printStackTrace();
                   System.exit(0);
                  }
              
              
                  queryBtn.setVisible(true);
          
            }
          });
      
      

      
        queryBtn.setVisible(false);
        queryBtn.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               JTextField message = new JTextField();
               message.setText("Enter the number");
               message.setEditable(false);
              
               JTextArea disclaimer = new JTextArea(5,10);
               disclaimer.setLineWrap(true);
               disclaimer.setText("The number k must be between 3 and 1229, where k means the the kth Prime number."
                       + "You will get results in format of Prime Number at Kth poistion, followed by its twins if any");
               disclaimer.setEditable(false);
              
               final JComponent[] inputs = new JComponent[] {
                       message,disclaimer
               };
              
               String result=JOptionPane.showInputDialog(null, inputs, "Query Box", JOptionPane.PLAIN_MESSAGE);
               String queryResults="";
                      
               try{
                   queryResults=twinPrimalityQuery(Integer.parseInt(result));
               }
               catch(Exception e2){
                   JOptionPane.showMessageDialog(frame, e2.getMessage(), "ERROR!",JOptionPane.ERROR_MESSAGE);
                   e2.printStackTrace();
                   System.exit(0);
               }
               JOptionPane.showConfirmDialog(null, queryResults, "Query Result", JOptionPane.PLAIN_MESSAGE);
                  
              
           }
        });
      
      
        exitBtn.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
        });
      
      
      
    
      
        m1.add(fileSetup);
      
        mb.add(m1);
        frame.setJMenuBar(mb);
        JPanel btnHold = new JPanel();
        btnHold.setPreferredSize(new Dimension(50,50));
        frame.add(btnHold,BorderLayout.SOUTH);
        btnHold.add(queryBtn);
        btnHold.add(exitBtn);
        frame.setVisible(true);
   }
  
  
   public static void main(String[] args) {
      
      
       new PrimeTwins().guiController();
      
   }

}

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