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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 22:00, karena13aguirre
Problems: 1. using textbooks, reference books, and internet as your source of research, draw the following microprocessor microarchitectures i. intel 8086 ii. motorola 68000 i atmel atmega32 iv. mips single cycle v. arm cortex-m3 write an hdl module for a hexadecimal seven-segment display decoder. the input is 4-bit binary representing a hex number (0-f), and the output is 8-bit seven segment display bits (a-h). thus, the decoder must handle the digits 10 - 15 to display a-f respectively, in addition to 0-9 numbers. 2. design a 4-bit left and right rotator (both outputs). first sketch schematic diagrams of your design. then implement your design using hdl coding. 3. 4. design a modified priority encoder that receives an 8-bit input, a7: 0 and produces a 3-bit output, y2o. y indicates the most significant bit of the input that is true. y should be 0 if none of the inputs are true. give a simplified boolean equation, sketch a schematic, and write an hdl code. 5.write an 8: 1 multiplexer module called mux8 with selection inputs s, data input d, and data output y. data input (d) and data output (v) are 32-bit wide
Answers: 3
image
Computers and Technology, 22.06.2019 00:40, 1fuzzybirdow6e0s
If you arrive at the same time as another user straight across from you yield if a. they flash your headlights at you b. you can’t see their turn signals c. you’re going street and they’re running d. you’re turning they’re going straight plz
Answers: 1
image
Computers and Technology, 23.06.2019 15:30, yanicas
Hey so i was just trying out some game hacks so i took a paste from online and built it in my visual studio and then suddenly my computer was working or clicking on stuff on its own am i hacked?
Answers: 1
image
Computers and Technology, 24.06.2019 02:30, talia43
Assume a class window with accessor method getwidth that accepts no parameters and returns an integer. assume further an array of 3 window elements named winarr, has been declared and initialized. write a sequence of statements that prints out the width of the widest window in the array.
Answers: 2
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 in other subjects:

Konu
Biology, 18.12.2019 17:31