Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task for Project 2 An Emirp(prime spelled backward) is a nonpalindromic prime nu

ID: 3624260 • Letter: T

Question

Task for Project 2 An Emirp(prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example. 17 is a prime and 71 is a prime. So are 17 and 71 are emirps. Write a program that displays the first 100 emirps. Display 10 numbers per line and align the numbers properly, as follows: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389.. Before submitting the program, you should test and run it. and make sure it works properly. Submit your source code (the file with the extension .java) to the Project 2 Folder. WARNING: NEVER WRITES YOUR SOURCE CODE USING MICROSOFT WORD! Instead, you should use either the editor come with the IDE or notepad. Tips: You can use as examples Solution for exercise 5.26 (Palindromic prime See attachment) and Listing 5.7 PrimeNumberMethod.java Rubric for Project 2 This project has three parts, with 60 total points possible. Carefully declare all the variables with appropriate data types. Use comments effectively to make the program more readable. Implement the calculations correctly. Compile and build the Java program. Test the program and make sure the program produces the desired answer. Submit your source code, UML diagram, Algorithms and output file as an attached files to Project 2 Folder

Explanation / Answer

Algorithm: test numbers until first 100 emirps are found.

public class Emirp
{
public static void main(String[] args)
{
int num = 10; // current number being checked (single digit numbers are palindromes)

int count = 0;

// find first 100 emirps
while(count < 100)
{
if(isEmirp(num))
{
System.out.print(num+" ");
count++;
}

num++;
}
System.out.println();
}

/**
* @return whether the parameter is an emirp
*/

public static boolean isEmirp(int num)
{
return !isPalindrome(num) && isPrime(num) && isPrime(reverse(num));
}

/**
* @return whether the parameter is a palindrome
*/

public static boolean isPalindrome(int num)
{
String temp = ""+num;

// any string of length 0 or 1 is a palindrome
while(temp.length() > 1)
{
// check end characters
if(temp.charAt(0) != temp.charAt(temp.length()-1))
{
return false;
}
// if they match, remove them
temp = temp.substring(1, temp.length()-1);
}

return true;
}

/**
* @return the reversal of a number
*/

public static int reverse(int num)
{
String temp = ""+num;
String temp2 = "";

for(char c : temp.toCharArray())
{
temp2 = c + temp2;
}

return Integer.parseInt(temp2);
}

/**
* @return whether the parameter is a prime number
*/

public static boolean isPrime(int num)
{
// test for factors
for(int i = 2; i <= Math.sqrt(num); i++)
{
// check divisibility
if(num % i == 0)
{
return false;
}
}

return true;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote