Background:
Imagine a school with 10 students, each with her own locker. At the beginning of the day, all 10 lockers are closed. Then, the 10 students do the following:
- Student #1 opens all lockers
- Student #2 goes through and changes lockers 2, 4, 6, 8, and 10 (so, closes those lockers)
- Student #3 goes through and changes lockers 3, 6, and 9. That is, changes all lockers that are a multiple of 3 (closing those that were open, and opening those that were closed)
- Student #4 goes through and changes all lockers that are a multiple of 4.
- and so on up to and including student #10.
The big question is: after all ten students have gone through, which lockers are open?
Requirements:
You are to write a program that can solve this for any number of students (the number of students will always be the same as the number of lockers), showing the lockers along the way, and then listing which lockers are open after each student. You should match the output format shown below as closely as possible.
A few specifics:
- Name your class Program13.
- When printing the ten lockers, print them at the end of each student going through and changing the lockers. So, the first stage that gets printed is the state of the lockers after person 1 has gone through and opened them all.
- Use O to represent an open locker, and to represent a closed locker. Do not put spaces between the lockers. So, for example, after stage 3, we see OOOO which indicates that lockers 1, 5, 6, and 7 are open.
- At the end of all the stages, list which lockers remain open.
- Use a boolean array to keep track of which lockers are open and which are closed.
- Break this problem into small single-purpose methods. My solution did not have any method that was longer than 12 lines long (plus comments).
- To submit, create a folder named program13. It should contain Program13.java. Then, zip and submit that folder.
Sample run #1: Notice that the user has the option of showing the lockers after each stage. This is useful for larger numbers. But the program will always show the comma-separated list of open lockers. Notice that 10 students means 10 lockers and 10 stages. After student #1, each locker is open. After student #2, the evens are closed, and so on. After student #10, lockers 1, 4, and 9 are still open.
How many lockers? 10Show stages? (y/n)yOOOOOOOOOOO-O-O-O-O-OOOOOOOOOOOO-OOO-OOOOO-OOOO-OOOOOO-OOOO-O-Open: 1, 4, 9 |
Sample run #2: Notice that the user did not want to see the stages, and so only the list of open lockers was printed at the end.
Reviews
There are no reviews yet.