Learn Java Easy. Chapter 13. Gathering Evidence Against Phil

Chapter 13. Gathering Evidence Against Phil

Chapter 13. Gathering Evidence Against Phil

After the alarm incident and the subsequent blackout, prison officials are on high alert. They’re starting to suspect something and are taking extraordinary precautionary measures. Most of these don’t affect us, but in order to access the prison hospital, the first thing Bud will have to do is disable the GPS location on 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, we must be completely invisible.

Exercise 1. Change GPS Signal: Bud and Pedro must change the GPS signal of their computers so that guards cannot track their real location. We’ll write a program to help with this task.

 

Instructions:

  1. Create an array that contains the real coordinates of each computer.
  2. Allow the user to enter fake coordinates.
  3. Verify if the entered coordinates are within an acceptable range (for example, within a 1 km radius of the real coordinates).
  4. 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: New York)
        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. Guards won't be able to track you.");
        } else {
            System.out.println("Fake coordinates out of range. Please try again.");
        }

        scanner.close();
    }
}

Upon entering the hospital, there's a room with medium security level that has five cameras. Additionally, officials are always searched when entering to prevent them from bringing in outside objects. The problem is that they're not searched when leaving, which allows Phil to steal materials. In this room, Bud will have to avoid the cameras.

The first thing to do is check that all cameras are turned on, because if they are, Bud can intercept and turn them off. If any are already turned off, they cannot be intercepted. Our program indicating that a camera is off doesn't necessarily mean it actually is; it might use another type of power source to operate. That would be a very high risk. Therefore, the objective is to verify that all are active and then turn them off.

Exercise 2. Intercept 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:

  1. Declare 5 boolean variables. (In the real prison program, we won't know if they're declared as true or false. We simply declare them with random values to see if the program works)
  2. Use a for loop to simulate the interception of video signals from several cameras (for example, 5 cameras). Have it check the status of the 5 cameras.
  3. 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 cannot intercept it.

 

Solution:

 

public class Main {

    public static void main(String[] args) {
        // Declaration of boolean variables
        // 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 with test values to check its operation.

        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++) {
            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 + " is in state: Intercepted");
            } else {
                System.out.println("Camera " + i + " is in state: Not intercepted");
            }
        }
    }
}

In the next room, the security level is high. Cameras are no longer used, but motion sensors are used instead, specifically five of them. To reach the computer room, it's necessary to cross this room.

Exercise 3: Disable Motion Sensors. Now Bud will have to disable the motion sensors to move without being detected. Write a program that allows disabling the sensors one by one.

 

Instructions:

 

  1. Use a boolean array to represent the status of the sensors (activated or deactivated).
  2. Use a loop to allow the user to deactivate each sensor.
  3. Display 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)");
            boolean response = scanner.nextBoolean();
            sensors[i] = !response; // If the user says true (wants to deactivate it), we set it to false
        }

        System.out.println("Sensor status:");
        for (int i = 0; i < sensors.length; i++) {
            System.out.println("Sensor " + (i + 1) + ": " + (sensors[i] ? "Activated" : "Deactivated"));
        }

        scanner.close();
    }
}

 

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 triggered. We need to create a program using try and catch that warns if 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 he might make mistakes during the process. Write a program that simulates the hacking and handles possible exceptions (such as entering a non-numeric value).

 

Instructions:

  1. Ask the user to enter a numeric code.
  2. Use try and catch to handle possible exceptions if the user enters a non-numeric value.
  3. 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):");
            int code = Integer.parseInt(scanner.nextLine());
            System.out.println("Correct code. Hacking successful.");
        } catch (NumberFormatException e) {
            System.out.println("Error: You entered a non-numeric value. Please try again.");
        }

        scanner.close();
    }
}

 

Once inside the last room, we need to access the computer. This last password works in a peculiar way. Each time it uses a new code to enter. 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 generated key in each iteration (checkedKey) with the known complete key. If a match is found, print the complete key found and terminate the loop.

 

In the program Bud uses to find the password, he obviously wouldn't have the final password. But we've included it in this exercise to check that it works properly.

 

Solution:

 

public class Main {

    public static void main(String[] args) {

        // Known base key
        int baseKey = 8787;

        // Complete key to be found
        int completeKey = 878793;

        // Variable to store the key being checked
        int checkedKey;

        // Check combinations of the last two digits
        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;
            }
        }
    }
}

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏