The goal of this lab is to practice creating and using basic arrays.
The problem is accompanied by a series of hints. Do not look at the hints until and unless you need them. The hints get more specific along the way, so if you don't know where to start, hint 1 will give you a few questions to think about that will hopefully push you in the right direction; hint 2 will suggest a direction to try but won't give any details; hint 3 will clarify hint 2; you get the idea.
While you write these programs, practice the documentation procedures you have seen in class. In the future, properly documenting your code will be part of your grade. In brief, begin each file with a header like this:
/** * (filename) * * (One or two-sentence description of the program) * * @author (your name(s) ) * Wheaton College, CSCI 235, Spring 2015 * Lab 3 * Feb 2, 2015 */
...and also write a line comment by each variable declaration describing what it is for.
Start out by opening a terminal. Then clone the repository to make a directory for this lab and move into that directory:
$ hg clone /cslab/class/csci235/labs/lab3 $ cd lab3
That gives you a lttle bit of a start on your program, plus some set-up to make Mercurial behave itself.
The Sieve of Eratosthenes is a method for finding prime numbers. The idea is first to write out all numbers (from 2 up to a certain highest number) in a list.
Then cross off all the multiples of 2.
The next uncrossed number (in this case 3) is also the next prime number. Cross off every 3rd number that's not already crossed off.
Repeat this process. Find the next uncrossed number (say, n) and cross off every nth number after that.
Your task is to write a program which uses this technique to compute all the prime numbers up through 100.
To save you some time, you've been provided with a start in the
file Sieve.java
. Open that in emacs and fill in the
opening documentation.
The template provided includes a declaration of a named constant
SIEVE_LIMIT
. This looks very much like a variable
declaration and initialization, but you should notice a few features
of this declaration:
main()
;
static
final
. We'll learn why static
is there in a
couple of weeks; the keyword final
means that
initialization is the last assignment that can be made to the
variable, which is what makes it a constant.
Write your program to print all primes up through
SIEVE_LIMIT
. Then you have only one place to change if
you want to run for a different size (which you might want to do when
you are testing...)
Hint 1 is very general. Hint 1.
Hints 2-4 talk about how to store the data. Hint 2. Hint 3. Hint 4.
Hints 5 and 6 talk about how you process the data to find the answer. Hint 5. Hint 6.
Prepare a typescript that shows your program running.
Then hand in your source file as
lab3
. You do not need to hand in a printed copy.