subject

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 and * * display all open locker numbers separated by exactly one space. *
* (Hint: Use an array of 100 Boolean elements, each of which indicates whether a *
* locker is open (true) or closed (false). Initially, all lockers are closed.) *
/
public class Exercise_07_23 {
/** Main method */
public static void main(String[] args) {
String[] lockers = new String[100];
// Close all the lockers
closeAllLockers(lockers);
// Invoke the stuentLockerChanges method
studentLockerChanges(lockers);
// Display all open lock numbers
print(lockers);
}
/** isOpen returns true if l is the string "OPEN". False otherwise*/
public static boolean isOpen(String l) {
return l == "OPEN";
}
/** closeAllLockers fills the array with the string "CLOSED" */
public static void closeAllLockers(String[] lockers) {
for (int i = 0; i < lockers. length; i++) {
lockers[i] = "CLOSED";
}
}
/** studentLockerChanges changes the string in each
* element from "CLOSED" to "OPEN" or Vice versa */
public static void studentLockerChanges(String[] lockers) {
int start = 0; // Locker student begins with
for (int s = 1; s <= lockers. length; s++) {
for (int l = 0; l < lockers. length; l += s) {
if (isOpen(lockers[l]))
lockers[l] = "CLOSED";
else
lockers[l] = "OPEN";
}
start++;
}
}
/** print displays all open locker numbers separated by exactly one space */
public static void print(String[] lockers) {
for (int i = 0; i < lockers. length; i++) {
if (isOpen(lockers[i])) {
System. out. print("L" + (i + 1) + " ");
}
}
System. out. println();
}
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 18:00
When is it appropriate to use an absolute reference
Answers: 1
question
Computers and Technology, 23.06.2019 10:00
Hey i just logged on and one of the moderators deleted a bunch of my answers to questions, even though the answers were right and the people it doesn't make sense but if anyone wants to talk about anything just message me lol (this is super random lol)
Answers: 1
question
Computers and Technology, 23.06.2019 18:30
This program should be a short piece of code that prints all of the positive integers from 1 to 100 as described more fully below. the program may contain multiple methods, and if using an oo language, should be contained within a single class or object. the program should be designed so that it begins execution when invoked through whichever mechanism is most common for the implementation language. Γ’β€“ΕŸ print out all positive integers from 1 to 100, inclusive and in order. Γ’β€“ΕŸ print messages to standard output, matching the sample output below. Γ’β€“ΕŸ in the output, state whether the each integer is 'odd' or 'even' in the output. Γ’β€“ΕŸ if the number is divisible by three, instead of stating that the number is odd or even, state that the number is 'divisible by three'. Γ’β€“ΕŸ if the number is divisible by both two and three, instead of saying that the number is odd, even or divisible by three; state that the number is 'divisible by two and three'. Γ’β€“ΕŸ design the logic of the loop to be as efficient as possible, using the minimal number of operations to perform the required logic. sample output the number '1' is odd. the number '2' is even. the number '3' is divisible by three. the number '6' is divisible by two and three.
Answers: 1
question
Computers and Technology, 24.06.2019 05:30
CΓ³mo pongo un tomo de llamada sin pagar?
Answers: 1
You know the right answer?
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the...
Questions
question
Health, 16.07.2020 09:01
question
Mathematics, 16.07.2020 09:01
question
Mathematics, 16.07.2020 09:01
Questions on the website: 13722362