Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CO SCI 141 Programming In Java The lockers that are opened have an interesting p

ID: 3720784 • Letter: C

Question

CO SCI 141 Programming In Java

The lockers that are opened have an interesting property, what is it?

public class Exercise07_23 {
public static void main(String[] args) {

boolean[] lockers = new boolean[100];

for (int student = 1; student <= 100; student++) {
shuffle(lockers, student);
}
display(lockers);
}

public static void shuffle(boolean[] lockersOpenState, int start) {

int nextLocker = start;
for (int i = start - 1; i < lockersOpenState.length; i += nextLocker) {
lockersOpenState[i] = !lockersOpenState[i];

}
}


public static void display(boolean[] lockers) {

int lockerCount = 0;
for (int i = 0; i < lockers.length; i++) {

if (lockers[i]) {
System.out.printf("L%d ", i+1);
lockerCount++;
if (lockerCount % 10 == 0) System.out.println("");
}


}

}


}

Explanation / Answer

all squares of numbers from 1 to 10 are opened so, lockers 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 are open

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote