1 import java.applet.*;
    2 import java.awt.*;
    3 import java.awt.event.*;
    4 import java.util.StringTokenizer;
    5 
    6 /**
    7  * Applet to find prime factorization of an integer
    8  * Creation date: (01/29/2002 9:45:40 AM)
    9  * Modified date: (04/15/2002 2:52:00 PM)
   10  * @author: Andrew Freed, arf132@psu.edu
   11  */
   12 
   13 /*
   14 Development of this applet was sponsored by the Penn State Fund for 
   15 Excellence in Learning and Teaching (FELT), project "Java-based 
   16 Teaching of Mathematics in Information Sciences and Technology",
   17 supervised by Frank Ritter and David Mudgett.
   18 */
   19 
   20 public class FactorizerApplet extends Applet {
   21   private Button btnGo          = new Button();
   22   private Label lblAnswer       = new Label("",Label.CENTER);
   23   private Label lblInstruction  = new Label("Number to be factored:");
   24   private TextField txtStartNum = new TextField("",10);
   25   private ScrollPane pneDisplay = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
   26 
   27   //The first 669 primes, which go up to 4999
   28   private int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
   29  89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
   30  181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271,
   31  277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
   32  383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479,
   33  487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
   34  601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
   35  709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
   36  827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
   37  947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049,
   38  1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
   39  1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249,
   40  1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361,
   41  1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459,
   42  1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559,
   43  1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657,
   44  1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759,
   45  1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877,
   46  1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997,
   47  1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089,
   48  2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213,
   49  2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311,
   50  2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411,
   51  2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543,
   52  2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663,
   53  2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741,
   54  2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851,
   55  2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969,
   56  2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089,
   57  3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221,
   58  3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331,
   59  3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461,
   60  3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557,
   61  3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671,
   62  3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779,
   63  3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907,
   64  3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013,
   65  4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129,
   66  4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243,
   67  4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363,
   68  4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493,
   69  4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621,
   70  4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
   71  4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871,
   72  4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973,
   73  4987, 4993, 4999};
   74   private int primeArraySize = primes.length;
   75 
   76 
   77 /**
   78  * Function that will factor a given integer
   79  */
   80     String getFactorization(int myNum)
   81     {
   82   //Error on negative input
   83   if( myNum <= 0 )
   84     return "Please select a positive integer to factor";
   85 
   86       String result = new String();
   87   result += (intToString(myNum) + " = ");
   88 
   89   //Cycle over all the primes in the list.
   90   //Note that we don't have to check primes after a certain point
   91   // myNum > primes[idx]*primes[idx] tells us the maximum prime we must check against
   92   int idx=0;
   93   int numFactors=0;
   94   while( myNum >= 1 && idx < primeArraySize && (myNum > primes[idx]*primes[idx]))
   95   {
   96     while( myNum % primes[idx] == 0 )
   97     {
   98       //Separate this into a function later
   99       result += (intToString(primes[idx]) + " * ");
  100       myNum /primes[idx];
  101       numFactors++;
  102     }
  103     idx++;
  104   }
  105   
  106   //Add on the last, unfactorable part.  If no factors, precede this with "1 *"
  107   if( myNum != 1 )
  108   {
  109     if( numFactors == 0 )
  110       result += "1 * ";
  111     result += intToString(myNum);
  112   }
  113   else
  114   {
  115     //Get rid of last ' * '
  116     result = result.substring(0, result.length() - 3);
  117   }
  118 
  119       return result;
  120     }
  121 
  122 //This method takes an integer value and converts it to a string with commas placed
  123 //every three digits from the right.
  124 private String intToString(int myInt)
  125 {
  126   String result = "";
  127   String buf = new Integer(myInt).toString();
  128   boolean isNegative = false;
  129   int numCommas = 0;
  130 
  131   if( myInt < 0 )
  132   {
  133     //Truncate sign
  134     isNegative = true;
  135     buf = buf.substring(1);
  136   }  
  137 
  138   int len = buf.length();
  139 
  140   //Short case, no commas
  141   if( len <= 3 )
  142   {
  143     if( isNegative )
  144       result = "-" + buf;
  145     else
  146       result = buf;
  147     
  148     return result;
  149   }
  150   
  151   //Otherwise place the first comma from the *left*
  152   //At each step, remove from the buffer what has been added to the result
  153   int firstComma = len % 3;
  154   if( firstComma == 0 )
  155   {
  156     firstComma += 3;
  157     result = buf.substring(0, 3) + ",";
  158     buf = buf.substring(3);
  159   }
  160   else
  161   {
  162     result = buf.substring(0, firstComma) + ",";
  163     buf = buf.substring(firstComma);
  164   }
  165   numCommas++;
  166 
  167   // Truncation of integer division on purpose -- 4, 5, 6 digit numbers each have the
  168   // same number of commas
  169   while( numCommas < ((len-1) /3) )
  170   {
  171     result += buf.substring(0, 3) + ",";
  172     buf = buf.substring(3);
  173     numCommas++;
  174   }
  175   result += buf;
  176 
  177   //Re-attach the sign if necessary
  178   if( isNegative)
  179     result = "-" + result;
  180 
  181   return result;
  182 }
  183 
  184 //Reads text from a text field while ignoring whitespace and commas
  185 //Our use will be to return a string that contains a number
  186 private String getText(TextField myText)
  187 {
  188   String result = "";
  189   String buf = myText.getText();
  190 
  191   //Want to pull out commas and spaces from text box
  192   StringTokenizer st = new StringTokenizer(buf, " ,\t");
  193 
  194   while( st.hasMoreElements() )
  195     result += st.nextToken();
  196 
  197   return result;
  198 }
  199 
  200 
  201 /**
  202  * Initializes the applet.
  203  */
  204 public void init() {
  205   try {
  206     super.init();
  207     setName("FactorizerApplet");
  208     setLayout(null);
  209     setSize(426, 240);
  210       pneDisplay.setBounds(15, 65, 398, 164);
  211       lblInstruction.setBounds(22, 18, 139, 23);
  212       txtStartNum.setBounds(173, 17, 100, 29);
  213       btnGo.setBounds(298, 21, 56, 23);
  214       btnGo.setLabel("Go!");
  215       btnGo.setActionCommand("btnGo");
  216       btnGo.addActionListener(new java.awt.event.ActionListener(){
  217         public void actionPerformed(java.awt.event.ActionEvent e)
  218         {
  219           makeCall();
  220         }
  221       });
  222       txtStartNum.addActionListener(new java.awt.event.ActionListener(){
  223         public void actionPerformed(java.awt.event.ActionEvent e)
  224         {
  225           makeCall();
  226         }
  227       });
  228     add(lblInstruction);
  229     add(txtStartNum);
  230     add(btnGo);
  231     pneDisplay.add(lblAnswer);
  232     add(pneDisplay);
  233 
  234     btnGo.requestFocus();
  235     txtStartNum.requestFocus();
  236   } catch (java.lang.Throwable Exc) {
  237     handleException(Exc);
  238   }
  239 }
  240 
  241     //Interface function between buttons and actual code
  242     //Retrieves value from textbox and sends proper value to code
  243     private void makeCall()
  244     {
  245   try{
  246     String strInput = getText(txtStartNum);
  247     txtStartNum.setText("");
  248         
  249         if( strInput == null )
  250           lblAnswer.setText("Please type in number to factor first");
  251         else
  252           lblAnswer.setText(getFactorization(Integer.parseInt(strInput)));
  253 
  254     //Very odd... I can't get the scrollbars to show up
  255     //until I do this!
  256     pneDisplay.doLayout();
  257   }
  258   catch(java.lang.NumberFormatException nfe)
  259   {
  260       lblAnswer.setText("Error: Please choose a positive number less than 2,147,483,647");
  261   }
  262     }
  263 
  264     
  265 /**
  266  * Called whenever the part throws an exception.
  267  * @param exception java.lang.Throwable
  268  */
  269 private void handleException(java.lang.Throwable exception) {
  270   //Currently exceptions are not handled since the applet does not have
  271   //stdout or stderr available to it.
  272 }
  273 
  274 // This main function allows you to run this applet as an application
  275 /**
  276  * main entrypoint - starts the part when it is run as an application
  277  * @param args java.lang.String[]
  278  */
  279 public static void main(java.lang.String[] args) {
  280   try {
  281     Frame frame = new java.awt.Frame();
  282     FactorizerApplet aFactorizerApplet;
  283     Class iiCls = Class.forName("FactorizerApplet");
  284     ClassLoader iiClsLoader = iiCls.getClassLoader();
  285     aFactorizerApplet = (FactorizerApplet)java.beans.Beans.instantiate(iiClsLoader,"FactorizerApplet");
  286     frame.add("Center", aFactorizerApplet);
  287     frame.setSize(aFactorizerApplet.getSize());
  288     frame.addWindowListener(new java.awt.event.WindowAdapter() {
  289       public void windowClosing(java.awt.event.WindowEvent e) {
  290         System.exit(0);
  291       };
  292     });
  293     frame.show();
  294     java.awt.Insets insets = frame.getInsets();
  295     frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
  296     frame.setVisible(true);
  297   } catch (Throwable exception) {
  298     System.err.println("Exception occurred in main() of java.applet.Applet");
  299     exception.printStackTrace(System.out);
  300   }
  301 }
  302 
  303 }
  304 
HTML generated by Java2Html, © 2000 Think Tank Ltd, Douglas, Isle Of Man. All rights reserved