// a simple factor demonstration
// John Sklar, CED 512 - fall, 2k   
   import java.applet.Applet;
   import java.awt.*;
   import java.awt.event.*;
   public class FactorMe extends Applet implements ActionListener
   {
      TextField target;
      Button Factor = new Button ("Factor");
      Label answer, Banner;
   
      public void init()
      {
         Font bigFont = new Font("TimesRoman", Font.ITALIC, 24);
         Font miniFont = new Font("TimesRoman", Font.ITALIC, 15);
         Font opFont = new Font("Courier", Font.BOLD, 24);
      
         target = new TextField(10);
      
         Banner = new Label ("Enter a number to be factored");
         answer = new Label("                                                                                                                       ");
      
         Factor.setFont(opFont);
         answer.setFont(miniFont);
         Banner.setFont(bigFont);
         target.setFont(opFont);
      
         add (Banner);
         add (target);
         add (Factor);
         add (answer);
         Factor.addActionListener(this);         
         target.addActionListener(this);
         setBackground(Color.white);
      }
      public void actionPerformed (ActionEvent e)
      {
         String facts = new String(); 
         boolean prime=true;
         int the_target;       
      
         if (Factor.getLabel()=="Factor")
         {
            the_target=Integer.parseInt(target.getText());       
            if(the_target < 1){
               Banner.setText (" Sorry...");
               facts = "     This program only factors positive integers!";
            }
            else {	
               facts += "1";
               for(int index=2;index < the_target;++index)
                  if(the_target%index==0){
                     prime=false;
                     facts += ", " + index;
                     Banner.setText ("               The Factors of");
                  }
               if(prime){
                  Banner.setText (" Please Note: ");
                  if(the_target==1)
                     facts = "             One is neither prime nor composite!";
                  else
                     facts = "             The number " + the_target + " is Prime!";
               }
               else
                  facts += ", " + the_target; // add the number itself to the string
            } 
            answer.setText (facts); 
            Factor.setLabel(" Clear ");
            target.setEnabled(false);// no typing now
         }
         else{ 
            Banner.setText ("Enter a number to be factored");
            target.setText("");
            answer.setText ("");
            Factor.setLabel("Factor");
            target.setEnabled(true);// ok, you can type
         }
      }
   }