• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Write the code of program Room?

 
Greenhorn
Posts: 18
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello GUYS i have this HOME WORK to do for tomorrow and i dont have any Idea how to do it can someone please help me ???


Write the code of program Room that chooses the following problem:

In a long room are 100 bulbs (lamps) lined up one after the other. Each bulb (lamp) has a breaker("Interlocking" (ON/OFF) which is turned off at the beginning. Each lamp has its ordinal number ranging from 1 to 100. The room has a door "incoming door" and an "exit door". The entrance door are 100 people. Also each person has its own ordinal number of 1 to 100.

Each person enters the room one by one according to the ordinal number:

No.1 enters the room, changes the condition of each lamp and leaves the room.
Person No. 2 enters the room and changes the condition of each second lamp (changing the condition of bullets number 2, 4, 6 ...) and comes out of the room.
No. 3 enters the room and changes the condition of each third pocket (changing the status of lamp number 3, 6, 9 ...) and comes out of the room.
Person no. 4 enters the room and changes the condition of each fourth lamp (changing the condition of lamp number 4, 8, 12 ...) and comes out of the room.
This continues with each person until the person no. 100 have gone through the room changing the condition of each one hundredth lamp (number 100).
Show how many of the lamps are (ON) after the 100th person has gone through the room.And whitch lamps are (ON) with numbers ?

Note: To simulate the states of the lamps use a array, while for simulating the actions of persons in the room it should be used only for loop.

CAN SOMEONE HELP ME WITH THE CODE ?
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Work through the steps of the stated problem one at a time. After implementing a step, test it. Go on to the next step.

Do you know how to create an array?
Can you figure out what data type the array should be?
How many elements in the array are needed? (Careful: Java starts counting with zero not one.)

Create a class with a main that does just this and post back here.
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Egzon Krasniqi wrote:[color=black]
...
No.1 enters the room, changes the condition of each lamp and leaves the room
...

It dosn't even state if the lamps are on or off to begin with, so I asume they're off?
If I would just give you the code, the mods would shoot me, but I'll give you this pointer, you'll need a nested for loop, one to represent the persons and one to itterate the lamps.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have seen a similar problem before, only not about light bulbs. It was about finding prime numbers and is called the Sieve of Eratosthenes. That isn't quite the same, but look up the Sieve may help with your current assignment.
Please avoid UPPER CASE TEXT and coloured text; I have changed yout text to alll black because it was illegible with the colours.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . It dosn't even state if the lamps are on or off to begin with . . .

It says the lamps are all turned off before the exercise.

[edit]Corrected qu9te tags and spelling error
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I read over that...
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are done with the simulation, can you figure out how many times lightbulb i is switched (i = 1...100), and predict in what state it will end?

edit: oops, I had not noticed Campbells remark about the Sieve. Sorry for that!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the cow, whoever it was
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, well, well, well, well! What a surprise seeing the result!
 
Egzon Krasniqi
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:

Egzon Krasniqi wrote:[color=black]
...
No.1 enters the room, changes the condition of each lamp and leaves the room
...

It dosn't even state if the lamps are on or off to begin with, so I asume they're off?
If I would just give you the code, the mods would shoot me, but I'll give you this pointer, you'll need a nested for loop, one to represent the persons and one to itterate the lamps.



Can you slove it or not ?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:It dosn't even state if the lamps are on or off to begin with, so I asume they're off?


In a long room are 100 bulbs (lamps) lined up one after the other. Each bulb (lamp) has a breaker("Interlocking" (ON/OFF) which is turned off at the beginning.

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Egzon Krasniqi wrote:Can you slove it or not ?


Sure, we can solve it, but that's not the point. We're hear to help you lean and not just give you the answer. Can you show us the code you have so far and tell us where you are stuck?
 
Egzon Krasniqi
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understand..

Lamps in the ROOM are OFF at the beginning.
Outside are 100 persons.
Person number 1 enter the ROOM and switch all lamps ON because all are (off);
Person number 2 enter the ROOM and switch OFF lamps 2 , 4 , 6.. because all are ON..
Person number 3 enter the ROOM and switch OFF lamps 3 , 5 , 7...

If you make an boolean array at the start all are FALSE (off);
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A petite hint: the divisors of 12 are 1, 2, 3, 4, 6, 12. What has that got to do with the problem, and what has that got to do with the Sieve of Eratosthenes?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Egzon Krasniqi wrote:Lamps in the ROOM are OFF at the beginning.

If you make an boolean array at the start all are FALSE (off);

Do you have the code to do this much?
 
Egzon Krasniqi
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont have any idea how to write the code, i will try it but can you tell me at the end how much lamps are ON ?
 
Egzon Krasniqi
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Egzon Krasniqi wrote:Lamps in the ROOM are OFF at the beginning.

If you make an boolean array at the start all are FALSE (off);

Do you have the code to do this much?



This is the code that a friend send me can someone tell me is this right ? at the end are 10 lamps on ?


public class ROOM{
   public static void main(String [] args){
       boolean [] lamp = new boolean [100];
       int countTrue = 0;
       int index = 0;

       
       
       for(int i = 0; i < lamp.length; i++){
           for(int j = 0; j < lamp.length;j++){
               if((j+1) % (i+1) == 0){
                   if(lamp[j] == false){
                       lamp[j] = true;
                   }
                   else{
                       lamp[j] = false;
                   }
               }
           }
       }
       
       for(int i = 0; i < lamp.length; i++){
           if(lamp[i] == true){
               countTrue++;
           }
       }
       
       int [] lista = new int [countTrue];
       
       for(int i = 0; i < lamp.length;i++){
           if(lamp[i] == true){
               lista[index++] = i+1;
           }
       }
       System.out.println();
System.out.println();
     
System.out.print("at the end on are :" + countTrue + " lamps.And the lamps that are on: ");
       for(int i = 0; i < lista.length;i++){
           System.out.print(lista[i] + ", " );
}
}  
}
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't even read the code, but I already can tell you this, if you would turn in this code at my professir, he would flunk you for sure. Just alone by the way it's formatted, and on first look, it seems way to mutch ocde for what you were trying to do
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need a nested for loop to itterate the lamp and the persons, 1 int for the interval and a final foreach to print the state of the lamps at the end, that's it...
 
Egzon Krasniqi
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I haven't even read the could, but I already can tell you this, if you would turn in this code at my professir, he would flunk you for sure. Just alone by the way it's formatted, and on first look, it seems way to mutch ocde for what you were trying to do



Is this forum to help or what ? i am beginner at java i just tried to do ?
 
Egzon Krasniqi
Greenhorn
Posts: 18
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:

if((j+1) % (i+1) == 0){  


that can never be true...
And that's only one of the many issues.
you need a nested for loop to itterate the lamp and the persons, 1 int for the interval and a final foreach to print the state of the lamps at the end, that's it...



Okay i will try it
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Egzon Krasniqi wrote:. . . Person number 3 enter the ROOM and switch OFF lamps 3 , 5 , 7... . . .

Surely that is 3, 6, 9....

. . . can someone tell me is this right ? . . .

I can tell you. It isn't correct. Not even if it gives the same result as I found this morning is it correct.
The error is in the line with % in. After that line, any correct results are there by luck, not good programming.
There are also design and style problems.
  • 1: Everything is crammed into the main() method, which is far too long.
  • 2: Never use == true and == false; they are poor style and very error‑prone.
  • 3: Don't switch booleans with an if‑else. Use lamps[j] = !lamps[j];
  • 4: There is no need to copy numbers into a second array; you can traverse the first array and print the results from that.
  •  
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Demesmaecker wrote:

    if((j+1) % (i+1) == 0){  


    that can never be true... . . .

    Are you sure about that? When i is 2 and j is 5, even?
     
    Daniel Demesmaecker
    Rancher
    Posts: 1171
    18
    IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Lol no I was just editting my post
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Demesmaecker wrote:Lol no I was just editting my post

    I was at school with Lol, but I haven't seen him for a long time. But I am not called Lol myself, so please avoid that word. Also please be careful when editing posts.
     
    Egzon Krasniqi
    Greenhorn
    Posts: 18
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Egzon Krasniqi wrote:. . . Person number 3 enter the ROOM and switch OFF lamps 3 , 5 , 7... . . .

    Surely that is 3, 6, 9....

    . . . can someone tell me is this right ? . . .

    I can tell you. It isn't correct. Not even if it gives the same result as I found this morning is it correct.
    The error is in the line with % in. After that line, any correct results are there by luck, not good programming.
    There are also design and style problems.
  • 1: Everything is crammed into the main() method, which is far too long.
  • 2: Never use == true and == false; they are poor style and very error‑prone.
  • 3: Don't switch booleans with an if‑else. Use lamps[j] = !lamps[j];
  • 4: There is no need to copy numbers into a second array; you can traverse the first array and print the results from that.


  • Thanks for advices man i will try it in my own way i just need the answer "how much lamps are ON at the end" to test my code if its right ?
     
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Egzon Krasniqi wrote: i will try it in my own way i just need the answer "how much lamps are ON at the end" to test my code if its right ?


    Please write the code and post it here, so somebody could run it and verify its correctness (or not).

    But in general I think, that if you'd understand the problem, you wouldn't question your algorithm whether it is correct or not. To me seems that you didn't spend enough time trying to understand the problem. And that is really the crucial part in any problem.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Egzon Krasniqi wrote:. . . Thanks for advices man

    That's a pleasure

    . . . i just need the answer "how much lamps are ON at the end" to test my code if its right ?

    Afraid not. The fact that you get the right answer doesn't mean that your algorithm is correct. Many people think that
    correctAlgorithm ⇒ correctAnswer ≡ correctAnswer ⇒ correctAlgorithm
    but that is logically incorrect. You have, I think, been given an incorrect algorithm which happens to produce the required answer. The correct logic, which is called the Contrapositive Rule, is,
    correctAlgorithm ⇒ correctAnswer ≡ ¬correctAnswer ⇒ ¬correctAlgorithm
    You can only tell from a wrong result that the algorithm is incorrect, not that a correct result means the algorithm is correct.

    You need to work out the correct algorithm, which can be relied upon to give the correct result. And that algorithm or technique will be easier to write than you think. There is a little wrinkle that array indices start at 0 and people start counting from 1. Maybe you will be better off with a 101‑element array.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Show how many of the lamps are (ON) after the 100th person has gone through the room.And whitch lamps are (ON) with numbers ?


    Counting the number of ON bulbs only satisfies part of the requirements.
     
    Egzon Krasniqi
    Greenhorn
    Posts: 18
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks to everyone for such a good advices i will post my code as soon as i think it is ok
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please post what you have, even if it isn't complete.
     
    Egzon Krasniqi
    Greenhorn
    Posts: 18
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

    Egzon Krasniqi wrote: i will try it in my own way i just need the answer "how much lamps are ON at the end" to test my code if its right ?


    Please write the code and post it here, so somebody could run it and verify its correctness (or not).

    But in general I think, that if you'd understand the problem, you wouldn't question your algorithm whether it is correct or not. To me seems that you didn't spend enough time trying to understand the problem. And that is really the crucial part in any problem.



    Campbell Ritchie wrote:Please post what you have, even if it isn't complete.



    Here i have the code i write it in 2 classes I dont know exactly if its right or not at the end are total 9 lamps ON :\ I practice this work on my notebook and it should be 40 lamps ON !

    ---------------------------------------------------------------

    ----------------------------------------------------------------------

     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic