GUI to check large numbers for primness in parallel Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of GUI to check large numbers for primness in parallel without wasting too much if your time.

The question is published on by Tutorial Guruji team.

My task is to create a GUI that launches threads to test excessively large numbers for primness in parallel. I would like to provide two text boxes into which the user can type numbers. When a button is clicked I want to launch a thread for each number. When a thread is done I want to write the results of that computation into a corresponding field on the form. I am receiving a few errors:

Exception in thread "Thread-2" java.lang.NullPointerException. as well as at primeBigIntegers.MyThread.IsPrime(MyThread.java:27) which is this line of code: 
if (num.mod(new BigInteger("2")).compareTo(BigInteger.ZERO) == 0) {
and another error at primeBigIntegers.MyThread.run(MyThread.java:43)

which is:

 String output = num + " is" + (IsPrime(num) ? " " : " not "). 

All of the display boxes are appearing correctly but the code is not checking for primeness.

 public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Test09 frame = new Test09();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }});}
public Test09() {
    clickCounter = 0;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final JButton btnCalc = new JButton("Calculate");
    btnCalc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnCalc.setEnabled(false);{
                MyThread mt = new MyThread(lblNewLabel, clickCounter);
                mt.start();
            }
            btnCalc.setEnabled(true);
        }});
    btnCalc.setBounds(10, 239, 91, 23);
    contentPane.add(btnCalc);

    JLabel lblOfThreads = new JLabel("First Number: ");
    lblOfThreads.setHorizontalAlignment(SwingConstants.RIGHT);
    lblOfThreads.setBounds(10, 38, 110, 14);
    contentPane.add(lblOfThreads);

    txtNumberOfThreads = new JTextField();
    txtNumberOfThreads.setText("15485867");
    txtNumberOfThreads.setBounds(130, 35, 80, 20);
    contentPane.add(txtNumberOfThreads);
    txtNumberOfThreads.setColumns(10);

    JLabel lblOfThreads2 = new JLabel("Second Number: ");
    lblOfThreads2.setHorizontalAlignment(SwingConstants.RIGHT);
    lblOfThreads2.setBounds(10, 38, 110, 100);
    contentPane.add(lblOfThreads2);

    txtNumberOfThreads2 = new JTextField();
    txtNumberOfThreads2.setText("45862118");
    txtNumberOfThreads2.setBounds(130, 75, 80, 20);
    contentPane.add(txtNumberOfThreads2);
    txtNumberOfThreads2.setColumns(10);

    lblNewLabel = new JLabel("Results Here");
    lblNewLabel.setVerticalAlignment(SwingConstants.TOP);
    lblNewLabel.setBackground(Color.BLACK);
    lblNewLabel.setBounds(10, 175, 206, 104);
    contentPane.add(lblNewLabel);
    }}

 public class MyThread extends Thread{

private JLabel myLabel;
private int threadNumber;
private BigInteger num; 

public MyThread(JLabel lblNewLabel, int i) {
    this.myLabel = myLabel;
    this.threadNumber = threadNumber;
    this.num= num; 
}

public static boolean IsPrime(BigInteger num) {
    if (num.mod(new BigInteger("2")).compareTo(BigInteger.ZERO) == 0) {
      return false;
    }
    for (BigInteger i = new BigInteger("3"); i.multiply(i).compareTo(num) <= 0; i =
        i.add(new BigInteger("2"))) {
      if (num.mod(i).compareTo(BigInteger.ZERO) == 0) {
 return false;
      }}
    return true;
  }
public void run() {
    String output = num + " is" + (IsPrime(num) ? " " : " not ")
            + "a prime number.";
  JOptionPane.showMessageDialog(null, output);

}}

Answer

this.num = num; in your constructor does nothing (there is no parameter num, so you are assigning this.num the value of itself). num is still null. You need to initialize it.

We are here to answer your question about GUI to check large numbers for primness in parallel - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji