This website is preserved for historical and scholarly reference and is no longer actively maintained.
CpSc 210, Section 1
Assignment 3
September 13, 2002
Due: Friday, September 20 at 11:59 p.m.
Total point value of assignment: 50 points
Not accepted late.
This assignment is to be done INDIVIDUALLY.
Assignment: Complete the method computeNextPrime. The class PrimeNumbers
must be stored in a file named PrimeNumbers.java. (3 point deduction
from grade for failure to name file correctly!) You may change
the testing in the main method if you wish. Do not change any
methods except computeNextPrime and main.
// CPSC 210
// Assignment #3
// Build prime number array
public class PrimeNumbers
{
public static void computeNextPrime ( int indexOfLast, int [] primes )
// pre: indexOfLast >= zero and prime[0] will contain 2
// If indexOfLast > zero, primes are sequentially stored
// post: computes the next prime number after primes[indexOfLast] and
// stores this value in the array primes
{
// code goes here
// with the initialization shown in the main program,
// the first time this method is called primes[6] will be set to 17
}
public static void printPrimes ( int indexOfLast, int [ ] primes )
// print primes[0] .. primes[indexOfLast]
{
System.out.println ("The first " + (indexOfLast+1) + " primes are: ");
for (int k = 0 ; k <= indexOfLast ; k++ )
System.out.print(" " + primes[k]);
// k > indexOfLast
System.out.println();
}
public static void main ( String [ ] args )
{
// an array to hold integers that represent prime numbers
int maxSize = 501;
int [ ] primes = new int [maxSize];
// start filling the array of prime numbers
primes[0] = 2;
primes[1] = 3;
primes[2] = 5;
primes[3] = 7;
primes[4] = 11;
primes[5] = 13;
int indexOfLastPrime = 5;
printPrimes (indexOfLastPrime, primes);
// compute and store 10 more primes
int increment = 10;
int newCount = increment + indexOfLastPrime;
while ( indexOfLastPrime <= newCount )
{
computeNextPrime(indexOfLastPrime++, primes);
printPrimes (indexOfLastPrime, primes);
}
// indexOfLastPrime > newCount
}
}
Also, a test case you should run is
primes[0]=2;
intindexOfLastPrime = 0;
Grading Rubric:
5 pts. reasonable attempt to solve problem as assigned
5 pt. also compiles
5 pts. style standards (includes clarity)
(1) Your name must be printed as a comment at the beginning of file.
(2) A description of what this program does must immediate follow name.
(3) Indent exactly 3 or 4 spaces. Be consistent. See handout examples.
(4) Every Java statement must begin on a separate line.
(5) The first operation of your program must be to print your name,
followed by "CPSC 210", the assignment number, and a description
of what program does.
(6) A comment must immediately follow all while, do, or for loops
giving the condition that is true after loop exit.
The following 35 points are available only for code that compiles
and runs without error.
5 pts. main as given gives correct
5 pts. main (primes[0]=2; indexOfLastPrime=0;) gives correct result
10 pts. program works correctly (with our code).
15 pts. overall quality of program (i.e., ease of reading and
understanding code, choice of algorithm including use of
necessary/unnecessary looping, etc.)
Other requirements:
Follow the style standards to be found on your instructor's
web page for CPSC 210.
Submit your program using the handin command. There is a link to
the handin description on the CPSC 210 lab page (www.cs.clemson.edu/~lab210).