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 06:00
The width of a piece of rectangular land is 5m shorter rhan 1/3 of its length .find the width of the land if the length is 60m,150m.
Answers: 1
question
Computers and Technology, 25.06.2019 12:00
Bachelor’s degrees are typically earned in a. 1-2 years b. 2-3 years c. 3-4 years d. 4-5 years
Answers: 1
question
Computers and Technology, 25.06.2019 13:10
1. which characteristics of pop art are evident in the image? select all that apply. (3 points). a. ben-day dots b. bold use of color c. celebrity figure d. everyday object
Answers: 3
question
Computers and Technology, 25.06.2019 21:00
People might use a word processing program to: a communicate with people b create and edit text documents c edit video d sort and organize information to reference later
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
Mathematics, 21.01.2021 04:50
question
Mathematics, 21.01.2021 04:50
question
Mathematics, 21.01.2021 04:50
question
Mathematics, 21.01.2021 04:50
question
Health, 21.01.2021 04:50
question
Biology, 21.01.2021 04:50
Questions on the website: 13722363