Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. public boolean isPrime(long number)
  2.  
  3. {
  4.  
  5. if (number % 2 == 0)
  6. {
  7. JOptionPane.showMessageDialog(null, "It is not a prime");
  8. return false;
  9. }
  10. else
  11. {
  12. long end = (long) Math.sqrt(number) + 1;
  13. for (long divisor = 3; divisor <= end; divisor+=2)
  14. {
  15. if (number % divisor == 0)
  16. {
  17. JOptionPane.showMessageDialog(null, "It is not a prime");
  18. return false;
  19. }
  20. }
  21. JOptionPane.showMessageDialog(null, "It is a prime");
  22. return true;
  23. }
  24.  
  25. }
  26.  
  27. public void primesToANumber() //finds all primes
  28. {
  29.  
  30. }
  31. }
  32.  
  33. public class runner
  34. {
  35. public static void main(String[] args)
  36. {
  37. Primes primechecker = new Primes ();
  38. Primes primesToANumber = new Primes ();
  39. boolean done = false;
  40.  
  41. while (done == false)
  42. {
  43. String[] choices = {"Choose 1 if you want to test if a number is prime or not",
  44. "2",
  45. "3",
  46. "4",
  47. "5",
  48. "6",
  49. "7"};
  50.  
  51. String s = (String)JOptionPane.showInputDialog(
  52. null,
  53. "PLease choose an option", //the question
  54. "Your Choice", //the frame title
  55. JOptionPane.PLAIN_MESSAGE, //the icon type
  56. null, //an icon you select
  57. choices, //array of options
  58. choices[0]); //the default to be selected
  59.  
  60. if(s.equals(choices[0]))
  61. {
  62. String numberString = JOptionPane.showInputDialog("Enter any integer");
  63. long number = Long.parseLong(numberString);
  64. primechecker.isPrime(number);
  65. }
  66.  
  67. else if(s.equals(choices[1]))
  68. {
  69. String numberString1 = JOptionPane.showInputDialog("Enter any integer");
  70. long num = Long.parseLong(numberString1);
  71. primesToANumber.primesToANumber(num);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement