Learn Java Easy. Chapter 15. Bypassing the Timer System

Chapter 15. Bypassing the Timer System

The hallway leading to the prison exit is the most heavily guarded. You might think it’s because of fear that prisoners might escape through there, but that’s not the case. What happens is that everyone enters and exits through the same place. Both visitors and officers and prisoners always follow the same path.

 

Every time someone enters the prison, they are searched and the images are recorded. Additionally, you must pass through three registration points, divided into various rooms. At each stop, there is a guard who doesn’t leave their post until relief arrives.

 

 

 

 

At the end of the hallway, there are two doors. One leads to the cells and the other to a waiting room where, in turn, we find other doors. From there you can access other parts of the prison, but they don’t interest us since we don’t need to set foot in them.

 

Knowing that there are at least five guards guarding the exit at all times, it seems unwise to try to use that route. However, not everything is as it seems, since during the night there are no visits and, after the shift change at 22:00, no one else enters. That means they don’t need to have five people guarding the entrance.

 

Bud believes that, at most, there will be two people. It’s even possible that at some point they might need to perform some task during their shift and there might be a short period of time when we could be alone.

 

The plan is to identify, measure, and analyze the jailers’ activities to know exactly what time we should attempt the escape. To do this, the first step will be to analyze when doors are opened and closed. That way, we can identify when there is movement. We’ll create a Program that calculates the inactivity time of the door leading to the waiting room. We can visually control the door leading to the cells, and we don’t need to worry about the street door, since no one uses it during the night.

 

We have a small advantage: with one of the keys obtained from Vicente’s office, Pedro has managed to access the seven door activity logs from last week, one for each day. With this, we can already get an idea of which day is the quietest and the hours when the guards enter and exit. The first step will be to determine which day we choose. For this, we need to create a Program that reads the periods of door activity and adds them up. We will choose the escape day that has the least total activity time.

 

The file divides the day into minutes, and each line tells us how many seconds the door was active in that minute. To give you an idea, it has this format:

 

 

0
0
0
0
60
60
12
0
0

 

 

Exercise 1. Recording Activity Time. We need to record the doors’ activity time to choose a day for our escape. For this, we need to write a Program that reads the inactivity time from a file and records the total inactivity time in another file.

 

Instructions:

 

  1. We use the activity.txt file obtained by Pedro, with activity times in seconds (one per line).
  2. Read the activity times from the file.
  3. Calculate the total inactivity time.
  4. Write the total inactivity time to a file total_activity.txt

 

Solution:

 

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) {

        String inputFile = "activity.txt";

        String outputFile = "total_activity.txt";

        try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
             BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {

            String line;
            int totalActivity = 0;

            while ((line = br.readLine()) != null) {
                totalActivity += Integer.parseInt(line);
            }

            bw.write("Total activity time: " + totalActivity + " seconds");


        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}

Monday is the day with the least activity, so we already know when we should attempt the escape. According to the record, from minute 1322 to 1438, that is, from 22:02 until 23:58, the door is inactive. Then, it becomes active again at approximately 2:00 and subsequently every hour until 6:00, when the main door opens and fills with police. On each occasion, the doors are active and open for less than two minutes.

Once we know at what times the doors open, we need to determine if they were opened to let someone in or perhaps for someone to exit. The officers must perform a series of tasks during their guard duty. Pedro has gained access to the record of these night tasks and the times that guards mark them as completed. We need to create a small Program that records these activities and returns them in a text format. We know that their main activities are: regular patrolling, inspection and maintenance, and cleaning and disinfection.

 

The log file looks like this:

 

“iRKJi2KpMW”, “ixpFHX0v8s”, “rMxNXbeUxC”, “bjO9IUsFGc”, “LpTB2ffO8w”, “S4WwZffjqB”, “HuuSb3RH6l”, “O8OmH1ApJx”, “LpFKbFT7hU”, “TZ9BmcroA7”, “dyLWJCvbU6”, “qNlWgK2wzD”, “s6vEOgmfsY”, “ccDtnysyec”, “waITKPmWbY”, “HH9N0Ufih4”, “cGIet9lE4y”, “MCwCJ6LNQP”, “OfecGXWhqY”, “SOd3Tco6mI”, “r8QwQDbHyp”, “4V6bJhJ20x”, “nzkiWhmxVj”, “0tT2nDToVa”, “67NEMfhs2x”, “W3HXrDI7SJ”, “KgGoiFJfMN”, “2WK7UZVmgX”, “3KNx0fDu54”, “Rn20v6baaz”, “t8jChEgFSz”, “489dXWEFi6”, “oNSsEfNwaP”, “V4zx2r6XR8”, “2dPXcKbZNQ”, “WpsxQvEbVw”, “7f6floF3VU”, “W1joPNEiK1”, “pXpGUMRsZq”, “1M1jDBpiGu”, “xjZBVieX1Q”, “y3dVBuhTrJ”, “s0YB6CVSFV”, “vZQgDge2HJ”, “EsWhUsnhLa”, “hevhsUt0ux”, “bYwU4Qy6Gl”, “sCW32VXSfD”, “mwCdwQ809e”, “zyRwiYkfDk”, “ko4o26MErP”, “Lk1rspAJQK”, “d5Y9IPuMk5”, “yKj992kdnn”, “Q1XsuVqUGr”, “fHMDmXiAcp”, “dql0nmQJcm”, “qRqSHARwSA”, “CgV8ne6fRG”, “30DYrnKWoA”, “r1QrdR0es2”, “HXTV8XvYoP”, “GQAWwyhMSL”, “4yMmh7e5NW”, “yT2kIJvDqN”, “AbIqpbKj5h”, “haAriF42gR”, “O6MciwEwQ4”, “hW8Pf9IkX7”, “kUaN7Cnv96”, “tAROxtCUiP”, “aZEZynwwpj”, “cqzR5Y3Ipi”, “M69Qr3BWJj”, “ySTegpfDxw”, “f47AKb3Bsn”, “XOsmCGvcSo”, “wA7iC3Fq1k”, “NqTC2OpWGr”, “IvvkjHR0Q2”, “JyT5lu4QPo”, “2whhr0ixDg”, “xcYX1ELvDi”, “zTS8EJE0hh”, “7gYXpaaPIs”, “UiZ8tMlvfl”, “uuSBzI6IfZ”, “BdcQ3ci06N”, “VNrQV6uZvU”, “gZILASO2gT”, “iOIRRKFxhc”, “jkSGZFtIpw”, “2gkQKh6J8L”, “jnTyMxcG8I”, “X07Mj6M0Rj”, “SqjfH3cyLO”, “GfKiA3DZ4w”, “RBIquXXt0k”, “udPC3Q0PXo”, “CM44vkuwyT”, “20n5Uwv6s8”, “5ST85ixStb”, “uliTvpFukE”, “ErQPLyDuRw”, “XX5eaAXr1a”, “nLenQQgqoP”, “ThjBwzns36”, “8RghH8Vneq”, “FWFaHu0c2a”, “HqGai14Ao6”, “nFEi87Bafj”, “rotkzVlY3k”, “Tah5BZU49N”, “YILUBnOTTh”, “kyhPp7TWZp”, “q1kiVAMsHv”, “OvKuo3mvSX”, “htDbhBg7vs”, “O8ZBRQB5cO”, “TlUDfrzdUi”, “7yIIltvZSI”, “HkAkB7qO3c”, “j4DvSZL3Ry”, “ONmRHuWdGc”, “a7LmXNTfap”, “MlHEWHZhrz”, “ADKHiZeBAS”, “0KQ6aXlsZY”, “OKMfZsps6e”, “vJpOxdfzEd”, “BKs9UTKTQJ”, “DBNrFCM5d7”, “hse8XLelZK”, “3jz3bLreNc”, “ZZSHQ35FpT”, “jOmaC6iMOW”, “iXvhHmq2pn”, “gqnfV11and”, “DDfLkv3uBg”, “UQjsb1ixF0”, “08zSNrhvxs”, “LSWyFeFyYv”, “Regular patrol”, “4Bpd1idsum”, “vCgKs8GFTI”, “uhUIpi1VLd”, “AQn63M3ANF”, “Y39NIKxIgJ”, “YcpNNmT3GX”, “C8Di70GqS7”, “Ybg4P3BOij”, “6Tb4opfNeC”, “aTmzLU9opz”, “QSHg6ZRmyH”, “lVUz2kHAOn”, “Inspection and maintenance”, “wUxaxSm9lC”, “DVTb4JEZQ9”, “sjahqvnVsk”, “TfvPongU7w”, “gQ6g0rJ7ax”, “bejbKLd6ZR”, “SMj8MjDEWY”, “fanVKMvsmZ”, “Regular patrol”, “gmqX2oBxvm”, “Mb9B4SHLvM”, “64rh8hh6Li”, “SjalZaHsOA”, “yxDLOhr4Od”, “qfN1GbZNFA”, “C4q5ridJ7I”, “aQLm7SJeEo”, “LNDWA1UHzG”, “QpYvFfSY6S”, “I7ySXRZkQH”, “WwtkFmM7pP”, “gl794m6lyl”, “6fvgHjprUx”, “SVlNByrKBN”, “DNEmWPvl51”, “Mm6NxxNkdF”, “gyLw8TP8kH”, “vcqjXfWTYz”, “bpuMR5EVFB”, “sMMP5HpxJg”, “9hU32sjepp”, “RsYnv80Hx2”, “SilPvzI3gD”, “L7pspbXgSw”, “RvAtxqkYF2”, “9gOPHDbw31”, “uUDPlKRjU3”, “NkPKW7zkZK”, “dBOiqar26k”, “GXPsNzuULq”, “KwwpvvPqQ2”, “Tif0qfs2eq”, “3xTc242fPW”, “c5jJz3DVrV”, “zrK0VW0AQH”, “zPJt2lGxKU”, “C3ssWKqU5s”, “XsfI0o5asg”, “QiR4pDd4N0”, “q4iY9xGn3V”, “msQISGxfMJ”, “yRyD1ec8fb”, “dBfcrywOUD”, “O9cMzNXF5t”, “lNuV5TSkbO”, “YEgbviK7AV”, “HKrrLQBtt2”, “ZU0hqtXuNa”

 

Note that if no activity is entered for a certain time, a random code is generated.

 

 

Exercise 2. Generate an Activity Log. Bud needs to generate an activity log to analyze the behavior of the jailers. Write a Program that records the different system activities in a file.

 

Instructions:

 

  1. Create an array with different activities (“Regular patrol”, “Inspection and maintenance”, “Cleaning and disinfection”).

 

Write these activities to a log_activities.txt file, one per line.

 

Solution:

 

import java.io.*;
public class Main {
    public static void main(String[] args) {

        String[] activities = {"iRKJi2KpMW", "ixpFHX0v8s", "rMxNXbeUxC", "bjO9IUsFGc", "LpTB2ffO8w", "S4WwZffjqB", "HuuSb3RH6l", "O8OmH1ApJx", "LpFKbFT7hU", "TZ9BmcroA7", "dyLWJCvbU6", "qNlWgK2wzD", "s6vEOgmfsY", "ccDtnysyec", "waITKPmWbY", "HH9N0Ufih4", "cGIet9lE4y", "MCwCJ6LNQP", "OfecGXWhqY", "SOd3Tco6mI", "r8QwQDbHyp", "4V6bJhJ20x", "nzkiWhmxVj", "0tT2nDToVa", "67NEMfhs2x", "W3HXrDI7SJ", "KgGoiFJfMN", "2WK7UZVmgX", "3KNx0fDu54", "Rn20v6baaz", "t8jChEgFSz", "489dXWEFi6", "oNSsEfNwaP", "V4zx2r6XR8", "2dPXcKbZNQ", "WpsxQvEbVw", "7f6floF3VU", "W1joPNEiK1", "pXpGUMRsZq", "1M1jDBpiGu", "xjZBVieX1Q", "y3dVBuhTrJ", "s0YB6CVSFV", "vZQgDge2HJ", "EsWhUsnhLa", "hevhsUt0ux", "bYwU4Qy6Gl", "sCW32VXSfD", "mwCdwQ809e", "zyRwiYkfDk", "ko4o26MErP", "Lk1rspAJQK", "d5Y9IPuMk5", "yKj992kdnn", "Q1XsuVqUGr", "fHMDmXiAcp", "dql0nmQJcm", "qRqSHARwSA", "CgV8ne6fRG", "30DYrnKWoA", "r1QrdR0es2", "HXTV8XvYoP", "GQAWwyhMSL", "4yMmh7e5NW", "yT2kIJvDqN", "AbIqpbKj5h", "haAriF42gR", "O6MciwEwQ4", "hW8Pf9IkX7", "kUaN7Cnv96", "tAROxtCUiP", "aZEZynwwpj", "cqzR5Y3Ipi", "M69Qr3BWJj", "ySTegpfDxw", "f47AKb3Bsn", "XOsmCGvcSo", "wA7iC3Fq1k", "NqTC2OpWGr", "IvvkjHR0Q2", "JyT5lu4QPo", "2whhr0ixDg", "xcYX1ELvDi", "zTS8EJE0hh", "7gYXpaaPIs", "UiZ8tMlvfl", "uuSBzI6IfZ", "BdcQ3ci06N", "VNrQV6uZvU", "gZILASO2gT", "iOIRRKFxhc", "jkSGZFtIpw", "2gkQKh6J8L", "jnTyMxcG8I", "X07Mj6M0Rj", "SqjfH3cyLO", "GfKiA3DZ4w", "RBIquXXt0k", "udPC3Q0PXo", "CM44vkuwyT", "20n5Uwv6s8", "5ST85ixStb", "uliTvpFukE", "ErQPLyDuRw", "XX5eaAXr1a", "nLenQQgqoP", "ThjBwzns36", "8RghH8Vneq", "FWFaHu0c2a", "HqGai14Ao6", "nFEi87Bafj", "rotkzVlY3k", "Tah5BZU49N", "YILUBnOTTh", "kyhPp7TWZp", "q1kiVAMsHv", "OvKuo3mvSX", "htDbhBg7vs", "O8ZBRQB5cO", "TlUDfrzdUi", "7yIIltvZSI", "HkAkB7qO3c", "j4DvSZL3Ry", "ONmRHuWdGc", "a7LmXNTfap", "MlHEWHZhrz", "ADKHiZeBAS", "0KQ6aXlsZY", "OKMfZsps6e", "vJpOxdfzEd", "BKs9UTKTQJ", "DBNrFCM5d7", "hse8XLelZK", "3jz3bLreNc", "ZZSHQ35FpT", "jOmaC6iMOW", "iXvhHmq2pn", "gqnfV11and", "DDfLkv3uBg", "UQjsb1ixF0", "08zSNrhvxs", "LSWyFeFyYv", "Regular patrol", "4Bpd1idsum", "vCgKs8GFTI", "uhUIpi1VLd", "AQn63M3ANF", "Y39NIKxIgJ", "YcpNNmT3GX", "C8Di70GqS7", "Ybg4P3BOij", "6Tb4opfNeC", "aTmzLU9opz", "QSHg6ZRmyH", "lVUz2kHAOn", "Inspection and maintenance", "wUxaxSm9lC", "DVTb4JEZQ9", "sjahqvnVsk", "TfvPongU7w", "gQ6g0rJ7ax", "bejbKLd6ZR", "SMj8MjDEWY", "fanVKMvsmZ", "Regular patrol", "gmqX2oBxvm", "Mb9B4SHLvM", "64rh8hh6Li", "SjalZaHsOA", "yxDLOhr4Od", "qfN1GbZNFA", "C4q5ridJ7I", "aQLm7SJeEo", "LNDWA1UHzG", "QpYvFfSY6S", "I7ySXRZkQH", "WwtkFmM7pP", "gl794m6lyl", "6fvgHjprUx", "SVlNByrKBN", "DNEmWPvl51", "Mm6NxxNkdF", "gyLw8TP8kH", "vcqjXfWTYz", "bpuMR5EVFB", "sMMP5HpxJg", "9hU32sjepp", "RsYnv80Hx2", "SilPvzI3gD", "L7pspbXgSw", "RvAtxqkYF2", "9gOPHDbw31", "uUDPlKRjU3", "NkPKW7zkZK", "dBOiqar26k", "GXPsNzuULq", "KwwpvvPqQ2", "Tif0qfs2eq", "3xTc242fPW", "c5jJz3DVrV", "zrK0VW0AQH", "zPJt2lGxKU", "C3ssWKqU5s", "XsfI0o5asg", "QiR4pDd4N0", "q4iY9xGn3V", "msQISGxfMJ", "yRyD1ec8fb", "dBfcrywOUD", "O9cMzNXF5t", "lNuV5TSkbO", "YEgbviK7AV", "HKrrLQBtt2", "ZU0hqtXuNa"};

        String outputFile = "activities_log.txt";

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {

            for (String activity : activities) {
                bw.write(activity);
                bw.newLine();
            }

        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}

 

 

Not all tasks are performed every day. Our plan is to escape on a Monday, so we’ll create a Program that shows us how many times each task is performed per day. We need to find one that isn’t performed at all.

 

Each task is always carried out at the same time, regardless of the day. For example, inspection and maintenance are always done from 2:00 to 3:00. Therefore, if on Monday this activity wasn’t performed at all, it would mean that during that period, the guards would have nothing to do and possibly be resting. We know at what time, in theory, each of the tasks is performed. Now we just need to verify if there’s any task that isn’t performed on Monday.

 

 

Exercise 3. We need to analyze the activity log to identify repetitive patterns during the night. If any of them do not repeat during the night we plan to escape, it means that the hallway might be empty.

 

Instructions:

 

  1. Read the log_activities.txt file.
  2. Count how many times each activity occurs.
  3. Write the results to a summary_activities.txt file.

 

Solution:

 

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {

        String inputFile = "activities_log.txt";
        String outputFile = "activities_summary.txt";

        Map<String, Integer> activityCounter = new HashMap<>();

        try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
             BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile)))
        {

            String line;
            while ((line = br.readLine()) != null) {
                activityCounter.put(line, activityCounter.getOrDefault(line, 0) + 1);
            }

            for (Map.Entry<String, Integer> entry : activityCounter.entrySet()) {
                bw.write(entry.getKey() + ": " + entry.getValue());
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

We have reviewed the log and the results are better than we expected. According to the analysis, the ‘Cleaning and disinfection’ activity is not performed on Mondays. This task is carried out between 3 and 4 in the morning. Now we know that during that hour the hallway remains inactive, without any type of activity.

 

With so much time available, we won’t have to worry about anything. It will be a piece of cake. However, we must be completely sure that there will be no surprises or unforeseen events.

 

Bud and Pedro should be satisfied, but they are too serious. Given this good news, I feel optimistic. We are just one step away from escaping. However, I think Bud is suspicious. Something is escaping us, and we will need external help to discover it.

 

Fortunately, we know a guard who can’t refuse to collaborate. Phil has informed us that two more activities have been recently implemented: ‘Camera monitoring’ and ‘Communication with control post’. We need to update the log to verify if, during the time period we thought was free, the guards are actually in the hallway performing either of these two tasks.

 

 

Exercise 4. Update Activity Log. We need to update the activity log in real time to reflect changes in the system. Write a Program that adds new activities to the activity log file.

 

The new log we have found is as follows:

 

String[] activities = {“iRKJi2KpMW”, “ixpFHX0v8s”, “rMxNXbeUxC”, “bjO9IUsFGc”, “LpTB2ffO8w”, “S4WwZffjqB”, “HuuSb3RH6l”, “O8OmH1ApJx”, “LpFKbFT7hU”, “TZ9BmcroA7”, “dyLWJCvbU6”, “qNlWgK2wzD”, “s6vEOgmfsY”, “ccDtnysyec”, “waITKPmWbY”, “HH9N0Ufih4”, “cGIet9lE4y”, “MCwCJ6LNQP”, “OfecGXWhqY”, “SOd3Tco6mI”, “r8QwQDbHyp”, “4V6bJhJ20x”, “nzkiWhmxVj”, “0tT2nDToVa”, “67NEMfhs2x”, “W3HXrDI7SJ”, “KgGoiFJfMN”, “2WK7UZVmgX”, “3KNx0fDu54”, “Rn20v6baaz”, “t8jChEgFSz”, “489dXWEFi6”, “oNSsEfNwaP”, “V4zx2r6XR8”, “2dPXcKbZNQ”, “WpsxQvEbVw”, “7f6floF3VU”, “W1joPNEiK1”, “pXpGUMRsZq”, “1M1jDBpiGu”, “xjZBVieX1Q”, “y3dVBuhTrJ”, “s0YB6CVSFV”, “vZQgDge2HJ”, “EsWhUsnhLa”, “hevhsUt0ux”, “bYwU4Qy6Gl”, “sCW32VXSfD”, “mwCdwQ809e”, “zyRwiYkfDk”, “ko4o26MErP”, “Lk1rspAJQK”, “d5Y9IPuMk5”, “yKj992kdnn”, “Q1XsuVqUGr”, “fHMDmXiAcp”, “dql0nmQJcm”, “qRqSHARwSA”, “CgV8ne6fRG”, “30DYrnKWoA”, “r1QrdR0es2”, “HXTV8XvYoP”, “GQAWwyhMSL”, “4yMmh7e5NW”, “yT2kIJvDqN”, “AbIqpbKj5h”, “haAriF42gR”, “O6MciwEwQ4”, “hW8Pf9IkX7”, “kUaN7Cnv96”, “tAROxtCUiP”, “aZEZynwwpj”, “cqzR5Y3Ipi”, “M69Qr3BWJj”, “ySTegpfDxw”, “f47AKb3Bsn”, “XOsmCGvcSo”, “wA7iC3Fq1k”, “NqTC2OpWGr”, “IvvkjHR0Q2”, “JyT5lu4QPo”, “2whhr0ixDg”, “xcYX1ELvDi”, “zTS8EJE0hh”, “7gYXpaaPIs”, “UiZ8tMlvfl”, “uuSBzI6IfZ”, “BdcQ3ci06N”, “VNrQV6uZvU”, “gZILASO2gT”, “iOIRRKFxhc”, “jkSGZFtIpw”, “2gkQKh6J8L”, “jnTyMxcG8I”, “X07Mj6M0Rj”, “SqjfH3cyLO”, “GfKiA3DZ4w”, “RBIquXXt0k”, “udPC3Q0PXo”, “CM44vkuwyT”, “20n5Uwv6s8”, “5ST85ixStb”, “uliTvpFukE”, “ErQPLyDuRw”, “XX5eaAXr1a”, “nLenQQgqoP”, “ThjBwzns36”, “8RghH8Vneq”, “FWFaHu0c2a”, “HqGai14Ao6”, “nFEi87Bafj”, “rotkzVlY3k”, “Tah5BZU49N”, “YILUBnOTTh”, “kyhPp7TWZp”, “q1kiVAMsHv”, “OvKuo3mvSX”, “htDbhBg7vs”, “O8ZBRQB5cO”, “TlUDfrzdUi”, “7yIIltvZSI”, “HkAkB7qO3c”, “j4DvSZL3Ry”, “ONmRHuWdGc”, “a7LmXNTfap”, “MlHEWHZhrz”, “ADKHiZeBAS”, “0KQ6aXlsZY”, “OKMfZsps6e”, “vJpOxdfzEd”, “BKs9UTKTQJ”, “DBNrFCM5d7”, “hse8XLelZK”, “3jz3bLreNc”, “ZZSHQ35FpT”, “jOmaC6iMOW”, “iXvhHmq2pn”, “gqnfV11and”, “DDfLkv3uBg”, “UQjsb1ixF0”, “08zSNrhvxs”, “LSWyFeFyYv”, “Regular patrol”, “4Bpd1idsum”, “vCgKs8GFTI”, “uhUIpi1VLd”, “AQn63M3ANF”, “Y39NIKxIgJ”, “YcpNNmT3GX”, “C8Di70GqS7”, “Ybg4P3BOij”, “6Tb4opfNeC”, “aTmzLU9opz”, “QSHg6ZRmyH”, “lVUz2kHAOn”, “Inspection and maintenance”, “wUxaxSm9lC”, “DVTb4JEZQ9”, “sjahqvnVsk”, “TfvPongU7w”, “gQ6g0rJ7ax”, “bejbKLd6ZR”, “SMj8MjDEWY”, “fanVKMvsmZ”, “Regular patrol”, “gmqX2oBxvm”, “Mb9B4SHLvM”, “64rh8hh6Li”, “SjalZaHsOA”, “yxDLOhr4Od”, “qfN1GbZNFA”, “C4q5ridJ7I”, “aQLm7SJeEo”, “LNDWA1UHzG”, “QpYvFfSY6S”, “I7ySXRZkQH”, “WwtkFmM7pP”, “gl794m6lyl”, “6fvgHjprUx”, “SVlNByrKBN”, “DNEmWPvl51”, “Mm6NxxNkdF”, “gyLw8TP8kH”, “vcqjXfWTYz”, “bpuMR5EVFB”, “sMMP5HpxJg”, “9hU32sjepp”, “RsYnv80Hx2”, “SilPvzI3gD”, “L7pspbXgSw”, “RvAtxqkYF2”, “9gOPHDbw31”, “uUDPlKRjU3”, “NkPKW7zkZK”, “dBOiqar26k”, “GXPsNzuULq”, “KwwpvvPqQ2”, “Tif0qfs2eq”, “3xTc242fPW”, “c5jJz3DVrV”, “zrK0VW0AQH”, “zPJt2lGxKU”, “C3ssWKqU5s”, “XsfI0o5asg”, “QiR4pDd4N0”, “q4iY9xGn3V”, “msQISGxfMJ”, “yRyD1ec8fb”, “dBfcrywOUD”, “O9cMzNXF5t”, “lNuV5TSkbO”, “YEgbviK7AV”, “HKrrLQBtt2”, “ZU0hqtXuNa”};

 

 

Instructions:

 

Use an array to simulate new activities being added to the system: “Camera monitoring”, “Communication with control post”

 

  1. Use an array to simulate new activities being added to the system, “Camera monitoring”, “Communication with control post”.
  2. Add these activities to the end of the activities_log.txt file.

 

Solution:

 


import java.io.*;

public class Main {
    public static void main(String[] args) {


String[] newActivities = {"gzCzIGm23u", "cDFm0hkrFw", "IDI0VBjizw", "jOxs8ry2fx", "GuscO6H64v", "DJrhWpPTqr", "zM3MWg5rSj", "DMYzD7vkHF", "BNghDoM55o", "yr89K38y7l", "H2qEcM6elW", "iw6OVfFJDh", "yj16xbky6M", "lFcJ6bQs54", "CNSJhoARCw", "jOxs8ry2fx", "INZj1DoLqA", "GSEGuaChrR", "jrE68aipWX", "h8doJ65vcr", "2EoBD02CKL", "C1av3xn5eG", "R8w7lp6uKQ", "sr1E5Nf6cz", "6C5Eff5czg", "WHVPxvyciZ", "KlO6H5Vduk", "x6NIpTkJBK", "3YvHL9eQQ0", "qYYtIPWjpv", "UZKlgaZ4TD", "msiexEsxj9", "RzoCEriGyZ", "vLuY3gwLJO", "d45CH4k7cs", "rGdyVlst2Q", "3XRVIfo9mc", "eWUlCZ8UOo", "Camera monitoring", "ic7SdX8di8", "C4qAoBxA3D", "Communication with control post", "VBxKrQngfS", "fICz9PFnDX", "dk2ncCGc8x", "QG1MBYgT9i", "HAJkKvCVwT", "gYoMT0TLOu", "vaNsJoXLnd", "TpBnt4Ieav", "TduiTd43zP", "Qm2zhvHZXB", "n7BOCacWYA", "VY4N9FrFPB", "TACDi2dNQY", "VW4PHE8ZDb", "SDYMTOCNJc", "Jnt9v9dgKy", "pY0yYILavE", "G9ERpaXMNl", "qDkqFSu4Sr", "O3VyiIOh6E", "5qB9sTCmKH", "g5MyR0lXeu", "egzksA1lg2", "h6uLmm8tbp", "Fx7ay5mNkD", "neDWml2spW", "IwkPB704pi", "7WqwiNEBrv", "9ed11mNwh7", "kf3qNbMBkS", "MYFOZ3hHC8", "t4I0RK0M8O", "tkvxwGzdjg", "8YsZ8xVw0m", "EdFd9zOWF5", "Te6y3Oa5XP", "9sg2XIQxbQ", "kJhouzOXkS", "128laUqDWt", "nJDSEaneBg", "2rGSBCuzTd", "a5GbJeDTDh", "WVvQGSEkjL", "Q26giaMbeM", "ArdXbDImAh", "Ll3nyJ5PRS", "WjZL4th5V5", "fTtNnPheOS", "73rwDzESwL", "8eXQ7OW9gv", "SMgcsFvILx", "wRKnDVc55v", "UWC1QxcVZc"};

        String outputFile = "activities_log.txt";

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile, true))) {

            for (String activity : newActivities) {
                bw.write(activity);
                bw.newLine();
            }

        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}

 

 

Now we will use again the Program created in exercise three to check if any of the new activities are performed on Mondays.

 

The news is not good. It seems that, at the time we thought the hallway was empty, there are actually two guards performing monitoring tasks and communication with the control post.

 

Our hope of escaping was set on Monday. We haven’t checked the other days, since, according to door activity, there is even more guard movement. We only have one Solution left: we must figure out how to make “Camera monitoring” and “Communication with control post” not happen next Monday.

 

Pedro’s idea is to change the system’s task list and eliminate these two new activities. The problem is that they are already Programmed and can only be modified if the director or deputy director decide to rotate their order. It’s not something they do every week, far from it; in fact, sometimes it takes years for them to do it.

 

However, if the system enters maintenance mode, files can be modified, and we can eliminate the tasks we don’t want to be performed on Monday. A good reason for the system to enter maintenance is to misalign the door activity log; besides, it’s something very easy to accomplish.

 

Remember that this file records minute by minute how many seconds the door was active. What we’ll do is assign one more second of activity to each minute. In minutes where the door is active for 60 seconds, it will show 61 seconds. This will generate an error, as the system will detect that a minute can only have a maximum of 60 seconds of activity.

 

 

Exercise 5. Manipulate Door Activity Times. Write a Program that reads activity times and assigns them from a file and modifies them.Instructions:

 

Write the modified times to a modified_times.txt file.

 

Create a record_times.txt file with registration times in milliseconds (one per line).

 

Read the registration times from the file.

 

Increase each time by a fixed amount (1 second).

 

Solution:

 

import java.io.*;
public class Main {

    public static void main(String[] args) {

        String inputFile = "activity.txt ";
        String outputFile = "modified_times.txt";
        int increment = 1; 

        try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
             BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {

            String line;
            while ((line = br.readLine()) != null) {

                long originalTime = Long.parseLong(line);
                long modifiedTime = originalTime + increment;
                bw.write(Long.toString(modifiedTime));
                bw.newLine();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }
    }
}

 

As expected, the Program quickly detected the error and proceeded to identify and repair it. It simply restarted the system, and the records returned to normal.

 

We achieved our goal: during that maintenance and error-finding period, a time window opened that allowed modification of all files. Pedro took advantage of that moment to change the list of Programmed tasks for Monday to the list we needed, the one that leaves the hallway empty for an hour.

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