Results 1 to 6 of 6

Thread: A prime number applet

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    2

    A prime number applet

    Hey,
    I need to transform this into an applet. I'm having a little trouble doing it. Please help soon. Thanks a ton!!!

    import java.awt.Container;
    import javax.swing.*;
    class JavaPage944 {
    public static void main(String arguments[]) {
    int[] primes = new int[400];
    int numPrimes = 0;
    int x = 2;
    while (numPrimes < 400) {
    if (isPrime(x)) {
    primes[numPrimes] = x;
    numPrimes++;
    }
    x++;
    }
    System.out.println("First 400 primes:");
    for (int i = 0; i < 400; i++)
    System.out.print(primes[i] + " ");
    }

    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;
    }
    }

  2. #2
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    I notice that you are declaring swing on line two, but not using it. Are you planning to use swing in your applet? One problem with Internet Explorer (5.x, 6.0) straight out of the box is that it will only work with JDK 1.1, but the current is JDK 1.3 and soon JDK 1.4. JDK 1.1 is pre-swing. You can use swing with IE, but in order for it to work as an applet, you have to go to Sun and download the 'Java Runtime with plugin.'

    If you are making a general-purpose applet that anyone on the Internet can use, you will need to work with an older JDK. Sun offers this on their website. I think it's JDK 1.1.8. This will work with the latest versions of IE, but it doesn't have swing.

    cudabean

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Applets arent my forte but im pretty sure you have to scrap the main method. Change your class header to class JavaPage extends Applet {. And override public void init(){} for any initialization you might want to do, and override public void start(){} also. Then within your html code you have too add the Applet tag like the following.

    Code:
    <HTML>
      <HEAD> 
        <TITLE>PRIMES</TITLE>
      </HEAD>
    
      <BODY BGCOLOR="BLACK">
       <APPLET codebase="C:/Java" code ="JavaPage.class" width=500 height=500>
         
       </APPLET>
      </BODY>
    </HTML>

  4. #4
    Hyperactive Member Pix's Avatar
    Join Date
    Feb 2001
    Location
    I'm not telling you (or them)
    Posts
    282
    Like the lava lamp

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Thanks. It gives me somthing soothing to look at while i give my self a headache trying to figure out all of this programming stuff.

  6. #6
    Hyperactive Member Pix's Avatar
    Join Date
    Feb 2001
    Location
    I'm not telling you (or them)
    Posts
    282
    cool

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width