File size: 8,070 Bytes
33852e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Coding Assignment</title>

        <style>
            body {
                font-family: sf pro text, -apple-system, BlinkMacSystemFont, Roboto, segoe ui, Helvetica, Arial,
                    sans-serif, apple color emoji, segoe ui emoji, segoe ui symbol;
                font-weight: 400;
                line-height: 22.4px;
                font-size: 16px;
            }
            p,
            ul,
            ol {
                font-size: 16px;
                font-weight: 400;
            }
            h1,
            h2,
            h3,
            h4,
            h5,
            h6 {
                font-weight: bold;
            }
            ul {
                list-style: none;
                margin: 0;
                padding: 0;
                max-width: none;
            }
            .code-snippet {
                background-color: #fff;
                border: 1px solid #d1d7dc;
                color: #b4690e;
                font-size: 90%;
                padding: 0.2rem 0.4rem;
            }
            .code-block {
                background-color: #fff;
                color: #b4690e;
                font-size: 90%;
            }
            .black-block {
                color: #000000;
            }
            .italic-text {
                font-style: italic;
            }
        </style>
    </head>

    <body onload="main()">
        <h1 id="coding-title"></h1>
        <div>
            <h2>Instructions</h2>
            <div id="coding-instructions"></div>
        </div>
        <div>
            <h2>Test(s)</h2>
            <div id="coding-tests"></div>
        </div>
        <div>
            <h2>Solution(s)</h2>
            <div id="coding-solutions"></div>
        </div>

        <script>
            const quizData = {"title": "003 Simple Number Calculator", "hasInstructions": true, "hasTests": true, "hasSolutions": true, "instructions": "<ol><li><p>Implement console program which will meet the following requirements:</p><ol><li><p>Program is started with two arguments (numbers)</p></li><li><p>Program adds these two numbers</p></li><li><p>In case one of the input arguments is floating-point number - the result of addition is floating-point number</p></li><li><p>In case two arguments are integer - the result of addition is integer</p></li></ol></li></ol>", "tests": [{"file_name": "Evaluate.java", "content": "import org.junit.Test;\nimport org.junit.Assert;\nimport com.udemy.ucp.*;\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertFalse;\nimport static org.junit.Assert.assertTrue;\n\nimport java.io.ByteArrayOutputStream;\nimport java.io.PrintStream;\n\nimport org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\n\npublic class Evaluate {\n   private static final String FLOATING_POINT_NUMBER_PLACES_SEPARATOR = \".\";\n\tprivate final ByteArrayOutputStream out = new ByteArrayOutputStream();\n\tprivate final PrintStream originalOut = System.out;\n\n\t@Before\n\tpublic void setStreams() {\n\t\tSystem.setOut(new PrintStream(out));\n\t}\n\n\t@After\n\tpublic void restoreInitialStreams() {\n\t\tSystem.setOut(originalOut);\n\t}\n\t\n\t@Test\n\tpublic void shouldAddPositiveAndNegativeInteger() {\n\t\tAddNumbers.main(new String[]{\"1\", \"-2\"});\n\t\tString consoleOutput = out.toString().trim();\n\t\tassertFalse(consoleOutput.contains(FLOATING_POINT_NUMBER_PLACES_SEPARATOR));\n\t\tassertEquals(\"-1\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldAddPositiveAndPositiveInteger() {\n\t\tAddNumbers.main(new String[]{\"1\", \"3\"});\n\t\tString consoleOutput = out.toString().trim();\n\t\tassertFalse(consoleOutput.contains(FLOATING_POINT_NUMBER_PLACES_SEPARATOR));\n\t\tassertEquals(\"4\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldAddZeroToZero() {\n\t\tAddNumbers.main(new String[]{\"0\", \"0\"});\n\t\tString consoleOutput = out.toString().trim();\n\t\tassertFalse(consoleOutput.contains(FLOATING_POINT_NUMBER_PLACES_SEPARATOR));\n\t\tassertEquals(\"0\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldAddFloatingPointNumberToInteger() {\n\t\tAddNumbers.main(new String[]{\"1.1\", \"1\"});\n\t\tString consoleOutput = out.toString().trim();\n\t\tassertTrue(consoleOutput.contains(FLOATING_POINT_NUMBER_PLACES_SEPARATOR));\n\t\tassertEquals(\"2.1\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldAddFloatingPointNumberToInteger2() {\n\t\tAddNumbers.main(new String[]{\"1\", \"2.1\"});\n\t\tString consoleOutput = out.toString().trim();\n\t\tassertTrue(consoleOutput.contains(FLOATING_POINT_NUMBER_PLACES_SEPARATOR));\n\t\tassertEquals(\"3.1\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldAddFloatingPointNumbers() {\n\t\tAddNumbers.main(new String[]{\"1.1\", \"2.1\"});\n\t\tString consoleOutput = out.toString().trim();\n\t\tassertTrue(consoleOutput.contains(FLOATING_POINT_NUMBER_PLACES_SEPARATOR));\n\t\tassertEquals(\"3.2\", consoleOutput);\n\t}\n\n}\n"}], "solutions": [{"file_name": "AddNumbers.java", "content": "public class AddNumbers {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tif (args[0].contains(\".\") || args[1].contains(\".\")) {\r\n\t\t\tdouble paramDouble1 = Double.parseDouble(args[0]);\r\n\t\t\tdouble paramDouble2 = Double.parseDouble(args[1]);\r\n\t\t\tSystem.out.println(paramDouble1 + paramDouble2);\r\n\t\t} else {\r\n\t\t\tint paramInt1 = Integer.parseInt(args[0]);\r\n\t\t\tint paramInt2 = Integer.parseInt(args[1]);\r\n\t\t\tSystem.out.println(paramInt1 + paramInt2);\r\n\t\t}\r\n\t}\r\n\r\n}"}]};

            function renderCodeList(rootElement, codeList, className, titlePrefix) {
                for (var i = 0; i < codeList.length; i++) {
                    var elem = codeList[i];
                    var jsElem = document.createElement("div");
                    jsElem.className = className;
                    var jsElemTitle = document.createElement("h3");
                    jsElemTitle.innerHTML = titlePrefix + " " + (i + 1);
                    var jsElemBody = document.createElement("code");
                    jsElemBody.className = "code-block black-block";
                    jsElemBody.innerHTML = "<pre>" + elem.content + "</pre>";
                    jsElem.appendChild(jsElemTitle);
                    jsElem.appendChild(jsElemBody);
                    rootElement.appendChild(jsElem);
                }
            }

            function main() {
                // display the assignment
                var codingTitle = document.getElementById("coding-title");
                codingTitle.innerHTML = quizData.title;

                var codingInstructions = document.getElementById("coding-instructions");
                if (quizData.hasInstructions) {
                    codingInstructions.innerHTML = quizData.instructions;
                } else {
                    codingInstructions.innerHTML = '<span class="italic-text">' + quizData.instructions + "</span>";
                }

                // display the test(s)
                var codingTests = document.getElementById("coding-tests");
                if (!quizData.hasTests) {
                    codingTests.innerHTML = '<span class="italic-text">' + quizData.tests + "</span>";
                } else {
                    renderCodeList(codingTests, quizData.tests, "coding-test", "Test");
                }

                // display the solution(s)
                var codingSolutions = document.getElementById("coding-solutions");
                if (!quizData.hasSolutions) {
                    codingSolutions.innerHTML = '<span class="italic-text">' + quizData.solutions + "</span>";
                } else {
                    renderCodeList(codingSolutions, quizData.solutions, "coding-solution", "Solution");
                }
            }
        </script>
    </body>
</html>