DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: perfect numbers

  1. #1
    Join Date
    Sep 2005
    Posts
    8

    Exclamation perfect numbers

    this is a class to determine the perfect numbers between 1 and 100..
    public class Perfect {

    public static void main(String[] args) {
    // Perfect numbers

    for ( int i=2; i<=1000; i++ )
    {
    int sum = 0;
    for ( int j=1 ; j<i ; j++ )
    {
    if ( i % j == 0 )
    {
    sum = sum + j;
    }
    }
    if ( sum == i )
    {
    System.out.println("" + i + " is a perfect number" );
    System.out.print("Factors are: " );
    for ( int f=1; f<i ; f++ )
    {
    if ( i % f == 0 )
    {
    System.out.print(" " + f );
    }
    }
    System.out.println();
    }
    }
    }
    }

    how can i do it as an applet using JTextArea??
    plz anyone reply me soooooon

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    extend JApplet from your Perfect class.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Like this

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Perfect
        extends JApplet
        implements ActionListener {
      JScrollPane jScrollPane1 = new JScrollPane();
      JPanel ctrlPanel = new JPanel();
      JButton ppnBtn = new JButton();
      JTextArea reportTA = new JTextArea();
      JButton clearBtn = new JButton();
    
      public Perfect() {}
    
      public void init() {
        try {
          jbInit();
          // hook up buttons
          this.ppnBtn.addActionListener(this);
          this.clearBtn.addActionListener(this);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
      /**
       * Print perfect numbers to textarea
       */
      private void printPerfectNumbers() {
        for (int i = 2; i <= 1000; i++) {
          int sum = 0;
          for (int j = 1; j < i; j++) {
            if (i % j == 0) {
              sum = sum + j;
            }
          }
          if (sum == i) {
            reportTA.append("" + i + " is a perfect number\n");
            reportTA.append("Factors are: ");
            for (int f = 1; f < i; f++) {
              if (i % f == 0) {
                reportTA.append(" " + f);
              }
            }
            reportTA.append("\n");
          }
        }
      }
      /**
       * Set up GUI
       */
      private void jbInit() throws Exception {
        ppnBtn.setText("Print Perfect Numbers");
        reportTA.setText("");
        clearBtn.setText("Clear");
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        this.getContentPane().add(ctrlPanel, BorderLayout.SOUTH);
        ctrlPanel.add(ppnBtn, null);
        ctrlPanel.add(clearBtn, null);
        jScrollPane1.getViewport().add(reportTA, null);
      }
      /**
       * Handle button clicks
       */
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == ppnBtn) {
          printPerfectNumbers();
        }
        else if (e.getSource() == clearBtn) {
          reportTA.setText("");
        }
      }
      /**
       * MAIN for testing applet (uses the applet as a JPanel)
       */
      public static void main(String[] args) {
        JFrame f=new JFrame("Applet Test");
        f.getContentPane().setLayout(new GridLayout());
        Perfect applet=new Perfect();
        f.getContentPane().add(applet);
        applet.init();
        f.setBounds(20,20,300,200);
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        f.setVisible(true);
      }
    }
    eschew obfuscation

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2006, 02:14 AM
  2. Random Numbers on button
    By daina in forum Java
    Replies: 3
    Last Post: 10-12-2005, 01:35 AM
  3. Replies: 0
    Last Post: 05-27-2005, 01:23 AM
  4. Rounding numbers
    By Big Swifty in forum Java
    Replies: 3
    Last Post: 11-10-2002, 06:33 AM
  5. generating unique random numbers
    By Dave in forum Java
    Replies: 3
    Last Post: 05-14-2002, 08:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links