Computer Science 523
Advanced Programming
Summer 2014, The College of Saint Rose
PrimeFactors Demo
A working demo of PrimeFactors will appear below. Click inside the applet to interact with it.
PrimeFactors BlueJ Project
Click here to download a BlueJ project for PrimeFactors.
PrimeFactors Source Code
The Java source code for PrimeFactors is below. Click on a file name to download it.
/* * Example PrimeFactors: use a JApplet to compute prime factorizations * * Jim Teresco, The College of Saint Rose, CSC 523, Summer 2014 * * $Id: PrimeFactors.java 2379 2014-06-17 03:52:55Z terescoj $ */ import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class PrimeFactors extends JApplet implements ActionListener { // need to remember our JTextField for use in the event handler private JTextField numberField; // and we'll be updating this JLabel with results private JLabel factorLabel; public void init() { Container contentPane = getContentPane(); setSize(500, 150); // set up our dialog window JPanel topPanel = new JPanel(); topPanel.add(new JLabel("Number to factor: ")); // create a text field with 10 visible characters numberField = new JTextField("", 10); topPanel.add(numberField); numberField.addActionListener(this); JButton button = new JButton("Factor It!"); topPanel.add(button); button.addActionListener(this); contentPane.add(topPanel, BorderLayout.NORTH); factorLabel = new JLabel("Results will appear here."); contentPane.add(factorLabel, BorderLayout.CENTER); contentPane.validate(); } // a helper method that clears the field and resets the label private void reset() { factorLabel.setText("Results will appear here."); numberField.setText(""); } // handle the button press OR enter in the text field // but since we do the same thing in either case, we // don't care which one triggered it public void actionPerformed(ActionEvent e) { // first, we'll try to convert the String in the text field to an int, // and if the conversion fails we'll get a NumberFormatException thrown int number; String s = numberField.getText(); try { number = Integer.parseInt(s); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Could not convert " + s + " to an integer.", "Error", JOptionPane.ERROR_MESSAGE); reset(); return; } // so we have a number -- is it factorable? if (number < 2) { JOptionPane.showMessageDialog(null, "Number must be at least 2.", "Error", JOptionPane.ERROR_MESSAGE); reset(); return; } // it is a good one - let's factor it in a straightforward way ArrayList<Integer> factors = new ArrayList<Integer>(); int remaining = number; // factor the 2's while (remaining % 2 == 0) { factors.add(2); remaining /= 2; } // now try odd factors -- we could try only primes but it's probably // harder to figure out the primes than to just try the odd non-primes for (int tryFactor = 3; tryFactor <= remaining; tryFactor += 2) { while (remaining % tryFactor == 0) { factors.add(tryFactor); remaining /= tryFactor; } } // so we have an ArrayList of the factors, let's put them into our label factorLabel.setText("Prime factors of " + number + " are: " + factors); numberField.setText(""); } }