loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5777770834934761","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIC8vIEVhY2ggcmVnaXN0ZXIgaXMgaW5pdGlhbGl6ZWQgdG8gMDAwCiAgICBwcml2YXRlIHN0YXRpYyBTdHJpbmdbXSByZWdpc3RlciA9IHsiMDAwIiwgIjAwMCIsICIwMDAiLCAiMDAwIiwgIjAwMCIsICIwMDAiLCAiMDAwIiwgIjAwMCIsICIwMDAiLCAiMDAwIn07CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewoKICAgICAgICAvLyBSZWR1Y2luZyB0aGUgY29tcGxleGl0eSBvZiBpbnB1dCBieSBhc2tpbmcgdXNlciB0byBpbnB1dCBhIGxvbmcgc3RyaW5nIG9mIGluc3RydWN0aW9ucwogICAgICAgIC8vIEVhY2ggaW5zdHJ1Y3Rpb24gdG8gYmUgc2VwYXJhdGVkIGJ5IGEgc2luZ2xlIHNwYWNlCiAgICAgICAgU2Nhbm5lciBpbiA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludCgiSW5wdXQgYSBsb25nIHN0cmluZyBvZiBpbnN0cnVjdGlvbnMgc2VwYXJhdGVkIGJ5IGEgc2luZ2xlIHNwYWNlOiAiKTsKICAgICAgICBTdHJpbmcgbG9uZ1N0cmluZ09mSW5zdHJ1Y3Rpb25zID0gaW4ubmV4dExpbmUoKTsKCiAgICAgICAgLy8gUHJvY2Vzc2luZyBvZiBMb25nIFN0cmluZyBvZiBJbnN0cnVjdGlvbnMKICAgICAgICAvLyBSZW1vdmluZyBsZWFkaW5nIGFuZCB0cmFpbGluZyB3aGl0ZXNwYWNlCiAgICAgICAgLy8gUmVwbGFjZSAyIG9yIG1vcmUgZW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b3AgcHJvZ3JhbSBleGVjdXRpb24KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIDI6CiAgICAgICAgICAgICAgICAvLyAyZG4gLSBtZWFucyB2YWx1ZSBvZiByZWdpc3RlciBkID0gbgogICAgICAgICAgICAgICAgcmVnaXN0ZXJbaW5zdDJdID0gcG9zdFByb2Nlc3NpbmcoaW5zdDMpOwogICAgICAgICAgICAgICAgZWNob1JlZ2lzdGVycygpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgMzoKICAgICAgICAgICAgICAgIC8vIDNkbiAtIG1lYW5zIHZhbHVlIG9mIHJlZ2lzdGVyIGQgPSB2YWx1ZSBvZiByZWdpc3RlciBkICsgbgogICAgICAgICAgICAgICAgcmVnaXN0ZXJbaW5zdDJdID0gcG9zdFByb2Nlc3NpbmcoSW50ZWdlci5wYXJzZUludChyZWdpc3RlcltpbnN0Ml0pICsgaW5zdDMpOwogICAgICAgICAgICAgICAgZWNob1JlZ2lzdGVycygpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgNDoKICAgICAgICAgICAgICAgIC8vIDRkbiAtIG1lYW5zIHZhbHVlIG9mIHJlZ2lzdGVyIGQgPSB2YWx1ZSBvZiByZWdpc3RlciBkICogbgogICAgICAgICAgICAgICAgcmVnaXN0ZXJbaW5zdDJdID0gcG9zdFByb2Nlc3NpbmcoSW50ZWdlci5wYXJzZUludChyZWdpc3RlcltpbnN0Ml0pICogaW5zdDMpOwogICAgICAgICAgICAgICAgZWNob1JlZ2lzdGVycygpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgNToKICAgICAgICAgICAgICAgIC8vIDVkbiAtIG1lYW5zIHZhbHVlIG9mIHJlZ2lzdGVyIGQgPSB2YWx1ZSBvZiByZWdpc3RlciBkIC8gbgogICAgICAgICAgICAgICAgcmVnaXN0ZXJbaW5zdDJdID0gcG9zdFByb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hbHMsIHdlIHJlbW92ZSBhbnl0aGluZyBhZnRlciBkZWNpbWFsIHBvaW50CiAgICAgICAgICAgICAgICByZWdpc3RlcltpbnN0Ml0gPSBwb3N0UHJvY2Vzc2luZygoaW50KSBNYXRoLnNxcnQoSW50ZWdlci5wYXJzZUludChyZWdpc3RlcltpbnN0Ml0pKSk7CiAgICAgICAgICAgICAgICBlY2hvUmVnaXN0ZXJzKCk7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSA5OgogICAgICAgICAgICAgICAgLy8gOWRzIC0gbWVhbnMgdmFsdWUgb2YgcmVnaXN0ZXIgZCA9ICh2YWx1ZSBvZiByZWdpc3RlciBkKXMKICAgICAgICAgICAgICAgIC8vIEJ5IGRvd24gY2FzdGluZyBhIG51bWJlciB3aXRoIGRlY2ltYWxzLCB3ZSByZW1vdmUgYW55dGhpbmcgYWZ0ZXIgZGVjaW1hbCBwb2ludAogICAgICAgICAgICAgICAgcmVnaXN0ZXJbaW5zdDJdID0gcG9zdFByb2Nlc3NpbmcoKGludCkgTWF0aC5wb3coSW50ZWdlci5wYXJzZUludChyZWdpc3RlcltpbnN0Ml0pLAogICAgICAgICAgICAgICAgICAgIGluc3QzKSk7CiAgICAgICAgICAgICAgICBlY2hvUmVnaXN0ZXJzKCk7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAwOgogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiT3V0cHV0OiBJbnN0cnVjdGlvbiBjb2RlIOKAmDAiICsgaW5zdDIgKyAiIiArIGluc3QzCiAgICAgICAgICAgICAgICAgICAgKyAi4oCZIGlzIG5vdCBzdXBwb3J0ZWQgYnkgdGhlIHN5c3RlbS4iKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyB2b2lkIGVjaG9SZWdpc3RlcnMoKSB7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCByZWdpc3Rlci5sZW5ndGg7IGkrKykgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50KGkgKyAiOiIgKyByZWdpc3RlcltpXSk7CiAgICAgICAgICAgIGlmIChpIDwgcmVnaXN0ZXIubGVuZ3RoIC0gMSkgewogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgiLCAiKTsgLy8gZGVsaW1pdGVyCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgcHJpdmF0ZSBzdGF0aWMgU3RyaW5nIHBvc3RQcm9jZXNzaW5nKGludCBlbnRlcmVkTnVtYmVyKSB7CiAgICAgICAgLy8gYWx3YXlzIHNhdmVkIGluIHRoZSByZWR1Y2VkIGZvcm1hdCB1c2luZyBtb2R1bHVzIG9mIDEwMDAKICAgICAgICBlbnRlcmVkTnVtYmVyID0gZW50ZXJlZE51bWJlciUxMDAwOwogICAgICAgIC8vIGFkZGluZyBsZWFkaW5nIHplcm9lcyB0byBpbnRlZ2VyIHZhbHVlLCByZXR1cm4gaXQgYmFjayBhcyBzdHJpbmcKICAgICAgICByZXR1cm4gU3RyaW5nLmZvcm1hdCgiJTAzZCIsIGVudGVyZWROdW1iZXIpOwogICAgfQoKfQoKCgo="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.Scanner;

public class Main {

    // Each register is initialized to 000
    private static String[] register = {"000", "000", "000", "000", "000", "000", "000", "000", "000", "000"};

    public static void main(String[] args) {

        // Reducing the complexity of input by asking user to input a long string of instructions
        // Each instruction to be separated by a single space
        Scanner in = new Scanner(System.in);
        System.out.print("Input a long string of instructions separated by a single space: ");
        String longStringOfInstructions = in.nextLine();

        // Processing of Long String of Instructions
        // Removing leading and trailing whitespace
        // Replace 2 or more empty spaces with a single space
        longStringOfInstructions = longStringOfInstructions.trim().replaceAll(" +", " ");

        // Split Long String of Instructions by single space
        String[] instructionArray = longStringOfInstructions.split(" ");

        // Check if any of the instruction is not 3 digit long
        for (int i = 0; i < instructionArray.length; i++) {
            if (instructionArray[i].length() != 3) {
                System.out.print("One or more instructions are longer or shorter than 3 digits!");
                // Stop program execution
                System.exit(1);
            }
        }

        // loop through split array of instructions
        for (int i = 0; i < instructionArray.length; i++) {

            // assign instruction to 'instructionCode' string variable
            String instructionCode = instructionArray[i];

            // Print Header - Instruction
            System.out.println("\n" + instructionCode);

            // Get each letter of instruction
            int inst1 = Integer.parseInt(instructionCode.substring(0, 1));
            int inst2 = Integer.parseInt(instructionCode.substring(1, 2));
            int inst3 = Integer.parseInt(instructionCode.substring(2, 3));

            // Execute business logic
            businessLogic(inst1, inst2, inst3);
        }
    }

    private static void businessLogic(int inst1, int inst2, int inst3) {
        // Process Instructions Depending on the first Integer of Instruction Code
        switch (inst1) {
            case 1:
                System.out.println("Program terminated.");
                System.exit(1); // Stop program execution
                break;
            case 2:
                // 2dn - means value of register d = n
                register[inst2] = postProcessing(inst3);
                echoRegisters();
                break;
            case 3:
                // 3dn - means value of register d = value of register d + n
                register[inst2] = postProcessing(Integer.parseInt(register[inst2]) + inst3);
                echoRegisters();
                break;
            case 4:
                // 4dn - means value of register d = value of register d * n
                register[inst2] = postProcessing(Integer.parseInt(register[inst2]) * inst3);
                echoRegisters();
                break;
            case 5:
                // 5dn - means value of register d = value of register d / n
                register[inst2] = postProcessing(Integer.parseInt(register[inst2]) / inst3);
                echoRegisters();
                break;
            case 6:
                // 6ds - means value of register d = value of register sˢ
                register[inst2] = postProcessing(Integer.parseInt(register[inst3]));
                echoRegisters();
                break;
            case 7:
                // 7ds - means value of register d = value of register d + value of register s
                register[inst2] = postProcessing(Integer.parseInt(register[inst2])
                    + Integer.parseInt(register[inst3]));
                echoRegisters();
                break;
            case 8:
                // 8dx - means value of register d = sqrt (value of register d). You can ignore the value of x
                // By down casting a number with decimals, we remove anything after decimal point
                register[inst2] = postProcessing((int) Math.sqrt(Integer.parseInt(register[inst2])));
                echoRegisters();
                break;
            case 9:
                // 9ds - means value of register d = (value of register d)s
                // By down casting a number with decimals, we remove anything after decimal point
                register[inst2] = postProcessing((int) Math.pow(Integer.parseInt(register[inst2]),
                    inst3));
                echoRegisters();
                break;
            case 0:
                System.out.print("Output: Instruction code ‘0" + inst2 + "" + inst3
                    + "’ is not supported by the system.");
                break;
        }
    }

    private static void echoRegisters() {
        for (int i = 0; i < register.length; i++) {
            System.out.print(i + ":" + register[i]);
            if (i < register.length - 1) {
                System.out.print(", "); // delimiter
            }
        }
    }

    private static String postProcessing(int enteredNumber) {
        // always saved in the reduced format using modulus of 1000
        enteredNumber = enteredNumber%1000;
        // adding leading zeroes to integer value, return it back as string
        return String.format("%03d", enteredNumber);
    }

}