Lab 4: Arrays

1. Introduction

The goal of this lab is to practice creating and using basic arrays.

The problem is accompanied by a series of hint. 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 this program, practice the documentation procedures you have seen in examples. 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 2011
 * Lab 4
 * Sep 16, 2011
 */

...and also write a line comment by each variable declaration describing what it is for.

First 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/lab4
$ cd lab4

2. The locker problem

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. Set up

Open the file in XEmacs in which to write this program.

xemacs Lockers.java &

Write the opening documentation, and also make a "stub" for this program, something like

public class Lockers {
   public static void main(String[] args) {



   }
}

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.

3. Turn in

Prepare a typescript that shows your program running.

Also hand in your source file and typescript online as lab4.


Thomas VanDrunen and Cary Gray
Last modified: Wed Sep 14 21:16:59 CDT 2011