Monday, October 12, 2015

Prime number checker

This is a runnable jar file. This check if a number is a prime number or not.

Download from:
https://www.dropbox.com/s/943fciwxvo0zapa/Primenumcheck.jar?dl=0

Code:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class GUItest extends JFrame {

 private JPanel contentPane;
 private JTextField textField;
 private final Action action = new SwingAction();
 private JLabel lblEnterANumber;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     GUItest frame = new GUItest();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the frame.
  */
 public GUItest() {
  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);
 
  JLabel lblNewLabel = new JLabel("Prime number checker");
  lblNewLabel.setBounds(128, 32, 157, 13);
  contentPane.add(lblNewLabel);
  lblNewLabel.setHorizontalAlignment(lblNewLabel.CENTER);
 
  textField = new JTextField();
  textField.setBounds(147, 101, 116, 24);
  contentPane.add(textField);
  textField.setColumns(10);
 
  JButton btnNewButton = new JButton("Check");
  btnNewButton.setAction(action);
  btnNewButton.setBounds(147, 182, 116, 21);
  contentPane.add(btnNewButton);
 
  lblEnterANumber = new JLabel("Enter a number");
  lblEnterANumber.setBounds(68, 135, 283, 13);
  contentPane.add(lblEnterANumber);
     lblEnterANumber.setHorizontalAlignment(lblEnterANumber.CENTER);
 }
 private class SwingAction extends AbstractAction {
  public SwingAction() {
   putValue(NAME, "Check");
   putValue(SHORT_DESCRIPTION, "Starts the calculation");
  }
  public void actionPerformed(ActionEvent e) {
   String textFieldValue = textField.getText() + "l";
   long num1 = Long.valueOf(textFieldValue).longValue();
   
   if(isNumber(textFieldValue) ){
    display(num1);
   }else{
    lblEnterANumber.setText("This is a too big number or String data.");
   }
   
  }

     private void display(long Num){
      if( isPrimeNum( Num ) ) {
       lblEnterANumber.setText( Num + " is a prime number." );
      } else{
       lblEnterANumber.setText( Num +" isn't a prime number.");
      }
     }

  private boolean isPrimeNum( long x ) {
      if( x == 2 )
      return true;

      if( x < 2 || x % 2 == 0 )
      return false;

      for( long a = 3; a <= Math.sqrt((double)x); a += 2 )
      if( x % a == 0 )
      return false;
 
      return true;
  }
 
  public boolean isNumber(String val) {
   try {
    Integer.parseInt(val);
    return true;
   } catch (NumberFormatException nfex) {
    return false;
      }
  }
 }
}