The design of PrimesApplet should be similar to that of other applets we've built. Of course, the applet must create an instance of the Primes. This is the object it will call upon to test whether numbers are prime.
In terms of its GUI elements, the applet should contain a TextField for user input and a Label for prompting the user. These components should be instantiated in the applet and added to the applet in the init() method. Because it will generate user actions, whenever the user types the Enter key, the TextField should be given an ActionListener. This should also be done in the init() method.
The algorithm used by PrimesApplet is event driven. In the init() method, the applet should be registered as the listener for ActionEvents that occur in the TextField. Then, whenever such an event occurs, Java will call the applet's actionPerformed() method, where the event should be handled. The actionPerformed() method should get the input from the TextField, convert it to an integer, and then display all of the prime numbers less than the user's number:
1. Get the user's input from the TextField.
2. Convert the input String into an int.
3. Display in the TextArea all the prime numbers less than
the user's number.
Step 2 of this algorithm will require us to use the Integer.parseInt() method to convert a String into an int:
int num = Integer.parseInt("564"); // Converts "564" to 564
Step 3 of this algorithm will require a loop, and it is here where the applet must call on the expertise of the Primes object. Because this step will be somewhat complex, it should be encapsulated into a separate method.
Taken together, these design decisions lead to the following specification for the PrimesApplet class:
Note that the findPrimes() method will be a private method. This is appropriate, since it is not intended to be used outside of this class.