Lab 3: Arrays

1. Introduction

The goal of this lab is to practice creating and using basic arrays. This lab will also be your first (required) use of Mercurial. Look back at the pre-lab reading for a reminder of how that works.

Each 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, Fall 2014
 * Lab 3
 * Sep 18, 2014
 */

...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. If you use the command ls -aF, you'll be able to see a directory named .hg, which is where the repository hides, and a file named .hgignore, which tells Mercurial about files that it does not need to keep (such as the .class files produced by the compiler).

2. The Sieve of Eratosthenes

A. Problem

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.

B. Set up

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:

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...)

C. Hints

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.

D. Committing to the repository

Whenever you have a version of your program that is significant, you can commit it to the repository so that you will be able to come back to it in the future. When you get ready to commit,

  1. Think of a one-line way to describe this version. Right now, you may choose something like "Completed the sieve".
  2. Check that things are as you expect with the command hg status. You'll see a list of files that have been modified in your working copy (M) and that will be added (A) or removed (R) when you do the commit. Any files that Mercurial does not know about will show up with a question mark, and files that Mercurial expects but are missing show up with an exclamation point. Right now, you should see just Sieve.java as modified.
  3. When you are ready, commit with
    hg commit -m "Completed the sieve"
    
    (or whatever message you chose in step 1). You will need the quotes around the message.

After you commit, you should be able to see your new revision in the list you get from hg log.

3. Lockers

Do at least the set-up portion for this problem; solve it if you have time.

A. Problem statement

Once there was school hallway with 1000 lockers, numbered 1 to 1000. Initially, all the lockers were open. Then someone came along and closed every other locker—the second, the fourth, the sixth, etc. Next, someone came along and and switched (closed it if it was open, opened it if it was closed) every third locker—closed the third, opened the sixth, closed the ninth. After that, someone did the same for every fourth locker—opened the fourth, opened the eighth, closed the twelfth. Many such people came along, the last one switching the 50th locker, the 100th locker, the 150th locker, etc.

At the end of all this locker-opening and closing, how many lockers were open and how many were closed? Which locker got switched the most often? Your program should print out the answer to these questions.

B. Setting up

Use Emacs to create a new file for this problem. You can do that with

$ emacs Lockers.java &
That gives you an empty file. You need to fill in the opening documentation, and you need the stub of the program, which will look like this:
public class Lockers {
    public static void main(String[] args) {


    }
}

For getting started and testing, it is a good idea to work with a smaller version of the problem. So before line containing main, insert the declarations of two named constants: N_LOCKERS, initially set to 10, and N_PEOPLE, initially set to 3. (That is a small enough problem that you can work it out by hand and check your answer.)

Save your file. Mercurial does not know about the new file; so if you run hg status now, it will show up as unknown. Tell Mercurial about it with the command hg add. Checking status should now show it as added; it won't get saved into the repository until you do a commit. If you'd like to, you can commit it now; remember to think of a message for the log first.

C. Solving the problem.

If you still have time, think about how to write a program that solves the problem. The two things that you need to be able to print out at the end are

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.

Once you have a working program, Change the values of the constants to reflect the original problem (1000 lockers and 50 people).

Commit your work at any time. Certainly commit it if you complete the problem, or before you get ready to leave.

4. Turn in

Prepare a typescript that shows both of your programs running. For today's programs, you need to run each of them only once. After you run your programs, include the command hg log.

Then hand in your source files and typescript as lab3. You do not need to hand in a printed copy.

5. Copying a repository

Use the hg clone command, as described here to copy your repository to your own account.


Thomas VanDrunen and Cary Gray
Last modified: Thu Sep 18 06:54:59 CDT 2014