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
- To give practice designing and writing a simple Java program.
- To give practice using for and while loops.
- 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:
- index.html
- LeapYearApplet.java is renamed PrimesApplet.java
- LeapYearGUIPanel.java is renamed PrimesGUIPanel.java
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.
- findPrimesLessThanN() Method This method requires
a loop that will go through all the values less than or equal to
its parameter and test whether it is a prime or not. If it is a
prime, it should append that value to a String that will accumulate
the prime numbers.
Design Questions: Is this a counting or non-counting loop?
What Java loop statement should you use here?
Here's some code to illustrate how to append the values 0..4 to a
String variable using a for-loop.
String s = ""; // Initialize a string to the empty string
for (int k = 0; k < 5; k++)
s = s + k + "\t"; // Append k and a tab to the string
System.out.println(s); // String s now equals 0 1 2 3 4
Note: This is not the most efficient way to append values
to a String. A better approach would use a StringBuffer,
which we'll learn about in Chapter 7.
- isPrime() Method This method requires a loop to test
whether its parameter is a prime or not. To determine whether
N is prime we can attempt to divide N by 2, then 3, then 4, and
so on up to N-1. If N is divisible by any of those values, it is not
prime. Otherwise, it is prime. You should see from this description
that we can stop testing for primality as soon as we find a number
less than N that evenly divides N.
Design Questions: Is this a counting or non-counting loop?
What type of loop should you use? Don't forget every loop must have
an initializer, a loop-entry condition and an
updater that makes progress toward its stopping condition. How
will you represent these?
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!