Skip to content

Commit

Permalink
hacks update
Browse files Browse the repository at this point in the history
  • Loading branch information
Trystan-Schmits committed Sep 24, 2024
1 parent 7cb22d8 commit 5e338d7
Showing 1 changed file with 111 additions and 4 deletions.
115 changes: 111 additions & 4 deletions _notebooks/Foundation/2023-09-21-hacks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,124 @@
{
"cell_type": "markdown",
"metadata": {},
"source": []
"source": [
"## Multiple Choice\n",
"### 1. What is wrong with this block of code?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"class Shape{\n",
" double length = 0;\n",
" double width = 0;\n",
" \n",
" public Shape(double length, double width){\n",
" this.length = length;\n",
" this.width = width;\n",
" }\n",
"\n",
" public double getArea(){\n",
" return this.length * this.width;\n",
" }\n",
"\n",
" private String toString(){\n",
" return \"Shape length:\"+ (new Double(this.length)).toString() + \" width:\" + (new Double(this.width)).toString();\n",
" }\n",
"}\n",
"\n",
"Shape myShape = new Shape(2,3);\n",
"\n",
"System.out.println(myShape.getArea());"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a) You can't use the **this** keyword in the constructor\n",
"\n",
"b) When passing a **double** through an argument it must be in the form of 0.0\n",
"\n",
"c) The **toString()** method must be public\n",
"\n",
"d) The **getArea()** method doesn't return a double\n",
"\n",
"### 2. Which method cannot be exectuted in the following example of Polymorphism"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"class Water{\n",
" public String toString(){\n",
" return \"Water\";\n",
" }\n",
"\n",
" private boolean isSalty(){\n",
" return false;\n",
" }\n",
"\n",
" public String typeOfWater(){\n",
" return \"Static\";\n",
" }\n",
"\n",
"}\n",
"\n",
"class Lake extends Water{\n",
" public String toString(){\n",
" return \"Lake\";\n",
" }\n",
"\n",
" public boolean isSalty(){\n",
" return true;\n",
" }\n",
"}\n",
"\n",
"Water myLakeWater = new Lake();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a) **typeOfWater()**\n",
"\n",
"b) **isSalty()**\n",
"\n",
"c) **toString()**\n",
"\n",
"d) **getClass()**"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "java (IJava/j!)",
"display_name": "Java",
"language": "java",
"name": "jbang-ijava"
"name": "java"
},
"language_info": {
"name": "java"
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "17.0.12+7-Ubuntu-1ubuntu222.04"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 5e338d7

Please sign in to comment.