| <!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": "008 Filter String array", "hasInstructions": true, "hasTests": true, "hasSolutions": true, "instructions": "<ol><li><p>Implement console program which will meet the following requirements:</p><ol><li><p>Program starts and asks user to enter random words separated by space</p></li><li><p>Program asks user to enter minimum length of string to filter words which were entered</p></li><li><p>Program creates array object from entered words</p></li><li><p>Program calls specific method which takes String[] as a parameter and returns array of strings which contains words that have length more or equal to value specified by user<br><br><strong><em>Method should look like this:<br>public static String[] filterWordsByLength(int minLength, String[] words) {</em></strong></p></li></ol><p><strong><em> <write your code here><br> }</em></strong><br><br><br></p><p>c. Program prints filtered array to the console output.</p></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;\n\nimport java.util.Arrays;\n\nimport org.junit.Test;\n\npublic class Evaluate {\n @Test\n\tpublic void shouldFilterWordsByMinLength() {\n\t\tString[] input = { \"asd\", \"asdf\", \"as\", \"asdfg\", \"a\" };\n\t\tint minLength = 3;\n\n\t\tassertEquals(\"[asd, asdf, asdfg]\",\n\t\t\t\tArrays.toString(FilterStringArray.filterWordsByLength(minLength, input)));\n\t}\n\n\t@Test\n\tpublic void shouldFilterWordsByMinLength2() {\n\t\tString[] input = { \"asd\", \"asdf\", \"as\", \"asdfg\", \"a\" };\n\t\tint minLength = 2;\n\n\t\tassertEquals(\"[asd, asdf, as, asdfg]\",\n\t\t\t\tArrays.toString(FilterStringArray.filterWordsByLength(minLength, input)));\n\t}\n}\n"}], "solutions": [{"file_name": "FilterStringArray.java", "content": "import java.util.Arrays;\r\nimport java.util.Scanner;\r\n\r\npublic class FilterStringArray {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tSystem.out.print(\"Please, enter any words separated by space: \");\r\n\t\tString userInput = sc.nextLine();\r\n\t\tSystem.out.print(\"Please, enter minumum word length to filter words: \");\r\n\t\tint minLength = sc.nextInt();\r\n\t\t\r\n\t\tString[] words = userInput.split(\"\\\\s+\");\r\n\t\tString[] filteredWords = filterWordsByLength(minLength, words);\r\n\t\tSystem.out.println(Arrays.toString(filteredWords));\r\n\t}\r\n\r\n\t\r\n\t\r\n\tpublic static String[] filterWordsByLength(int minLength, String[] words) {\r\n\t\tString[] filteredArray = new String[words.length];\r\n\t\tfor (int i = 0; i < words.length; i++) {\r\n\t\t\tif (words[i].length() >= minLength) {\r\n\t\t\t\tfilteredArray[i] = words[i];\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tfilteredArray = filterNulls(filteredArray);\r\n\t\t\r\n\t\treturn filteredArray;\r\n\t}\r\n\r\n\r\n\r\n\tprivate static String[] filterNulls(String[] arr) {\r\n\t\tint newArraySize = 0;\r\n\t\tfor (String word : arr) {\r\n\t\t\tif (word != null) {\r\n\t\t\t\tnewArraySize++;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tString[] filteredArray = new String[newArraySize];\r\n\t\t\r\n\t\tint filteredArrayIndex = 0;\r\n\t\tfor (String word : arr) {\r\n\t\t\tif (word != null) {\r\n\t\t\t\tfilteredArray[filteredArrayIndex++] = word;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\treturn filteredArray;\r\n\t}\r\n\r\n\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() { |
| |
| 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>"; |
| } |
| |
| |
| 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"); |
| } |
| |
| |
| 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> |
|
|