Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 88 additions & 18 deletions _projects/lessons/java/notebooks/2025-09-21-2.2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"lesson_part: interactive\n",
"lesson_type: lesson\n",
"toc: true\n",
"codemirror: true\n",
"title: Boolean Expressions\n",
"menu: nav/csa_units/csaunit2.html\n",
"permalink: /csa/unit_02/2_2\n",
Expand All @@ -38,7 +39,9 @@
"| `<` | less than |\n",
"| `>` | greater than |\n",
"| `<=` | less than or equal to |\n",
"| `>=` | greater than or equal to |"
"| `>=` | greater than or equal to |\n",
"\n",
"Run it yourself below."
]
},
{
Expand All @@ -51,12 +54,18 @@
},
"outputs": [],
"source": [
"int score = 85;\n",
"// CODE_RUNNER: Boolean Expressions - Try It\n",
"public class Main {\n",
" public static void main(String[] args) {\n",
" int score = 85;\n",
"\n",
"System.out.println(score == 85); // true\n",
"System.out.println(score != 100); // true\n",
"System.out.println(score > 90); // false\n",
"System.out.println(score <= 85); // true"
" System.out.println(score == 85); // true\n",
" System.out.println(score != 100); // true\n",
" System.out.println(score > 90); // false\n",
" System.out.println(score <= 85); // true\n",
" }\n",
"}\n",
"Main.main(null);"
]
},
{
Expand Down Expand Up @@ -112,18 +121,24 @@
},
"outputs": [],
"source": [
"int temp = 72;\n",
"System.out.println(temp >= 70);\n",
"System.out.println(temp == 72.0);\n",
"System.out.println(temp < 32);"
"// CODE_RUNNER: Popcorn Hack 1 - Predict the Output\n",
"public class Main {\n",
" public static void main(String[] args) {\n",
" int temp = 72;\n",
" System.out.println(temp >= 70);\n",
" System.out.println(temp == 72.0);\n",
" System.out.println(temp < 32);\n",
" }\n",
"}\n",
"Main.main(null);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Popcorn Hack 2\n",
"Find and fix the 2 errors in this Boolean expression."
"Find and fix the 2 errors in this Boolean expression, then run it until it works."
]
},
{
Expand All @@ -136,9 +151,66 @@
},
"outputs": [],
"source": [
"int number = 4;\n",
"boolean isEven = number % 2 = 0;\n",
"System.out.println(isEven)"
"// CODE_RUNNER: Popcorn Hack 2 - Fix the Bug\n",
"public class Main {\n",
" public static void main(String[] args) {\n",
" int number = 4;\n",
" boolean isEven = number % 2 = 0;\n",
" System.out.println(isEven)\n",
" }\n",
"}\n",
"Main.main(null);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Show fixed code</summary>\n",
"<pre><code class=\"language-java\">int number = 4;\n",
"boolean isEven = number % 2 == 0;\n",
"System.out.println(isEven);</code></pre>\n",
"</details>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Try It Yourself\n",
"Change `false` below to a Boolean expression using `age` and a relational operator, so `canJoinClub` is only `true` when `age` is 13 or older. Run it, then edit `age` to test 12, 13, and 100."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"// CODE_RUNNER: Try It Yourself - Age Check\n",
"public class Main {\n",
" public static void main(String[] args) {\n",
" int age = 16;\n",
"\n",
" boolean canJoinClub = false; // TODO: use age and a relational operator\n",
"\n",
" System.out.println(canJoinClub);\n",
" }\n",
"}\n",
"Main.main(null);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Show one possible answer</summary>\n",
"<pre><code class=\"language-java\">boolean canJoinClub = age >= 13;</code></pre>\n",
"</details>"
]
},
{
Expand All @@ -154,10 +226,8 @@
"<details><summary>Show answer</summary>B \u2014 <code>!=</code> is false when both sides are equal.</details>\n",
"\n",
"**2.**\n",
"```java\n",
"int x = 5, y = 10;\n",
"System.out.println(x <= y);\n",
"```\n",
"<pre><code class=\"language-java\">int x = 5, y = 10;\n",
"System.out.println(x <= y);</code></pre>\n",
"What is printed?\n",
"A) `5` B) `10` C) `true` D) `false`\n",
"\n",
Expand Down
Loading
Loading