Chapter 13. Gathering Evidence Against Phil
After the incident with the alarm and subsequent blackout, prison officials are on alert. They are beginning to suspect something, and extraordinary precautionary measures are being taken. Most of them don’t affect us, but to access the prison hospital, the first thing Bud needs to do is deactivate the GPS location of his laptop. After what happened with the fire alarm, many security measures are being taken, and everyone is being monitored. It’s very likely that prison officials already suspect Bud and Pedro. From now on, they need to be completely invisible.
Exercise 1. Changing GPS signal: Bud and Pedro must change the GPS signal of their computers so guards cannot track their real location. We will write a Program to help us with this task.
Instructions:
- Create an array containing the real coordinates of each computer.
- Allow the user to enter fake coordinates.
- Verify if the entered coordinates are within an acceptable range (for example, within a 1 km radius of the real coordinates).
- If the coordinates are acceptable, display a success message. If not, display an error message.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] realCoordinates = {40.7128, -74.0060}; // (example)
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the fake coordinates one by one (latitude and longitude):");
double fakeLatitude = scanner.nextDouble();
double fakeLongitude = scanner.nextDouble();
double distance = Math.sqrt(Math.pow(fakeLatitude - realCoordinates[0], 2) + Math.pow(fakeLongitude - realCoordinates[1], 2));
if (distance <= 0.01) { // Approximately 1 km radius
System.out.println("Fake coordinates accepted. The guards won't be able to track you.");
} else {
System.out.println("Fake coordinates out of range. Please try again.");
}
}
}
Just after entering the hospital, there’s a room with medium security level that has five cameras. Additionally, officers are always checked when entering to prevent them from bringing objects from outside. The problem is that they are not checked when leaving, which allows Phil to steal materials. In that room, Bud must avoid the cameras.
The first thing to do is to check that all cameras are turned on, since, if they are, Bud will be able to intercept and turn them off. If any camera is off beforehand, it cannot be intercepted. Just because our Program indicates that a camera is off doesn’t necessarily mean it really is; it might be using another type of power source to function. That would be too big a risk. Therefore, the objective is to verify that all cameras are active and then turn them off.
Exercise 2. Intercepting Video Signals. Bud needs to intercept the video signals from the security cameras. Write a Program that simulates the interception of video signals and displays a message each time a signal is intercepted.
Instructions:
- Declare 5 boolean variables. (In the real prison Program, we won’t know if they are declared as true or false. We’ll just declare them with random values to see if the Program works)
- Use a for loop to simulate the interception of video signals from multiple cameras (for example, 5 cameras). Make it check the status of all 5 cameras.
- Display a message indicating that each camera’s signal has been intercepted. True means it’s on and therefore the Program intercepts it. False means it’s off and the Program won’t be able to intercept it.
Solution:
public class Main {
public static void main(String[] args) {
// Boolean variable declaration
// As mentioned in the statement, in the prison system we won't know the status of the cameras. That's why we create this Program.
// We simply add values to see if the Program works.
boolean camera1 = true;
boolean camera2 = false;
boolean camera3 = true;
boolean camera4 = false;
boolean camera5 = true;
// For loop to check the status of each camera
for (int i = 1; i <= 5; i++) {
// We create a boolean status. We need to assign a value to avoid an error.
// It doesn't really matter if it's true or false. It will change depending on each camera.
boolean status = false;
if (i == 1) {
status = camera1;
} else if (i == 2) {
status = camera2;
} else if (i == 3) {
status = camera3;
} else if (i == 4) {
status = camera4;
} else if (i == 5) {
status = camera5;
}
if (status) {
System.out.println("Camera " + i + " status: Intercepted");
} else {
System.out.println("Camera " + i + " status: Not intercepted");
}
}
}
}
In the next room, the security level is high. They no longer use cameras, but motion sensors, specifically five of them. To reach the computer room, it’s necessary to cross this room.
Exercise 3: Deactivating Motion Sensors. Now Bud must deactivate the motion sensors to move without being detected. Write a Program that allows deactivating the sensors one by one.
Instructions:
- Use a boolean array to represent the status of the sensors (activated or deactivated).
- Use a loop to allow the user to deactivate each sensor.
- Show the status of the sensors after each change.
Solution:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean[] sensors = {true, true, true, true, true}; // All sensors are initially activated
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < sensors.length; i++) {
System.out.println("Do you want to deactivate sensor " + (i + 1) + "? (true/false)");
sensors[i] = !scanner.nextBoolean();
}
System.out.println("Sensors status:");
for (int i = 0; i < sensors.length; i++) {
System.out.println("Sensor " + (i + 1) + ": " + (sensors[i] ? "Activated" : "Deactivated"));
}
}
}
Protecting the computer room, there’s a door that requires a numeric access code. If an incorrect or non-numeric password is entered, the Program will detect it and an alert will be activated. We need to create a Program using try and catch that warns in case a non-numeric or incorrect code is entered. This is a precaution in case the code stolen from Vicente doesn’t work. If that happened, the plan would be compromised and we couldn’t escape, but at least we wouldn’t be discovered trying to flee.
Exercise 4. Exception Handling when Hacking the Security System. Bud tries to access the security system, but can make mistakes during the process. Write a Program that simulates hacking and handles possible exceptions (such as entering a non-numeric value).
Instructions:
- Ask the user to enter a numeric code.
- Use try and catch to handle possible exceptions if the user enters a non-numeric value.
- If the value 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) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Enter the hacking code (integer number):");
int code = Integer.parseInt(scanner.nextLine());
System.out.println("Correct code. Hack successful.");
} catch (NumberFormatException e) {
System.out.println("Error: You entered a non-numeric value. Please try again.");
}
}
}
Once inside the last room, we need to access the computer. This last password works in a peculiar way. It uses a new code each time to gain access. This makes it practically impossible to hack. To access the system, we’ll need a key obtained from Vicente’s office that is then randomly combined with two other numbers. Before entering it, we need to check exactly what those two numbers are.
Exercise 5. Create a Program that finds the complete 6-digit key from a 4-digit base key. The base key is combined with a variable two-digit number to form a complete key.
- Create three int variables, one for the incomplete password we have, another for the final password, and a temporary one that we’ll use within our for loop to check all combinations from 00 to 99.
- Use a for loop to iterate from 0 to 99, representing all possible combinations of the last two digits.
- In each iteration of the loop, combine the base key with the current loop number multiplied by 100, adding the loop value. This generates a possible complete key (checkedKey).
- Compare the key generated in each iteration (checkedKey) with the known complete key. If a match is found, print the complete key found and end the loop.
In the Program that Bud uses to find the password, obviously he wouldn’t have the final password. But we have included it in this exercise to verify that it works properly.en.
Solution:
public class Main {
public static void main(String[] args) {
// Known base key
int baseKey = 8787;
// Complete key to be found
int completeKey = 878793;
// This key will increase in our loop one by one
int checkedKey;
// Check combinations of the last two digits
boolean found = false;
for (int i = 0; i < 100; i++) {
checkedKey = (baseKey * 100) + i;
System.out.println(checkedKey);
if (checkedKey == completeKey) {
System.out.println("This is the complete key:" + checkedKey);
break;
}
}
}
}