Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have developed a method (DetermineIfPrime) that determines if a number is prime public static boolean determineIfPrime(int valuePassed) { boolean isPrime = true; if(valuePassed <

I have developed a method (DetermineIfPrime) that determines if a 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
I have also developed a method that will determine if a number is a palindrome (the same forward and backwards): 

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

You may use the methods above in your code (just copy and past).

Assignment:

1. 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. 3, 7, 31, 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago