diff --git a/_notebooks/2023-04-26-HW-Rohangroup.ipynb b/_notebooks/2023-04-26-HW-Rohangroup.ipynb index 6b6a4b5..c7a17a9 100644 --- a/_notebooks/2023-04-26-HW-Rohangroup.ipynb +++ b/_notebooks/2023-04-26-HW-Rohangroup.ipynb @@ -96,6 +96,281 @@ "}\n", "SeatingChart.main(null);" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hack 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "//create ArrayLists that satisfy the following\n", + "// a) that stores Boolean values\n", + "ArrayList boolList = new ArrayList();\n", + "// b) that stores Turtle Objects\n", + "ArrayList turtleList = new ArrayList();\n", + "// c) that initializes with 10 Strings\n", + "ArrayList stringList = new ArrayList(Arrays.asList(\"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\", \"ten\"));" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hack 2" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ArrayList: [1, 4, 7, 12, 23]\n" + ] + } + ], + "source": [ + "import java.util.ArrayList;\n", + "\n", + "public class Hack2 {\n", + " public static void main(Integer[] args) {\n", + " ArrayList randomNumbers = new ArrayList();\n", + " randomNumbers.add(1);\n", + " randomNumbers.add(4);\n", + " randomNumbers.add(7);\n", + " randomNumbers.add(12);\n", + " randomNumbers.add(23);\n", + " System.out.println(\"ArrayList: \" + randomNumbers);\n", + "\n", + " ;\n", + " ;\n", + " ;\n", + " }\n", + "}\n", + "Hack2.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hack 3" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ArrayList: [1, 4, 7, 12, 23]\n", + "Total: 47\n" + ] + } + ], + "source": [ + "public class Hack3 {\n", + " public static void main(String[] args) {\n", + " ArrayList values = new ArrayList();\n", + " values.add(1);\n", + " values.add(4);\n", + " values.add(7);\n", + " values.add(12);\n", + " values.add(23);\n", + " System.out.println(\"ArrayList: \" + values);\n", + "\n", + " int total = 0;\n", + "\n", + " for (int i=0; i < values.size(); i++) {\n", + " total += values.get(i);\n", + " }\n", + "\n", + " System.out.println(\"Total: \" + total);\n", + " }\n", + "}\n", + "\n", + "Hack3.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hack 4" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11 12 22 25 64 " + ] + } + ], + "source": [ + "public class SelectionSort {\n", + " public static void main(String[] args) {\n", + " int[] arr = {64, 25, 12, 22, 11};\n", + " for (int i = 0; i < arr.length; i++) {\n", + " for (int j = i + 1; j < arr.length; j++) {\n", + " if (arr[j] < arr[i]) {\n", + " // Swap the elements\n", + " int temp = arr[i];\n", + " arr[i] = arr[j];\n", + " arr[j] = temp;\n", + " }\n", + " }\n", + " System.out.print(arr[i] + \" \");\n", + " }\n", + " }\n", + "}\n", + "\n", + "SelectionSort.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hack 5" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Elements with even indexes:\n", + "1 3 5 7 9 \n", + "Elements with odd indexes:\n", + "2 4 6 8 10 \n" + ] + } + ], + "source": [ + "public class ArrayIndexingExample {\n", + " public static void main(String[] args) {\n", + " int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };\n", + "\n", + " System.out.println(\"Elements with even indexes:\");\n", + " for (int i = 0; i < arr.length; i += 2) {\n", + " System.out.print(arr[i] + \" \");\n", + " }\n", + " System.out.println();\n", + "\n", + " System.out.println(\"Elements with odd indexes:\");\n", + " for (int i = 1; i < arr.length; i += 2) {\n", + " System.out.print(arr[i] + \" \");\n", + " }\n", + " System.out.println();\n", + " }\n", + "}\n", + "ArrayIndexingExample.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hack 7" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sum of diagonal elements: 5\n" + ] + } + ], + "source": [ + "public class Test1 {\n", + " public static void main(String[] args) {\n", + " int[][] array = { {1,2,3},{-1,-2,-3},{4,5,6} };\n", + " int sum = 0;\n", + "\n", + " // Iterate over the diagonal of the array\n", + " for (int i = 0; i < array.length; i++) {\n", + " sum += array[i][i];\n", + " }\n", + "\n", + " System.out.println(\"Sum of diagonal elements: \" + sum);\n", + " }\n", + "}\n", + "\n", + "Test1.main(null);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Extra Credit" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "java" + } + }, + "outputs": [], + "source": [ + "// FRQ Array Lists\n" + ] } ], "metadata": {