-
Extra Code
Need some help. I want to have this FindPrimes program to be able to redo its sunction without having to restart the program. Here is the text of the program. Esentaially what I am asking for is a addition to this program that will erase the textbox and allow the user to enter a quantity and have the program display the prime numbers.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class FindPrimes extends JFrame implements Runnable, ActionListener {
Thread go;
JLabel howManyLabel = new JLabel("Quantity: ");
JTextField howMany = new JTextField("400", 10);
JButton display = new JButton("Display Primes");
JTextArea primes = new JTextArea(8, 40);
FindPrimes() {
super("Find Prime Numbers");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
BorderLayout bord = new BorderLayout();
content.setLayout(bord);
display.addActionListener(this);
JPanel topPanel = new JPanel();
topPanel.add(howManyLabel);
topPanel.add(howMany);
topPanel.add(display);
content.add(topPanel, BorderLayout.NORTH);
primes.setLineWrap(true);
JScrollPane textPane = new JScrollPane(primes);
content.add(textPane, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
display.setEnabled(false);
if (go == null) {
go = new Thread(this);
go.start();
}
}
public void run() {
int quantity = Integer.parseInt(howMany.getText());
int numPrimes = 0;
// candidate: the number that might be prime
int candidate = 2;
primes.append("First " + quantity + " primes:");
while (numPrimes < quantity) {
if (isPrime(candidate)) {
primes.append(candidate + " ");
numPrimes++;
}
candidate++;
}
}
public static boolean isPrime(int checkNumber) {
double root = Math.sqrt(checkNumber);
for (int i = 2; i<= root; i++) {
if(checkNumber % i == 0)
return false;
}
return true;
}
public static void main(String[] arguments) {
FindPrimes fp = new FindPrimes();
}
}
Thanks for any help you can give
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks