Question: Java Programing!!! Write a program (TypesOfPrimes.java) that has methods as follows (the methods you develop below should call the methods above as appropriate): //determines if
Java Programing!!!
Write a program (TypesOfPrimes.java) that has methods as follows (the methods you develop below should call the methods above as appropriate):
//determines if a number is a Sophie Germaine Prime (a number n such that n and 2 * n + 1 are both prime) public static boolean isSophieGermainPrime(int n) e.g 2, 23, 89
//determines if a number is a Twin Prime (a number n such that n and n + 2 are both prime) public static boolean isTwinPrime(int n) e.g 3,11,17
//determines if a number is prime and also palindromic (same forward and backwards) public static boolean isPalPrime(int n) e.g 11, 101
//determines if the number passed is an emirp (a nonpalindromic prime number whose reverse is also prime) public static boolean isEmirp(int n) e.g. 13
//determines if the number passed is a Mersenne Prime (a prime number n such that 2n - 1 is also prime) public static boolean isMersenne(int n) e.g. e.g. 2, 3, 13, 127
2. Write another program (name it PrimeMethodsDemo.java) . Within a main method get an int from the user and pass it to the five methods above and print to the screen an appropriate messages given the value returned by the method called.
Determines if number is prime
public static boolean determineIfPrime(int valuePassed) { boolean isPrime = true; if(valuePassed < 2) isPrime = false; else if(valuePassed == 2) isPrime = true; else for(int i = 2; i < Math.sqrt(valuePassed) + 1; ++i) { if(valuePassed % i == 0) return false; }//end for loop return isPrime; }//end method
Determines if number is a palindrome
public static boolean testIfPalindromeNumber(int n) { String originalString = Integer.toString(n); String reverseString = ""; for(int i = originalString.length() - 1; i >= 0; --i) {//open for loop reverseString = reverseString + originalString.charAt(i); }//end for loop return originalString.equals(reverseString); }//end testIfPalindromeNumber
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
