Learn Java Easy. Chapter 17. Decoding Communication Systems

Chapter 17. Decoding Communication Systems

Chapter 17. Decoding Communication Systems

My fellow escapees have given me homework. I think what they want is to help me keep my mind occupied so I’ll be a bit calmer. Bud is going to access the guards’ communication system and has asked me to help him.

If anyone finds out we’ve escaped before I can get back to prison, the plan will have been a complete failure, at least for me. Everything is well calculated so that doesn’t happen, but if we run into any jailers, we must prevent them from raising the alarm. To do this, we’ll create a firewall.

Inside the prison, the situation will be a bit different. If a guard discovers us, we’ll have no choice but to subdue and tie him up. As long as he can’t call for reinforcements, the plan will continue.

Bud and Pedro are going to take the computer with them and, among other things, they’ll use it to monitor and block communications between the prison guards. Of course, not all communications, just the ones we’re interested in.

So, taking advantage of the fact that I just learned the for-each loop, I’m going to make the five programs they asked me for using that concept. Of course, before starting the plan, Bud and Pedro are going to review them to make sure everything is fine.

Program 1. Intercept Messages.

Initialize an array with three elements:

  • “Message 12: sector 12”
  • “Message 6: sector 6”
  • “Message 14: sector 14”

Use a for-each loop to print the current message to the console preceded by the string “Intercepted message: “.

Solution:

public class Main {

    public static void main(String[] args) {
        String[] messages = {
            "Message 12: sector 12",
            "Message 6: sector 6",
            "Message 14: sector 14"
        };

        for (String message : messages) {
            System.out.println("Intercepted message: " + message);
        }
    }

}

In case some messages from another sector slip through, we should do something to filter them and avoid false positives. Let’s remember that for us, only messages referring to sectors 6, 12, and 14 are important.

Exercise 2. Filter messages.

Initialize an array with fourteen elements:

  • “Message 1: Alert in sector 1”, “Message 2: Alert in sector 2”, “Message 3: Alert in sector 3”, “Message 4: Alert in sector 4”, “Message 5: Alert in sector 5”, “Message 6: Alert in sector 6”, “Message 7: Alert in sector 7”, “Message 8: Alert in sector 8”, “Message 9: Alert in sector 9”, “Message 10: Alert in sector 10”, “Message 11: Alert in sector 11”, “Message 12: Alert in sector 12”, “Message 13: Alert in sector 13”, “Message 14: Alert in sector 14”

Use a for-each loop to filter and display important messages:

Inside the for-each loop, check if the current message contains the number “6”, “12” or “14”.

If the condition is met, print the current message to the console preceded by the string “Important message: “.

Solution

public class Main {

    public static void main(String[] args) {
        String[] messages = {
            "Message 1: Alert in sector 1",
            "Message 2: Alert in sector 2",
            "Message 3: Alert in sector 3",
            "Message 4: Alert in sector 4",
            "Message 5: Alert in sector 5",
            "Message 6: Alert in sector 6",
            "Message 7: Alert in sector 7",
            "Message 8: Alert in sector 8",
            "Message 9: Alert in sector 9",
            "Message 10: Alert in sector 10",
            "Message 11: Alert in sector 11",
            "Message 12: Alert in sector 12",
            "Message 13: Alert in sector 13",
            "Message 14: Alert in sector 14"
        };

        for (String message : messages) {
            if (message.contains("6") || message.contains("12") || message.contains("14")) {
                System.out.println("Important message: " + message);
            }
        }
    }

}

Once we’ve identified and filtered the messages that bother us, it’s time to block them. This way we’ll prevent them from spreading and alerting all the prison guards.

Exercise 3. Block Communications

Initialize an array with three elements:

  • “Message 12: sector 12”
  • “Message 6: sector 6”
  • “Message 14: sector 14”

Use a for-each loop to display blocked messages. Inside the for-each loop, print the current message to the console preceded by the string “Blocking message:”.

Solution:

public class Main {

    public static void main(String[] args) {
        String[] messages = {
            "Message 12: Alert in sector 12",
            "Message 6: Alert in sector 6",
            "Message 14: Alert in sector 14"
        };

        for (String message : messages) {
            System.out.println("Blocking message: " + message);
        }
    }

}

In addition to blocking them, we need to redirect them to our main program. Bud and Pedro use two different computers, and both will need to be alert to know if any guard has raised the alarm.

Exercise 4. Redirect communications.

  • Initialize the array with three elements:
  • “Message 12: sector 12”
  • “Message 6: sector 6”
  • “Message 14: sector 14”
  • Use a for-each loop to iterate over show redirected messages:

Inside the for-each loop, print the current message to the console preceded by the string “Redirecting message: ” and followed by the string ” to external system”.

Solution:

public class Main {

    public static void main(String[] args) {
        String[] messages = {
            "Message 12: sector 12",
            "Message 6: sector 6",
            "Message 14: sector 14"
        };

        for (String message : messages) {
            System.out.println("Redirecting message: " + message + " to external system");
        }
    }

}

In addition to communications between the guards, there is another security measure that concerns us: camera monitoring. In the prison, artificial intelligence software is used that analyzes images and generates a message per minute. Three of those messages are harmless, but we need to create software that detects the one that incriminates us. After that, Bud will complete the software so that those communications are blocked, just as we did with the previous exercises. Below, I show you the four messages generated by the AI software. As you can imagine, the message that says ‘Alert’ is the one we need to identify and neutralize.

Exercise 5. Camera Monitoring

  • Initialize the array with four elements:
  • “Image 1: Empty hallway” “Image 2: Alarm” “Image 3: Door open” “Image 4: All clear”
  • Use a for-each loop to iterate over each element of the images array. Detect Suspicious Activity.

Inside the for-each loop, check if the current image contains the words “Door open” or “Guard”.

If the condition is met, print the message to the console preceded by the string “Suspicious activity detected: “.

If the condition is not met, print the message to the console preceded by the string “Normal monitoring: “.

Solution:

public class Main {

    public static void main(String[] args) {

        String[] images = {
            "Image 1: Empty hallway",
            "Image 2: Alarm",
            "Image 3: Door open",
            "Image 4: All clear"
        };

        for (String image : images) {

            if (image.contains("Door open") || image.contains("Guard")) {
                System.out.println("Suspicious activity detected: " + image);
            } else {
                System.out.println("Normal monitoring: " + image);
            }

        }

    }

}

I’ve done my part. With these programs, Bud and Pedro have everything they need. From now on, it’s time for me to rest and mentally prepare for the final phase of the escape. So far, everything is going perfectly and there have been no setbacks. I hope the good streak continues.

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