Chapter 9. Creating a Distraction Device. With Loops, Conditionals and Scannerner
I hadn’t realized it, but I’ve been behind bars for almost a month and a half now. It might not be a long time, but I’ve already forged a great friendship with Bud. I told him about my situation, and he’s willing to help me. Today, finally, he’s going to tell me the plan that will allow me to get out of here. He hasn’t given me any hints, so I have no idea how we’re going to do it.
Bud is quite happy with my Java level. I’ve been a good student and I’m learning very quickly. The problem is that if I escape from prison, I can forget about continuing my progress. My future is very uncertain right now. I can only make daily plans; planning things from one week to the next is a luxury.

My fate is uncertain. Just today, I received a message from one of the guards: I’ve been assigned to work in the laundry. I have to report there at three o’clock, and I don’t know what my duties will be. Whatever it is, it’s surely better than sitting in the cell doing nothing.
I’ve been alone in the cell all day. It’s nice to have some privacy occasionally. Solitude sometimes helps you clear your thoughts. Time has passed very quickly and, without realizing it, I’m already on my way to the laundry. I wonder how many people work there. I might be able to make new friends.
As soon as I arrived, I realized my plan to socialize was completely ruined. Those who were waiting for me were Bud and Pedro. That’s when they told me they had a plan to break out of prison. They needed one more person and, thanks to my programming knowledge, I could join.
There was a small problem. They wanted to escape, but I didnโt. I didn’t want to spend the rest of my life on the run. I just need to get out for a while to unmask Rich. Bud knew this, so he offered me a solution. The two of them would escape, but I would return to prison after two hours. It should be enough time to get a confession from Rich, return to my cell, and have no one find out about my excursion.
It took me less than two minutes to decide. Of course, I accepted. I think these two have everything well planned out, so it seems the risk is minimal. What I didn’t know was that the plan would start the next day. Bud hadn’t been teaching me Java by chance. They needed a third person who knew how to program to carry out the plan.
The first part of the plan is simple. Well, at least my contribution. In the prison library, there are several computers that can access the entire security system. From there, Pedro will access the prison’s alarm function and make it go off for a few minutes.
Thanks to this distraction, Bud will be able to access the warden’s office, where he’ll get something that will help us with the next step. It’s important to access from one of the library computers, since if it’s done from Pedro’s or Bud’s computer, they would be discovered within minutes. Both laptops are closely monitored, and every action is logged.

There’s a small problem, and that is that Pedro won’t have much time to hack the system. The truth is that there’s a lot to program and only a few minutes to do it without being discovered. That’s where I come in. I need to create five Java programs, hidden in the library PC, which Pedro will later use to work his magic.
I have exactly one hour of time in the library. But usually the computers are in high demand, so I might only be able to use them for half an hour. It doesn’t matter, they’ve explained exactly what I need to do, so I have plenty of time.
Tomorrow is the big day. I’m a bit nervous, but tonight I’ll try to rest. It’s not easy to sleep under these circumstances, but I’ll try to think that, if everything goes well, I’ll prove my innocence and finally be able to get out of here.
Inside the Prison Library:
I arrived at the library among the first ones. My goal was to make sure I wouldn’t be left without a computer. I’ve been lucky, as today there aren’t many people wanting to use them. I have at least half an hour. It’s not much time and there’s quite a bit of work to do. As I mentioned before, my part is to create and hide several Java programs that Pedro will later use to enter the central system and create a distraction device.
Program 1
First, we’ll need to create a Program that discovers the central system’s password. The password consists of 6 digits, of which we believe we know 5. Therefore, we’ll only need to check 10 combinations.
The problem is that if the data is checked too quickly, the system might detect it. So, after the ninth attempt, we need to wait at least 30 seconds. For this, we’ll make the Program stop after 9 attempts, and to check the last one, we’ll need to type “continue” on the keyboard.
Here’s the code I used. Review it line by line and you’ll see how you can understand its operation without problems:
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Pass is the password we don't know. It's hidden in the Program.
// For now, we generate a variable for it and give it value 0.
// But in the actual prison code lines it's a real password.
int pass = 0;
// We know all the password numbers except the last one.
int incompletePass = 58987;
// We declare the variable for password guessing attempts.
int completePass;
// We create a Scanner since we'll need to input text.
Scanner scan = new Scanner(System.in);
// We create a variable to continue
// If reaching the ninth attempt Pedro should wait 30 seconds
// Then he should type "continue"
String continue = "continue";
// Our Program will have 10 attempts.
// So we create a "for" loop for it.
for (int i = 0; i < 10; i++) {
// This way we generate the last number that is added
// to our incomplete password.
completePass = incompletePass*10 + i;
i++;
// When our attempt matches the real password
// A text will be displayed
if (pass == completePass) {
System.out.println("This is the password");
break;
}
// We need to warn Pedro if the Program has made 8 attempts
// Then he decides if he wants to try the last one
if (i == 9) {
System.out.println("This is the ninth attempt, you must wait 30 seconds");
System.out.println("Type continue to proceed");
String pause = scan.nextLine();
if (!continueStr.equals(pause)) {
System.out.println("Error");
System.exit(0);
}
}
}
System.out.println("Password successfully obtained: " + pass);
}
}
Program 2
With the password, we can enter the central system; from there, we can access the alarm. This is located in a circuit where energy is generally saved and is turned off all the time. Therefore, first of all, we need to make sure that electricity is working. After this condition is met, Pedro must mark the manual fire alarm option as true. This way, he can later trigger it. Let’s prepare the code properly for him.
Let’s create a Program that looks for the boolean variable alarmActivated and marks it as true. But only if the boolean variable electricityOn is activated. If not, the Program will have to activate it.
Solution:
public class Main {
public static void main(String[] args) {
// Declaration of variables
boolean electricityOn = false;
boolean alarmActivated = false;
// Check the status of electricityOn
if (electricityOn) {
// If electricityOn is activated, we activate the alarm
alarmActivated = true;
System.out.println("Electricity is on. The alarm has been activated.");
} else {
// If electricityOn is not activated, we activate it and then activate the alarm
electricityOn = true;
alarmActivated = true;
System.out.println("Electricity was not on. Electricity and alarm have been activated.");
}
// Show final status of variables
System.out.println("Final status - Electricity: " + electricityOn + ", Alarm: " + alarmActivated);
}
}
Program 3
The alarm cannot be triggered at any time. It must be done when all guards are in their watchtowers. This way, it will take at least 60 seconds until they reach the central room to deactivate the alarm. That’s the time we need the distraction to last. In the hypothetical case that only one guard is in the central room, they could turn off the alarm in a few seconds and the plan would be ruined.
The prison system marks each watchtower with a 1 if someone is inside and with a 0 if it’s empty. Therefore, we must create a Program that establishes a 2D matrix representing the prison’s 6 watchtowers. Then, we create a boolean variable that will be true if all watchtowers are occupied. If at least one is empty, it will be false.
Solution:
public class Prison {
public static void main(String[] args) {
// Create a 2D matrix of 6 rows and 1 column to represent the 6 watchtowers
int[][] watchtowers = {
{1}, // Watchtower 1
{1}, // Watchtower 2
{1}, // Watchtower 3
{1}, // Watchtower 4
{1}, // Watchtower 5
{1} // Watchtower 6
};
// Boolean variable that indicates if all watchtowers are occupied
boolean allOccupied = true;
// Check the status of watchtowers
for (int i = 0; i < watchtowers.length; i++) {
if (watchtowers[i][0] == 0) {
allOccupied = false;
break;
}
}
// Print the result
if (allOccupied) {
System.out.println("All watchtowers are occupied.");
} else {
System.out.println("There are empty watchtowers.");
}
}
}
Program 4
In addition to the 60 seconds of sound distraction, we’ll need the complex’s lights to turn off afterward. So the plan is that, once the guards deactivate the alarm, there will be an instant blackout. Our job is to create a simple Program that changes the state of a boolean variable called turnOffLight from false to true.
Solution:
public class StateChange {
public static void main(String[] args) {
boolean turnOffLight = false; // Initial state of turnOffLight variable
System.out.println("Initial state of turnOffLight: " + turnOffLight);
// Change turnOffLight state from false to true
turnOffLight = true;
System.out.println("New state of turnOffLight: " + turnOffLight);
}
}
Program 5
Those are all the Programs that Pedro will need to provide a good distraction for Bud. But he also needs to cover his tracks. The alarm can’t go off while he’s sitting in front of a computer. It would be too obvious that he’s responsible. We need to create a very simple Program where the number of seconds needed before the alarm starts ringing is entered via keyboard. This way, Pedro can leave the library without anyone suspecting it was him. Depending on the situation, he’ll decide how many seconds he needs.
The only condition is that the entered number must be greater than 600. That equals 10 minutes, which will give Pedro enough time to reach his cell and avoid any suspicion.
Solution:
import java.util.Scanner;
public class Prison {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a number
System.out.print("Enter a number greater than 600: ");
int number = scanner.nextInt();
// Verify if the number is greater than 600
if (number > 600) {
System.out.println("The number " + number + " is correct.");
} else {
System.out.println("The number is not correct. It must be greater than 600.");
}
// Close the scanner
scanner.close();
}
}