Computers and Technology

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();
}
}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 21:30, Boogates7427
Apower user needs you to install a second type of operating system on his computer to increase efficiency while running some specialized software programs. which installation technique should you use?
Answers: 3
image
Computers and Technology, 22.06.2019 00:30, silasjob09
Jenny wants to look at row 345 and compare it to row 17. what can she do if she wanted to easily adjust to see both at once?
Answers: 3
image
Computers and Technology, 22.06.2019 03:10, victoriadorvilu
This program reads a file called 'test. txt'. you are required to write two functions that build a wordlist out of all of the words found in the file and print all of the unique words found in the file. remove punctuations using 'string. punctuation' and 'strip()' before adding words to the wordlist. write a function build_wordlist() that takes a 'file pointer' as an argument and reads the contents, builds the wordlist after removing punctuations, and then returns the wordlist. another function find_unique() will take this wordlist as a parameter and return another wordlist comprising of all unique words found in the wordlist. example: contents of 'test. txt': test file another line in the test file output: ['another', 'file', 'in', 'line', 'test', 'the']
Answers: 1
image
Computers and Technology, 22.06.2019 20:00, Jana1517
What is the worst-case complexity of the maxrepeats function? assume that the longest string in the names array is at most 25 characters wide (i. e., string comparison can be treated as o( class namecounter { private: int* counts; int nc; string* names; int nn; public: namecounter (int ncounts, int nnames); int maxrepeats() const; }; int namecounter: : maxrepeats () { int maxcount = 0; for (int i = 0; i < nc; ++i) { int count = 1; for (int j = i+1; j < nc; ++j) { if (names[i] == names[j]) ++count; } maxcount = max(count, maxcount); } return maxcount; }
Answers: 3
Do you know the correct answer?
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the...

Questions in other subjects: