Need this in Java all code please: for both the class MyMath and MyMathTest will
ID: 3683503 • Letter: N
Question
Need this in Java all code please: for both the class MyMath and MyMathTest
will rate promptly
Create a class MyMath with a static method isPerfect() and a static method isPrime(). Each method has a int parameter and returns a boolean value. Create an application MyMathTest that displays a menu allowing the user to choose whether to display the prime numbers 1-1000, display the perfect numbers 1-10,000, or quit. Be sure to clearly label the output. Output the prime numbers 10 to a line. Use dialog boxes for ALL input and output.
Explanation / Answer
PROGRAM:
public class MyMath
{
public static boolean isPrime (long number)
{
boolean prime = true;
int counter = 2;
if (number <= 1)
{
prime = false;
}
else if (number == 2)
prime = true;
else
{
while (counter <= Math.sqrt(number))
{
if (number % counter == 0)
prime = false;
counter++;
}
}
return prime;
}
public static boolean isMersennePrime (long number)
{
boolean mersenne = false;
if (isPrime(number))
{
mersenne = true;
while (number <= (long)(Math.pow(2, number)) - 1);
{
number++;
}
}
return mersenne;
}
public static boolean isFermatPrime (long number)
{
boolean fermat = false;
if (isPrime(number))
{
fermat = true;
while (number <= (long)(Math.pow(2, Math.pow(2, number))) - 1);
{
number++;
}
}
return fermat;
}
}
Here's the MyMathTest Program that suppose to make call the methods from MyMath to display the numbers:
import javax.swing.JOptionPane;
public class MyMathTest
{
public static void main(String[] args)
{
String menuChoice = "1: Display the Prime Numbers 1 - 1000 " +
"2: Display the Mersenne Prime Numbers 1 - 10,000 " +
"3: Display the Fermat Primes 1 - 1000 " +
"4: Quit ";
int userInput = 0;
do
{
String numberChoice = JOptionPane.showInputDialog(null, menuChoice);
userInput = Integer.parseInt(numberChoice);
switch (userInput)
{
case 1:
String displayPrime = "The Prime Numbers from 1 through 1,000: ";
int outputLine = 1;
for (int prime = 1; prime <= 1000; ++prime)
{
if (MyMath.isPrime(prime))
{
displayPrime += String.format ("%d ", prime);
if (outputLine == 10)
{
displayPrime += " ";
outputLine = 0;
}
++outputLine;
}
}
JOptionPane.showMessageDialog(null, displayPrime);
break;
case 2:
String displayMersenne = "The Mersenne Prime Numbers from 1 through 10,000: ";
for (int mersenne = 1; mersenne <= 10000; mersenne++)
{
if (MyMath.isMersennePrime(mersenne))
{
displayMersenne += String.format ("%d ", mersenne);
}
}
JOptionPane.showMessageDialog(null, displayMersenne);
break;
case 3:
String displayFermat = "The Fermat Prime Numbers from 1 through 1,000: ";
for (int fermat = 1; fermat <= 1000; fermat++)
{
if (MyMath.isFermatPrime(fermat))
{
displayFermat += String.format ("%d ", fermat);
}
}
JOptionPane.showMessageDialog(null, displayFermat);
break;
case 4:
break;
default:
}
}
while (userInput != 4);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.