The goal of this lab is to practice creating and using basic arrays. You will be given two problems. For each, you will write a program which computes the answer making use of an array.
To help you, I will be providing a series of hints for each problem. 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 me use 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 2010 * Lab 4 * Sept 21, 2010 */
...and also write a line comment by each variable declaration describing what it is for.
First start out by opening a terminal, making a new directory for this lab, and moving into that directory.
mkdir lab4 cd lab4 cp /cslab/class/csci235/lab4/* .
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.
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) { } }
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.
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 less than 100.
Open the file in XEmacs in which to write this program.
xemacs Sieve.java
Write the opening documentation, and also make a "stub" for this program, something like
public class Sieve { public static void main(String[] args) { } }
As it was last time, 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.
Prepare a typescript that shows your programs running. Clean up the typescript, and print your source files and the typescript to hand in.
Also hand in the same files online as lab4
.