Laboratory 8: Prime Number Finder

March 28, 29, 2006

Bring your Java, Java, Java text to the laboratory.

Objectives

The objectives of this laboratory are
  1. To give practice designing and writing a simple Java program.
  2. To give practice using for and while loops.
  3. To give practice using basic arithmetic and relational operators.
In preparation for this lab you should review Chapter 6 of Java, Java, Java.

Problem statement

Suppose that Atlas Computer Security has contacted you about consulting for them to develop public key encryption software. One thing you're going to need in this software is a method that can test whether a number is a prime or not. To help demonstrate your prime number tester, the company wants you to write a Java applet that prompts the user for a positive integer and then displays all of the prime numbers less than or equal to the user's integer. If they like your demo, they will probably give you the contract for the encryption software.

Sample Solution

Prime Finder Applet

Problem Decomposition

The GUI for this project should resemble the GUI you developed for last week's lab. In fact, you can copy last week's java files into this week's folder and modify them. So copy LeapYearApplet.java and LeapYearGUIPanel.java to this week's folder and modify these files (including comments, variable names, prompts, etc.) so that they are appropriate for this week's problem. The computational object for this week's problem can be defined in the PrimeFinder class, which has the design shown in the following UML diagram.
PrimeFinder


+ PrimeFinder()
+ getPrimesLessThanN(n: int): String
+ isPrime(n: int): boolean

As we have done in previous programs, the applet will prompt the user for an integer. It will then pass that integer to the PrimeFinder's getPrimesLessThanN() method, which will return a String giving all of the prime numbers less than the user's number. The applet will display these numbers in its text area. (Note: It is important to insert tabs ("\t") and new lines ("\n") into the returned string so that the output is formatted in the text area.)

Note that the PrimeFinder class contains the a helper method named isPrime(). This method can be called by getPrimesLessThanN() to test whether a given integer is or is not a prime.

Implementation

Step 1. Set Up.

Copy and rename the following files from your lab7 folder into this week's lab8 folder:

Step 2. Adapting the Applet and Panel Classes.

As you did last week, modify both PrimesApplet and PrimesGUIPanel so they interface with the PrimeFinder class and have the appropriate components and documentation.

Step 3. Designing the Loop Algorithms.

Hand In.

Before handing in your programs, be sure to document your source code. Every method must have a comment block that describes the method, its parameters, and its return value.

Have your work checked by the laboratory instructor or TA. Print out and hand in a copy of your PrimesGUIPanel.java and PrimeFinder.java source code.

You're done. Great work!