100 Lockers Problem Java Program

  • Comp Sci
  • Thread starter thatguy101
  • Start date
  • Tags
    Java Program
In summary: Assignment5 { public static void main(String[] args) {//set up all variables final boolean OPEN = true; final boolean CLOSED = false;//set up array boolean lockers[] = new boolean[100];//make all of them false for(int j = 0; j <= 99; j++){ lockers[j]= CLOSED ; }//set up the students that open and close lockers
  • #1
thatguy101
14
0
. Homework Statement
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open, and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100. After all the students have passed through the building and changed the lockers, which lockers are open? Write a program to find your answer. The program should display the answer like this: Locker x is open Locker y is open ... Locker z is open Design requirement: Use a method and an array of 100 boolean elements, each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.

Homework Equations


I'm writing a program but the output is all false's. I know that all the perfect squares should be true, but I'm not sure where I am struggling with this program

The Attempt at a Solution


Mod note: Added code tags to preserve indentation.
Java:
public class Assignment5 {

    public static void main(String[] args) {
//set up all variables
        final boolean OPEN = true;
        final boolean CLOSED = false;
//set up array
        boolean lockers[] = new boolean[100];
//make all of them false
        for(int j = 0; j <= 99; j++){
            lockers[j]= CLOSED ;
        }
//set up the students that open and close lockers
        for( int stu = 1; stu <= 100; stu++){
            for(int j = 0; j <= 99; j++){
                if(j % stu == 0){
                    if (lockers == CLOSED){
                        lockers = OPEN;
                        }
//I think it's somewhere in here where my code is wrong.
                    if (lockers == OPEN){
                        lockers = CLOSED;
                   
                    }
                }
           
            }
       
        }
        for( int j = 0; j <= 99; j++){
            System.out.println("locker " + (j+1) + " is " + lockers[j]);
        }

    }

}
 
Last edited:
Physics news on Phys.org
  • #2
I don't know any Java but here's some pseudo code

Initialise L1-L100 = 0

For n = 1 to 100

For k = n to 100 step n

Add 1 to Lk

End for

End for
 
  • #3
Thank you for that, but unfortunately it has to be in Java It is the language we are learning in class.
 
  • #4
thatguy101 said:
Thank you for that, but unfortunately it has to be in Java It is the language we are learning in class.
The idea with pseudocode is to present the algorithm, which can then be implemented in whatever code your writing in.

Please use [ code ] and [ /code ] tags around your code (without the extra spaces). These tags preserve your indentation, which makes your code easier to read.

I also made a slight change in your code. An array index of i gets interpreted by our system as the start tag for italics, so I changed one of your loop variables to j to prevent this from happening.
 
  • #5
Pseudocode also breaks down the problem of writing a bug free program into getting the logic right, then getting the syntax right.

So, if everything is coming out "false" you can check your pseudocode and look for a logic error. And, if your logic is sound, you can look for an error in your Java syntax.

The thing that confuses me most about your code is why you start your inner for loop from 0? And not from the student number.
 
  • #6
PeroK said:
Pseudocode also breaks down the problem of writing a bug free program into getting the logic right, then getting the syntax right.

So, if everything is coming out "false" you can check your pseudocode and look for a logic error. And, if your logic is sound, you can look for an error in your Java syntax.

The thing that confuses me most about your code is why you start your inner for loop from 0? And not from the student number.
I did this because the inner loop is about the locker number. So the first locker would be lockers[0]. and to iterate that 100 times, it would need to stop at 99
 
  • #7
But each student starts at different locker. The 15th student starts at locker 15, then 30, then 45 ...

And after 50, each student only does 1 locker, so it should be more efficient to have only 1 line of code executed.

Student 56 does locker 56 and that's it.

It would be interesting to see your pseudocode!
 
  • #8
Perhaps this is too obvious but you haven't indexed the lockers variable. Does Java automatically pick up j from the inner loop?
 
  • #9
You can't do this:
Java:
if (lockers == CLOSED){
       lockers = OPEN;
}
//I think it's somewhere in here where my code is wrong.
if (lockers == OPEN){
        lockers = CLOSED;
}
lockers is an array of type boolean, so it makes no sense to compare lockers -- the name of the array -- with a boolean value.

After you have initialized the lockers by closing them, your code needs to do this:
Student 1 flips the state of each locker.
Student 2 flips the states of lockers 2, 4, 6, ..., 100
Student 3 flips the states of lockers 3, 6, 9, ..., 99
and so on.

Your inner loop needs to iterate not through all of the numbers, but from the student's number by multiples of that number, as I've laid out above. This can be done in how you set up your for loop.

I would number the students and lockers the same; i.e. 1 through 100. Although arrays and C and C-based languages (such as Java) normally start at index 0, it will make your code easier to follow by numbering lockers 1, 2, ..., 100.
 
  • Like
Likes PeroK
  • #10
PeroK said:
Perhaps this is too obvious but you haven't indexed the lockers variable. Does Java automatically pick up j from the inner loop?
It does not, so the two comparisons always fail (an array is never a boolean value). Fixing that should solve the problem, but the program can be sped up with the suggestion by Mark.
 
  • #11
Mark_44 is right.you can't assign a single value to an array it's not a syntax.Also since you are checking a particular locker no. you should check that particular index of array i.e. lockers[j] instead of lockers as j loop is iterating for locker numbers.
Java:
.      
if (lockers[j]== CLOSED){
       lockers[j]= OPEN;
}
//I think it's somewhere in here where my code is wrong.
if (lockers[j]== OPEN){
        lockers[j] = CLOSED;
}
 
  • #12
anyonebutangel said:
Mark_44 is right.you can't assign a single value to an array it's not a syntax.Also since you are checking a particular locker no. you should check that particular index of array i.e. lockers[j] instead of lockers as j loop is iterating for locker numbers.
Java:
.     
if (lockers[j]== CLOSED){
       lockers[j]= OPEN;
}
//I think it's somewhere in here where my code is wrong.
if (lockers[j]== OPEN){
        lockers[j] = CLOSED;
}
Note that this will not function as intended. What state is lockers[j] in after the first if?
 
  • #13
Ibix said:
Note that this will not function as intended. What state is lockers[j] in after the first if?
well as the outer for loop iterates from student 1 to the last student,then for each student the inner loop will iterate for locker 1 to 10 now the two referred if statement are within an if statement that'll only work if the student of a particular number approaches that number locker.
now since student is supposed to open a closed locker and close an open locker the two if statementsvtake care of that
Java:
.    
for( int stu = 1; stu <= 100; stu++)
}
 for(int j = 0; j <= 99; j++)
{
 if(j % stu == 0)
{
 if (lockers [j]== CLOSED)
{ 
lockers[j]= OPEN; 
} //I think it's somewhere in here where my code is wrong. if (lockers[j] == OPEN)
{ 
lockers[j]= CLOSED;
 }
 }
 }
 }
 
  • #14
I remember this thread. Hard to believe it was five years ago!
 
  • Like
Likes Ibix and anyonebutangel
  • #15
PeroK said:
I remember this thread. Hard to believe it was five years ago!
hope it gets solved.
 
  • #16
Hopefully the OP has now passed this course. Time to close the thread.
 
  • #17
anyonebutangel said:
Java:
.  
for( int stu = 1; stu <= 100; stu++)
}
for(int j = 0; j <= 99; j++)
{
if(j % stu == 0)
{
if (lockers [j]== CLOSED)
{
lockers[j]= OPEN;
} //I think it's somewhere in here where my code is wrong. if (lockers[j] == OPEN)
{
lockers[j]= CLOSED;
}
}
}
}
Aside from being terribly formatted to the point of being nearly unreadable, this code has several syntax errors, with incorrect braces -- the first error is that the right brace after the for loop header at the top should be a left brace, not a right brace.

Thread is still closed...
 
  • Like
Likes PeroK

1. What is the "100 Lockers Problem" in Java?

The "100 Lockers Problem" is a popular math puzzle that involves 100 lockers numbered from 1 to 100 and 100 students. The students take turns opening and closing the lockers according to a specific rule, and the puzzle is to determine which lockers will be open or closed after all the students have had a turn.

2. How is the "100 Lockers Problem" solved in Java?

The "100 Lockers Problem" can be solved using a Java program that simulates the actions of the students opening and closing the lockers. The program uses a loop to iterate through each student and each locker, following the specified rule to determine the final status of each locker.

3. What is the rule for opening and closing lockers in the "100 Lockers Problem"?

The rule for opening and closing lockers in the "100 Lockers Problem" is that the first student, on their first turn, will open all 100 lockers. On their second turn, they will close every second locker (2, 4, 6, etc.). On their third turn, they will toggle every third locker (3, 6, 9, etc.) and so on, until all 100 students have had a turn.

4. How many lockers will be open at the end of the "100 Lockers Problem" in Java?

The number of lockers that will be open at the end of the "100 Lockers Problem" in Java depends on the number of factors that each locker has. For example, a locker with an even number of factors will end up closed, while a locker with an odd number of factors will end up open. In the case of the 100 lockers, only the lockers with a perfect square number (1, 4, 9, 16, etc.) will be open at the end.

5. What is the significance of the "100 Lockers Problem" in Java?

The "100 Lockers Problem" in Java is significant because it demonstrates the power and versatility of programming to solve complex problems. It also highlights the importance of understanding mathematical concepts such as factors and perfect squares. Additionally, it can be used as a fun and challenging exercise for students learning to code in Java or for individuals looking to improve their problem-solving skills.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • General Math
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top