For this assignment, you are to write a GUI program that does the following: Cre
ID: 3677745 • Letter: F
Question
For this assignment, you are to write a GUI program that does the following:
Creates a GUI with a label and associated text field. In the text field, a user will
enter a number. Your program should display an error message in the text field
described in 2) if the user does not input a valid number.
The GUI should also have a text field to output. After the “Calculate” button is
selected, the program will determine if the number input in the text field in 1) is prime or not. If it is prime, this text field should be updated to state the input number and the text “ is prime “ or “ is not prime” as appropriate.
a. Important: Don’t worry about doing sophisticated methods to determine if a number is prime or to generate the prime factors. A brute force method is ok. Note the definition of a prime number: A Prime number is prime if it is only evenly divisible by 1 or itself. Also note that 1 is not a prime number.
You GUI needs to include Calculate and Cancel buttons. When calculate is pressed, the program should read the text in 1), do the prime number determination, and update the output text field (2) with the appropriate text. When cancel is pressed, your program should gracefully terminate.
If you have questions on this assignment, ask in the “Ask the Instructor” discussion board. Do not make assumptions that may cost you points!
Please make sure you comment your code thoroughly. My preferred style is to not comment every line of code, but instead have a block comment for every logical section and to use line comments when you want to emphasize something special that line of code does.
The code should be nicely formatted and should use proper variables and variable names. While we’ve sometimes jokingly used wacko variable names in class, please do not do so in your assignment
Bonus points:
a) 4 bonus if you implement the following enhancement:
The GUI should also have two radio buttons:
Prime Number?
Prime Factors?
Your program will behave differently based on whether the Prime Number or
Prime Factor option is selected.
When the Prime Factor option is selected, the output text field should be
updated to list the prime factors for the number.
Explanation / Answer
//find the following program for required output:-
//save it with PrimeGui.java
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.*;
import javax.swing.*;
public class PrimeGui extends JFrame implements TextListener,ActionListener
{
JLabel l1,l2;
TextField t1;
JButton submit,cancel;
String numstr;
int num,ctr = 0,n;
JColorChooser tcc;
public PrimeGui()
{
super(" Prime Numbers Verifier ");
setLayout(new FlowLayout());
l1 = new JLabel(" Enter A Number ");
add(l1);
t1 = new TextField(10);
add(t1);
submit = new JButton(" Submit ");
submit.setMnemonic(KeyEvent.VK_S);
submit.setFocusPainted(true);
add(submit);
cancel = new JButton (" cancel ");
cancel.setOpaque(true);
add(cancel);
t1.addTextListener(this);
submit.addActionListener(this);
cancel.addActionListener(this);
}
public void textValueChanged(TextEvent te)
{
}
public void actionPerformed(ActionEvent ae )
{
if(ae.getSource() == cancel)
{
System.exit(0);
}
if(ae.getSource() == submit)
{
ctr=0;
numstr = t1.getText();
num = Integer.parseInt(numstr);
for (n=num;n>=1;n--)
{
if (num % n == 0)
{
ctr++;
}
}
if ( ctr <=2 )
{
l1.setText(num+" is a prime number");
}
else
{
l1.setText(num+" is not a prime number");
}
}
}
public static void main (String args[])
{
PrimeGui ob = new PrimeGui();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(230,100);
ob.setLocation(350,300);
ob.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.