Comp Sci 100 Lockers Problem Java Program

  • Thread starter Thread starter thatguy101
  • Start date Start date
  • Tags Tags
    Java Program
AI Thread Summary
The discussion revolves around a Java programming assignment involving 100 lockers and 100 students, where students toggle the state of lockers based on their student number. The initial code provided fails to produce the correct output, consistently returning all false values for locker states. Key issues identified include incorrect comparisons of the locker array and boolean values, as well as improper loop indexing. Suggestions emphasize the need to correctly index the lockers and adjust the logic to toggle the state of specific lockers based on student actions. The conversation highlights the importance of debugging logic and syntax in programming to achieve the desired results.
thatguy101
Messages
14
Reaction score
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
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
 
Thank you for that, but unfortunately it has to be in Java It is the language we are learning in class.
 
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.
 
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.
 
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
 
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!
 
Perhaps this is too obvious but you haven't indexed the lockers variable. Does Java automatically pick up j from the inner loop?
 
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

Similar threads

Replies
10
Views
5K
Replies
12
Views
2K
Replies
1
Views
3K
Replies
7
Views
3K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
5
Views
3K
Back
Top