CSE 143 Computer Programming II

Programming Assignment #4

checkpoint: Saturday, 7/15/2006 at 12:00 p.m.

due: Tuesday, 7/18/2006 at 9:00 p.m.

Introduction

Assignment 4 is designed to prepare you for the midterm at the end of next week. You will need to use all of the concepts and skills you have learned so far this quarter to implement a class that computes all of the prime numbers up to some integer n. The technique you are to use, known as the Sieve of Eratosthenes, was developed by a Greek (Eratosthenes) who lived in the third century BC.

The goals of Assignment 4 are:

As usual, your code will be evaluated not only on functionality but also on quality. Make sure to comment your code appropriately and follow Java conventions.

This assignment has two deadlines. The checkpoint deadline is a bonus to encourage you to start the assignment early. The entire assignment is due at the regular time (9:00 p.m. on Tuesday). Read the Turn-in section below for details.

Background

The Sieve of Eratosthenes is described by the following pseudocode:

Fill a list with the consecutive integers 2 through n inclusive.
Create an empty queue to store primes.
do {
    Obtain the next prime (P) by removing the first value in the list.
    Add P to the queue of primes.
    Iterate through the list, eliminating numbers divisible by P.
} while (P < sqrt(n));
All remaining values in the list are prime, so transfer them to the queue.

Problem Statement

Assignment 4 has six starter files:

You should be able to complete this assignment in stages. Remember to test your code thoroughly after each step to keep debugging simple and to ensure a stable codebase for the later stages.

First, use the provided IntLinkList and IntListNode classes to create LinkList and ListNode classes. Your new classes should differ from the originals in three ways:

Next, define a class called Sieve with the following public methods:

Method

Description

Sieve()

Constructs a sieve object.

void computeTo(int n)

Computes all primes up to and including n.  Throws an IllegalArgumentException if n is less than 2.

void reportResults()

Reports the primes to System.out.  Throws an IllegalStateException if no legal call has been made to the computeTo method.

Your reportResults method should print the maximum n used and should then show a list of the primes (12 per line with a space after each prime). We do not guarantee that the number of primes will be a multiple of 12. The calls on reportResults must exactly reproduce the format of the sample log, but you should note that the final line of output that appears in the log (reporting the percentage of primes) is generated by the main program, not by the call on reportResults.

int getMax()

Returns the value of n used in the last call to computeTo.  Throws an IllegalStateException if no legal call has been made to the computeTo method.

int getCount()

Returns the number of primes found in the last call to computeTo.  Throws an IllegalStateException if no legal call has been made to the computeTo method.

In addition, you are required to adhere to the following constraints when implementing Sieve:

Turn-in

Hints and Tips