I need help writing this program in JAVA, this is an introductory java course, s
ID: 3817750 • Letter: I
Question
I need help writing this program in JAVA, this is an introductory java course, so if possible, keep it as basic/simple as possible while still following the instructions. The output should look like the sample execution at the end of the problem.
Write a menu-driven program which should loop the menu continuously until the user chooses to exit the program. The menu should have the following options:
1. Display Prime Palindromes
2. Display Mirrored Primes
3. Exit Program
Option 1 Prime Palindromes: A prime palindrome is a prime number that is also a palindrome. Example: 131 is a prime number and also a palindrome. When the user chooses Option 1, you should ask them how many prime palindromes to display (n), then display the first n prime palindromes, with 10 numbers per line separated by a single space.
Option 1 Sample:
Option 2 Mirrored Primes: A mirrored prime is a non-palindrome prime whose reverse is also prime. Example, 17 and 71 are primes and mirrors of each other. When the user chooses Option 2, you should ask them how many mirrored primes to display (n), then display the first n mirrored primes, with 10 numbers per line separated by a single space.
Option 2 Sample:
Input Validation:
No input validation required.
Requirements:
To get credit for this assignment you must use a minimum of 5 different methods other than main. In reality you will more than likely need more, but 5 is the minimum.
Explanation / Answer
import java.util.Scanner;
public class Menu
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int userChoice;
boolean quit = false;
do
{
//Menus
System.out.println(" 1. Display Prime Palindromes");
System.out.println(" 2. Display Mirrored Primes");
System.out.println(" 3. Exit Program");
// User Choice Taken from Keyboard
userChoice = sc.nextInt();
switch (userChoice)
{
case 1:
// Prime Palindromes
System.out.println("How many prime palindromes to display?:");
int n=sc.nextInt();
displayPalindrome(n);
break;
case 2:
// Mirrored Primes
System.out.println("How many mirror Primes to display?:");
int i=sc.nextInt();
displayMirror(i);
break;
case 3:
//Exit
quit = true;
break;
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
}
// Method to display prime Palindrome numbers
static void displayPalindrome(int n)
{
for(int num=0;num<=n;)
{
for(int i=1;i<1000;i++)
{
// Prime number checked
if(isPrime(i)==true)
{
//number is palindrome or not checked
if(isPalindrome(i)==true)
{
System.out.println(" "+i+" ");
num++;
}
}
}
}
}
//Method to check number prime or not
static boolean isPrime(int n)
{
for(int i=2; i<=n/2; i++)
{
if(n % i == 0)
{
return false;
}
}
return true;
}
// Method to display Mirrored Primes
static void displayMirror(int i)
{
for(int num=0;num<=i;)
{
for(int j=0;j<100;j++)
{
//Mirrored number and Prime number checked
if(isMirror(j)&&isPrime(j))
{
System.out.println(" "+j+" ");
num++;
}
}
}
}
//Method to check whether no is mirrored or not
static boolean isMirror(int n)
{
boolean t1=false;
int reverse=0;
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
if(isPrime(reverse)&&isPrime(n))
{
t1=true;
}
return t1;
}
// Method to check no is palindrome or not
static boolean isPalindrome(int num)
{
boolean t2=false;
int n = num;
//reversing number
int rev=0,rmd;
while(num > 0)
{
rmd = num % 10;
rev = rev * 10 + rmd;
num = num / 10;
}
if(rev == n)
{
t2=true;
}
return t2;
}
}