modal-121 / 10 - Methods in Java /006 Draw empty rectangle.html
Tan115's picture
Add files using upload-large-folder tool
2a75a66 verified
Raw
History Blame
8.25 kB
<!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": "006 Draw empty rectangle", "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 input height of the rectangle</p></li><li><p>After user inputs heights of the rectangle, program asks to input width of the rectangle.</p></li><li><p>Program calls specific method which takes two parameters of int type which prints rectangle to the console:<br> ******</p></li></ol><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; *&nbsp; &nbsp; &nbsp; &nbsp; *</p><p>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;******</p></li></ol><p>In the example above height of the rectangle is 3, the width of the rectangle is 6</p><p><strong><em>N.B.: rectangle is empty inside</em></strong></p>", "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.io.ByteArrayInputStream;\nimport java.io.ByteArrayOutputStream;\nimport java.io.InputStream;\nimport java.io.PrintStream;\n\nimport org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\n\n\npublic class Evaluate {\nprivate static final String OUTPUT_RESULT_SEPARATOR = \"Please, enter width of rectangle: \";\n\tprivate final ByteArrayOutputStream out = new ByteArrayOutputStream();\n\tprivate final PrintStream originalOut = System.out;\n\t\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\n\t\n\t@Test\n\tpublic void shouldDrawRectangleEmptyInside() {\n\t\tString input = \"3 6\";\n\t\tInputStream in = new ByteArrayInputStream(input.getBytes());\n\t\tSystem.setIn(in);\n\n\t\tEmptyRectangle.main(new String[] {});\n\t\t\n\t\tString consoleOutput = out.toString().split(OUTPUT_RESULT_SEPARATOR)[1].strip();\n\t\tassertEquals(\"******\" + System.lineSeparator() +\n\t\t\t\t\"* *\" + System.lineSeparator() +\n\t\t\t\t\"******\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldDrawSquareEmptyInside() {\n\t\tString input = \"4 4\";\n\t\tInputStream in = new ByteArrayInputStream(input.getBytes());\n\t\tSystem.setIn(in);\n\n\t\tEmptyRectangle.main(new String[] {});\n\t\t\n\t\tString consoleOutput = out.toString().split(OUTPUT_RESULT_SEPARATOR)[1].strip();\n\t\tassertEquals(\"****\" + System.lineSeparator() +\n\t\t\t\t\"* *\" + System.lineSeparator() +\n\t\t\t\t\"* *\" + System.lineSeparator() +\n\t\t\t\t\"****\", consoleOutput);\n\t}\n\t\n\t@Test\n\tpublic void shouldDrawRectangleNotEmptyInside() {\n\t\tString input = \"2 6\";\n\t\tInputStream in = new ByteArrayInputStream(input.getBytes());\n\t\tSystem.setIn(in);\n\n\t\tEmptyRectangle.main(new String[] {});\n\t\t\n\t\tString consoleOutput = out.toString().split(OUTPUT_RESULT_SEPARATOR)[1].strip();\n\t\tassertEquals(\"******\" + System.lineSeparator() +\n\t\t\t\t\"******\", consoleOutput);\n\t}\n}\n"}], "solutions": [{"file_name": "EmptyRectangle.java", "content": "import java.util.Scanner;\r\n\r\npublic class EmptyRectangle {\r\n\t\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 height of rectangle: \");\r\n\t\tint height = sc.nextInt();\r\n\t\tSystem.out.print(\"Please, enter width of rectangle: \");\r\n\t\tint width = sc.nextInt();\r\n\t\t\r\n\t\tdrawRectangle(height, width);\r\n\t}\r\n\r\n\tpublic static void drawRectangle(int height, int width) {\r\n\t\tfor (int i = 0; i < height; i++) {\r\n\t\t\tfor (int j = 0; j < width; j++) {\r\n\t\t\t\tif (j == 0 || j == width - 1 || i == 0 || i == height -1) {\r\n\t\t\t\t\tSystem.out.print(\"*\");\r\n\t\t\t\t} else {\r\n\t\t\t\t\tSystem.out.print(\" \");\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\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>