Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How should I start creating this program? Help

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How should I start creating this program? Help

    A locker room has lockers numbered 0,1,2,...,1023 . All lockers are initially closed. The first person walks in and flips every locker The 2nd person flips every other locker (starting at 0) The 3rd person flips every third locker (starting at 0) . . . The 1024 person flips every 1024 locker (starting at 0) At the end, which lockers are open and which are closed?

    What the program must do:
    Write a program that creates an array of 1024 indices of type Boolean. Each index will represent a locker. You may assume that when the Boolean value is false, the locker is closed and when the value is true, the locker is open. This program will determine which lockers are open and closed at the end of the procedure above.
    So it will output the locker number and whether or not it is open or closed.

    For starters I was thinking to create an array called
    boolean[] Lockers = new boolean[1024];
    which will create 1024 indices with either true or false (open or closed) elements.
    What I don't know is how to make every person go to their correct interval locker, and put it into a loop. When helping please don't use advanced code I have only learned arrays, while/dowhile/for loops, bubble sort*, user input.
    I'm a complete newb at Java so any help would be appreciated.


    *I also think bubble sort should be applied here, but I don't know how to apply it.
    The following code is an unfinished bubble sorting program that I don't understand how to complete.
    int []x = {4, 2, 3,5,1};
    		int temp;
     
    		for (int c=0; c<x.length-1; c++){
    			if (x[c]>x[c+1]){
    				temp = x[c+1];
    				x[c+1]=x[c];
    				x[c] = temp;
    			}
    		}
    For the code above, the for-loop will move through the array x and swap any values if the current index is greater than the next index. To complete the bubble sort, the for loop above needs to be repeated until no swaps are made. Using the code above, add a Boolean variable called swap and a conditional loop that will repeat the for-loop until no more swaps are made.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How should I start creating this program? Help

    What does "flipping" a locker mean? Change its door's status: if open close it, if closed open it?

    If you were to write a for loop for every door flipper, what would the code look like for the first 4 flippers?
    Looking at that code should show you a pattern that can then be used for the 1024 flippers.

    Hint: with boolean variables, the ! operator will change (flip) its value. Every slot in an array can be treated as a variable: theArray[index] is the same as a variable.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How should I start creating this program? Help

    'flipping' a locker would mean either to open or close it. Depending on the state that it's in, it will always do the opposite.
    Also how would I make it so an individual door flipper flips the correct door?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How should I start creating this program? Help

    how would I make it so an individual door flipper flips the correct door?
    Use the for loop's control variable. The variable can be incremented/decremented by any value, not just one:
    for(int x=0; x < max; x += incrVal) {
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    New Hampshire
    Posts
    2
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How should I start creating this program? Help

    I would recommend using the current value of your counter to also determine the increment.

    Meaning, have a for loop that runs 1024 times. The number in the counter variable will tell us which person is running through (i.e. when it's 3, that means it's the fourth person going, since it starts counting at 0)
    Inside that, have a for loop that simulates the person running through the locker room opening/closing lockers. The increment for this inside loop should be the current value of the counter of the outer loop.

    Example of this situation:

    int x, y;
     
    for (x = 0;x < 1024;x++) { //Outer loop. Increments by 1
        for (y = 0;y < 1024;y += x + 1) { //Inner loop, increments by whatever the value of the outer loops is, +1
            //flip locker at the 'y' location
        }
    }


    --- Update ---

    Quote Originally Posted by illegit View Post
    'flipping' a locker would mean either to open or close it. Depending on the state that it's in, it will always do the opposite.
    Also how would I make it so an individual door flipper flips the correct door?
    Lockers[x] = !Lockers[x]

    ^ Not 100% on that but that's my first thought...I'm still learning too (;

  6. #6
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How should I start creating this program? Help

    How would I make the program output the locker number it is at, and whether or not it is open/closed.
    output:
    1 closed
    2 open
    3 closed
    ...

    edit:
    for(int c=0; c<Lockers.length; c++) {
    	System.out.println(c +" " + Lockers[c]);
    }
    had to add the space between them to fix the error

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How should I start creating this program? Help

    Also, how can I make my boolean values output whether or not it is open/closed rather then true/false

    --- Update ---

    Quote Originally Posted by rpressler View Post
    I would recommend using the current value of your counter to also determine the increment.

    Meaning, have a for loop that runs 1024 times. The number in the counter variable will tell us which person is running through (i.e. when it's 3, that means it's the fourth person going, since it starts counting at 0)
    Inside that, have a for loop that simulates the person running through the locker room opening/closing lockers. The increment for this inside loop should be the current value of the counter of the outer loop.
    So like this:
    boolean[] Lockers = new boolean[1024];
    	for(int c=0; c<Lockers.length; c++) {
    		for(int y=0; y<Lockers.length; y+=c+1) {
    			Lockers[y]=!Lockers[y]; 
    		System.out.println(Lockers[y]);
    			}
    		}
    ?

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How should I start creating this program? Help

    When you need to make a program do something, try to break it down into steps you can do one step at a time.

    What you want to do is print if a locker is open or closed. So pseudo code may look like this:

    print "locker number " + lockerNumber + " is"
    if : the locker value is true (this means open)
      println " open."
    else
     println " closed."
    ...and you want to do that "for each" locker, so set up a loop to go through each one

    In the end you should see one of the following for each locker number:
    Locker number 0 is Open.
    Locker number 0 is Closed.
    ...but with the correct locker number depending on which number the loop is on

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How should I start creating this program? Help

    This is a good place to use the ternary operator. Google it for examples. It works the same in several languages.
    It can work like an inline method. Something like this:
    print("some text " + (boolean_expression ? "true message" : "false message"));
    The part in ()s will return one of two Strings depending on the value of boolean_expression.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Not sure where to start: Trying to write a program dealing with an Amazon API
    By ~DrummerGirl~ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2013, 06:20 PM
  2. Where does the program start?
    By CrestaMan in forum Java Theory & Questions
    Replies: 12
    Last Post: May 10th, 2012, 12:16 PM
  3. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 05:58 AM
  4. newbie start java program with switches
    By fortune2k in forum Java Theory & Questions
    Replies: 4
    Last Post: November 1st, 2010, 05:13 PM
  5. Need help creating this program
    By ixjaybeexi in forum File I/O & Other I/O Streams
    Replies: 25
    Last Post: October 19th, 2009, 07:08 AM