Skip to content
Merged
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
176 changes: 172 additions & 4 deletions _projects/lessons/java/notebooks/2025-09-21-2.2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"cells": [
{
"cell_type": "markdown",
"id": "064892a5",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"layout: post\n",
Expand All @@ -19,13 +22,178 @@
"permalink: /csa/unit_02/2_2\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2.2 Boolean Expressions\n",
"\n",
"A **Boolean expression** evaluates to `true` or `false` \u2014 nothing else. The College Board puts it simply: \"An expression involving relational operators evaluates to a Boolean value\" (College Board, 2025, p. 61).\n",
"\n",
"| Operator | Meaning |\n",
"|---|---|\n",
"| `==` | equal to |\n",
"| `!=` | not equal to |\n",
"| `<` | less than |\n",
"| `>` | greater than |\n",
"| `<=` | less than or equal to |\n",
"| `>=` | greater than or equal to |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Raise your hand:\n",
"1. If `score` was 90, what would `score > 90` print?\n",
"2. Is `score != 85` `true` or `false` right now?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Where else do you see this?\n",
"Every API you build \u2014 Spring on the CSA side, any backend on the CSP side \u2014 has to decide what to send back before it responds: *Does this exist? Is this value allowed?* Each check is a Boolean expression built from the exact operators above, and the `true`/`false` result decides the response."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"if (item == null) {\n",
" return notFound(); // equality operator: does it exist?\n",
"}\n",
"if (quantity > maxAllowed) {\n",
" return badRequest(); // relational operator: is it valid?\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Popcorn Hack 1\n",
"Predict what each line prints, then run it to check."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"int temp = 72;\n",
"System.out.println(temp >= 70);\n",
"System.out.println(temp == 72.0);\n",
"System.out.println(temp < 32);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Popcorn Hack 2\n",
"Find and fix the 2 errors in this Boolean expression."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"int number = 4;\n",
"boolean isEven = number % 2 = 0;\n",
"System.out.println(isEven)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## AP Exam Practice\n",
"Try these on your own before checking the answer.\n",
"\n",
"**1.** What does `12 != 12` evaluate to?\n",
"A) `true` B) `false` C) `0` D) Compile-time error\n",
"\n",
"<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",
"What is printed?\n",
"A) `5` B) `10` C) `true` D) `false`\n",
"\n",
"<details><summary>Show answer</summary>C \u2014 5 is less than 10, so the relational expression evaluates to <code>true</code>.</details>\n",
"\n",
"**3.** Which correctly tests two `int` values, `a` and `b`, for equality?\n",
"A) `a = b` B) `a == b` C) `a.equals(b)` D) `a != b`\n",
"\n",
"<details><summary>Show answer</summary>B \u2014 <code>==</code> compares primitive values directly; <code>a.equals(b)</code> is for objects, not primitives.</details>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"- College Board. (2025). *AP Computer Science A course and exam description*. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description.pdf\n",
"- Oracle. (n.d.). *Equality, relational, and conditional operators*. The Java Tutorials. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Java",
"language": "java",
"name": "java"
},
"language_info": {
"name": "python"
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "17.0.12+0"
}
},
"nbformat": 4,
"nbformat_minor": 5
"nbformat_minor": 2
}
Loading