Chapter 11. Hacking the Security System Again
The day has arrived, the escape has officially begun and, from today, there’s no turning back. Pedro must already be in the library, so if everything happens as we expect, the alarm will sound shortly. Bud is already prepared with his computer; without it, he couldn’t reach the warden’s office. We don’t say anything; Bud stares at the screen intently, deeply focused.
They don’t let him have the laptop in his cell every day, but today he made sure it would be there. He managed to make the kitchen’s ventilation system stop working and, in theory, he’s trying to repair it.
Breaking into the warden’s office has a single objective: to access the warden’s notebook, where he writes everything down manually. He’s an elderly gentleman who doesn’t like computers much. His name is Vicente. The information we’re trying to get is very valuable, as it will help us with the next phases of the plan. Finally, the moment arrived: the alarm went off. It was much louder than I had imagined. I saw Bud start typing at full speed. The raid on Vicente’s office had begun.

To make the wait more pleasant, we’re going to put ourselves in Bud’s shoes and program all the code necessary to carry out this part of the plan. If we want to reach the end, we need to complete five steps and also have some luck.
Exercise 1. Bud needs to unlock the main door of his cell. To do this, he must write a Program that verifies a security code before inserting it. If the wrong code is entered, the door could lock. The correct code is stored in an array and must be compared with a user input.
Instructions:
- Create an array containing the security code, for example: {1, 4, 5, 7, 9}.
- Ask the user to enter a five-digit code.
- Compare the entered code with the code stored in the array.
- If the code is correct, display a success message. If not, display an error message.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] securityCode = {1, 4, 5, 7, 9};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the 5-digit security code:");
int[] enteredCode = new int[5];
for (int i = 0; i < 5; i++) {
enteredCode[i] = scanner.nextInt();
}
boolean isCorrect = true;
for (int i = 0; i < 5; i++) {
if (enteredCode[i] != securityCode[i]) {
isCorrect = false;
break;
}
}
if (isCorrect) {
System.out.println("Correct code. The door is unlocked.");
} else {
System.out.println("Incorrect code. Try again.");
}
}
}
Exercise 2. Avoiding the Security Camera. Bud needs to move through the first corridor without being detected by a security camera that turns on and off at regular intervals. He must write a Program that determines if he can move at a given moment.
Actually, Bud’s Program is a bit more elaborate than what we’re going to do, since he doesn’t know in which seconds the cameras turn on. He gets that information directly thanks to his connection to the central system, and his Program works with that data.
Instructions:
- Use a for loop to simulate the seconds in a minute (0 to 59).
- The camera is on during the first 10 seconds of each minute and off during the next 50 seconds.
- Ask the user to enter a second when they plan to move.
- Determine if the camera will be on or off at that second and display a corresponding message.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the second when you plan to move (0-59):");
int second = scanner.nextInt();
if (second >= 0 && second < 10) {
System.out.println("The camera is on. Wait a bit longer.");
} else if (second >= 10 && second < 60) {
System.out.println("The camera is off. You can move!");
} else {
System.out.println("Invalid input. Try again.");
}
}
}
Exercise 3. There’s another small problem: the office is located in an area separate from the main pavilion. This means that the fire alarm won’t sound here. Therefore, the guard will remain there, right in front of the door we need to access. Bud will have to avoid him. The idea is simple yet very effective: the plan consists of increasing the corridor’s temperature until the guard can’t stand it anymore and is forced to go to the central room to lower the degrees. This will give Bud enough time to enter and exit the office unseen. The system functions that don’t require a high level of security, like thermostat regulation, aren’t controlled, so Bud will be able to modify the temperature without anyone detecting it.
Create a Program that meets the following requirements:
- Enter the initial corridor temperature
- Enter the number of degrees you want to increase
- Add the desired increase to the initial temperature
- Display the new corridor temperature after the adjustment
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create a scanner to read user input
Scanner scanner = new Scanner(System.in);
// Ask user for initial temperature
System.out.print("Enter the initial corridor temperature (in Celsius): ");
double initialTemperature = scanner.nextDouble();
// Ask user for desired temperature increase
System.out.print("Enter the number of degrees to increase: ");
double increase = scanner.nextDouble();
// Calculate the new temperature
double newTemperature = initialTemperature + increase;
// Display the result
System.out.println("The new corridor temperature is: " + newTemperature + "Β°C");
// Close the scanner
scanner.close();
}
}
Exercise 4. Password to Enter the Office. Bud needs to generate a security code based on the current time (minutes and seconds). This code changes every minute and must be entered correctly to unlock a door.
Instructions:
- Use the LocalTime class to get the current time.
- Generate a security code by adding the current minutes and seconds.
- Show the generated code to the user.
- Ask the user to enter the displayed code.
- Verify if the entered code is correct and display an appropriate message.
Solution:
import java.time.LocalTime;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Get current time
LocalTime now = LocalTime.now();
int minutes = now.getMinute();
int seconds = now.getSecond();
// Generate security code by adding minutes and seconds
int generatedCode = minutes + seconds;
// Display the generated code
System.out.println("The generated security code is: " + generatedCode);
// Ask user to enter the displayed code
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the security code to validate:");
int enteredCode = scanner.nextInt();
// Verify if entered code is correct
if (enteredCode == generatedCode) {
System.out.println("Correct code. The door is open.");
} else {
System.out.println("Incorrect code. Try again.");
}
}
}
Exercise 5. Once inside, Bud will need to grab the notebook and get out as quickly as possible. Although probably, by the time he finds it, the guard will have already returned to his post. To leave unseen, the guard needs to be moved again. In the same room where the guard lowered the corridor temperature, there’s a coffee machine. It’s broken and, every time it starts its self-cleaning process, it makes a terrible noise. It needs to be turned off manually. So the idea is to activate this self-cleaning and force the guard to go turn it off.
Fortunately for Bud, this Program is already created, since the coffee maker comes programmed from the factory; even so, we are going to create one. Since we have barely used switch-case conditionals, I think it’s a good time for you to review them.
Instructions:
- Implement a simple menu that allows the user to select between making coffee or cleaning the coffee maker.
- When the user chooses the option to clean the coffee maker, display messages indicating that cleaning has started and finished.
- Allow the user to repeat the process as many times as desired until selecting the option to exit the Program.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Variable to control if the Program keeps running
boolean continue = true;
// Main Program loop
while (continue) {
// Display menu
System.out.println("\n--- Coffee Maker Menu ---");
System.out.println("1. Make coffee");
System.out.println("2. Activate manual cleaning function");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
// Read user's selected option
int option = scanner.nextInt();
// Process selected option
switch (option) {
case 1:
System.out.println("Making coffee... Enjoy your drink!");
break;
case 2:
System.out.println("Starting manual cleaning function...");
// Cleaning process simulation
System.out.println("Coffee maker is cleaning...");
System.out.println("Cleaning finished! Coffee maker is ready to use.");
break;
case 3:
System.out.println("Exiting Program. Goodbye!");
continue = false; // Exit loop
break;
default:
System.out.println("Invalid option. Please choose an option from the menu.");
break;
}
}
// Close scanner
scanner.close();
}
}