Learn Java Easy. Chapter 11. Hacking the Security System Again

Chapter 11. Hacking the Security System Again

Chapter 11. Hacking the Security System Again

The day has come, the escape has officially begun, and from today, there is no turning back. Peter must already be in the library, so if everything goes as we expect, the alarm will sound shortly. Bud is already ready with his computer; without it, he couldn’t reach the director’s office. We say nothing; Bud stares at the screen, very focused.

Not every day do they let him have the laptop in the cell, but today he made sure it was so. He managed to make the kitchen ventilation system stop working and, in theory, he is trying to fix it.

Breaking into the director’s office has a single goal: to access the director’s notebook, where he manually writes everything down. He is an elderly man who doesn’t like computers much. His name is Vincent. The information we are trying to get is very valuable, as it will serve us for 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 assault on Vincent’s office had begun.

To make the wait more enjoyable, let’s put ourselves in Bud’s shoes and program all the necessary code 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.

Program 1. Unlock the Main Door

Bud needs to unlock the main door of his cell. To do this, he must write a program that verifies a security code before entering it. If the wrong code is entered, the door could lock. The correct code is stored in an array and must be compared with user input.

Instructions:

  • Create an array that contains 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. Please try again.");
        }
    }
}

Program 2. Avoid the Security Camera

Bud needs to move through the first hallway 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.

Instructions:

  • Use a for loop to simulate the seconds of 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 at which 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 you plan to move (0-59):");
        int second = scanner.nextInt();

        if (second >= 0 && second < 10) {
            System.out.println("The camera is on. Please wait a moment.");
        } else if (second >= 10 && second < 60) {
            System.out.println("The camera is off. You can move!");
        } else {
            System.out.println("Invalid input. Please try again.");
        }
    }
}

Program 3. Increase the Hallway Temperature

The office is located in an area separated from the main pavilion. The guard will stay there, right in front of the door we need to access. Bud will have to avoid him by increasing the hallway temperature so that the guard is forced to go to the central room.

Instructions:

  • Enter the initial hallway temperature.
  • Enter the number of degrees you want to increase.
  • Add the desired increase to the initial temperature.
  • Display the new hallway temperature after the adjustment.

Solution:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the initial hallway temperature (in degrees Celsius): ");
        double initialTemperature = scanner.nextDouble();

        System.out.print("Enter the number of degrees to increase: ");
        double increase = scanner.nextDouble();

        double newTemperature = initialTemperature + increase;

        System.out.println("The new hallway temperature is: " + newTemperature + "°C");

        scanner.close();
    }
}

Program 4. Security Code Based on Current Time

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:

  1. Use the LocalTime class to get the current time.
  2. Generate a security code by adding the current minutes and seconds.
  3. Display the generated code to the user.
  4. Ask the user to enter the displayed code.
  5. 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) {

        LocalTime now = LocalTime.now();
        int minutes = now.getMinute();
        int seconds = now.getSecond();

        int generatedCode = minutes + seconds;

        System.out.println("The generated security code is: " + generatedCode);

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the security code to validate:");
        int enteredCode = scanner.nextInt();

        if (enteredCode == generatedCode) {
            System.out.println("Correct code. The door is open.");
        } else {
            System.out.println("Incorrect code. Please try again.");
        }

        scanner.close();
    }
}

Program 5. Menu to Prepare Coffee or Clean the Coffee Maker

Bud must activate the coffee maker’s self-cleaning function to distract the guard. The program should allow preparing coffee, activating cleaning, or exiting.

Instructions:

  • Implement a simple menu to select prepare coffee, clean the coffee maker, or exit.
  • Display messages indicating the start and end of cleaning.
  • Allow repeating the process until the user decides to exit.

Solution:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        boolean continueProgram = true;

        while (continueProgram) {

            System.out.println("\n--- Coffee Maker Menu ---");
            System.out.println("1. Prepare coffee");
            System.out.println("2. Activate manual cleaning function");
            System.out.println("3. Exit");

            System.out.print("Choose an option: ");

            int option = scanner.nextInt();

            switch (option) {
                case 1:
                    System.out.println("Preparing coffee... Enjoy your drink!");
                    break;

                case 2:
                    System.out.println("Starting manual cleaning function...");
                    System.out.println("The coffee maker is cleaning...");
                    System.out.println("Cleaning finished! The coffee maker is ready to use.");
                    break;

                case 3:
                    System.out.println("Exiting the program. See you later!");
                    continueProgram = false;
                    break;

                default:
                    System.out.println("Invalid option. Please choose a menu option.");
                    break;
            }
        }

        scanner.close();
    }
}

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