Lab 3: 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 2013
 * Lab 3
 * Sep 20, 2013
 */

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

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. Thinking ahead: testing

Before you start writing, think about how you could tell if your program is correct. (You probably don't want to solve the problem as stated by hand...)

C. Set up

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

emacs 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) {



   }
}

D. 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 lab3.


Thomas VanDrunen and Cary Gray
Last modified: Fri Sep 20 11:22:27 CDT 2013