Your mathematics professor would like you to write a simple program for her, cal
ID: 3676358 • Letter: Y
Question
Your mathematics professor would like you to write a simple program for her, called PrimeDecomposer. This program should determine whether a positive integer is a prime number or a composite number. As you recall, a prime number is a number that can only be divided by itself or by 1. If a number is not prime, than it is referred to as a composite number, which means it is the product of at least two other numbers. The prime decomposition of a number is the expression that shows all of the individual prime factors of a number. For instance, the prime decomposition of 105 is 3, 5 and 7. In other words, 105 = 3 X 5 X 7 where 3, 5, and 7 are all prime. Your professor wants the program to prompt the user for a positive integer, and then display the results, EXACTLY as shown below.
Your entered number, 105, is not prime.
Its prime decomposition is 3 X 5 X 7
OR
Your entered number, 41, is prime.
You must use the JOptionPane Class to display all messages and user input. The program should loop until the user enters a negative 1 (-1). This means that the program cycles back to the prompt without asking if the user wants to continue. The user indicates they want to quit by entering a -1 in the number entry JOptionPane or b You must exit the program if the X is clicked or if the cancel button is clicked. You must provide data validation to make sure an empty string is not entered. If an empty string is entered, you are to cycle back to the user prompt.
I need it done very simply in JAVA so I can try to understand what is going on.
Thank you so much.
This is what I have so far...I am stuck on the while loops and if else statements... can someone please help?
public class PrimeDecomposer {
public static void main(String[] args) {
String intro = "This program will ask the user to enter a positive integer. " +
"If the number is not prime, this program will display the prime decompostion of the number. " +
"If the number is prime, the program will say the entered number is prime " +
"Finally, this program will display a termination message and procede to terminate. "
+ "To exit enter -1";
JOptionPane.showMessageDialog(null, intro, "Prime Decomposer Program", 1);
String request = "Please enter a positive integer";
String integerEntry = JOptionPane.showInputDialog(null, request, "Integer Entry", 1);
int userInt = Integer.parseInt(integerEntry);
if (userInt == -1)
{
JOptionPane.showMessageDialog(null, "Program terminating!");
System.exit(0);
}
//Formula
String request2 = "";
JOptionPane.showMessageDialog(null, request2, "", 1);
String request3 = "This program is now terminating!";
JOptionPane.showMessageDialog(null, request3, "Program Terminating", 1);
}
}
Explanation / Answer
import java.util.Scanner; public class PrimeN { public static void main(String[]args); { String data = JOptionPane.showInputDialog("Enter a prime or composite number"); int n=Integer.parseInt(data); int sum = 0; //Enter number and sentiel { if ((n%n=0) && (n>=1)); { System.out.println("The number is prime"); } else { System.out.println("The number is composite") //Dechipher if number is composite or prime } while (number !=0)//Loop { sum += number; data = JOptionPane.showInputDialog("Enter a prime or composite number (Enter 0 to Quit)"); number = Integer.parseInt(data); } } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.