1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

100 Lockers Problem Java Program

  1. Apr 4, 2015 #1
    . The problem statement, all variables and given/known data
    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.

    2. Relevant 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 im struggling with this program


    3. The attempt at a solution
    Mod note: Added code tags to preserve indentation.
    Code (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: Apr 4, 2015
  2. jcsd
  3. Apr 4, 2015 #2

    PeroK

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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
     
  4. Apr 4, 2015 #3
    Thank you for that, but unfortunately it has to be in Java It is the language we are learning in class.
     
  5. Apr 4, 2015 #4

    Mark44

    Staff: Mentor

    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.
     
  6. Apr 4, 2015 #5

    PeroK

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.
     
  7. Apr 4, 2015 #6
    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
     
  8. Apr 4, 2015 #7

    PeroK

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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!
     
  9. Apr 4, 2015 #8

    PeroK

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Perhaps this is too obvious but you haven't indexed the lockers variable. Does Java automatically pick up j from the inner loop?
     
  10. Apr 4, 2015 #9

    Mark44

    Staff: Mentor

    You can't do this:
    Code (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.
     
  11. Apr 4, 2015 #10

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: 100 Lockers Problem Java Program
  1. Java programming (Replies: 4)

  2. Java program (Replies: 3)

Loading...