2.06 Comparing Boolean Expressions
Prove two Boolean expressions are equivalent with truth tables and De Morgan's Laws, and compare objects correctly.
- 1. LxD Cycle Process
- 2. Lesson Plan
- 3. Reference Guide
- 4. Code Examples
- A. Simple: Equivalence in Code
- Code Runner Challenge
- 5. Hacks & Practice Tasks
- 6. Grading Plan (1 Point Total)
- 7. Lesson Revisions & Feedback Evidence
- 8. Summary
- 9. References
1. LxD Cycle Process
Empathize: Students compare Strings with == and see it work, because short identical literals are often the same object. The same code then fails on a String built at run time, and the cause is invisible.
Define:
- POV: CSA students need to distinguish reference equality from value equality, because the failure is intermittent and cannot be diagnosed by reading output alone.
- Learning Goal: Students will prove equivalences with truth tables, rewrite conditions without
!, and compare object references against.equals.
Ideate:
- HMW Question: How might we show students the case where
==on Strings fails, rather than the case where it appears to work? - Activity: A De Morgan proof table, an equivalence check, a references and
equalsactivity, and a remove-the-NOT rewrite.
Prototype & Test: Activity 3 contrasts a String literal with a String built at run time, because comparing only literals is what hides the bug.
2. Lesson Plan
Learning Objective: Compare and contrast equivalent Boolean expressions, and compare object references.
Success Criteria: You can show two conditions are equivalent with a truth table, apply De Morgan’s Laws to remove a !, and explain why == on objects does not compare their contents.
Tech Talk & Introduction (5 minutes)
Two conditions are equivalent when they produce the same result for every input, not just the ones you tried.
- A truth table proves equivalence by listing every combination.
- De Morgan’s Laws:
!(a && b)equals!a || !b, and!(a || b)equals!a && !b. - On objects,
==compares references, meaning whether two variables point at the same object..equalscompares contents. For Strings this distinction causes real bugs.
3. Reference Guide
Key Ideas
- Two Boolean expressions are equivalent when they have the same value for every possible input. A truth table proves it.
- De Morgan’s Laws move a
!inside parentheses by flipping the operator:!(a && b)is equivalent to!a || !b!(a || b)is equivalent to!a && !b
- A
!in front of a comparison flips the comparison:!(x < 5)isx >= 5,!(x == y)isx != y. - Simplifying
!makes conditions easier to read and harder to get wrong.
Comparing Objects
- For primitives (
int,double,boolean,char) use==. - For objects,
==asks “are these the same object?” (two references to one object are called aliases). .equalsasks “do these have the same contents?” Use it forStrings.nullmeans “no object”. Comparing a reference tonullwith==is fine; calling a method onnullthrows aNullPointerException.
| Flip rule | Before | After |
|---|---|---|
| NOT less than | !(x < 5) |
x >= 5 |
| NOT greater or equal | !(x >= 5) |
x < 5 |
| NOT equal | !(x == y) |
x != y |
| De Morgan (AND) | !(a && b) |
!a \|\| !b |
| De Morgan (OR) | !(a \|\| b) |
!a && !b |
4. Code Examples
A. Simple: Equivalence in Code
Run this program. Each pair of lines should print the same value for every input. Change x, raining, and windy and confirm.
Code Runner Challenge
Equivalent expressions and object comparison. Change the values and confirm each pair matches.
View IPYNB Source
// CODE_RUNNER: Equivalent expressions and object comparison. Change the values and confirm each pair matches.
public class Equivalent {
public static void main(String[] args) {
int x = 7;
boolean raining = true;
boolean windy = false;
System.out.println("!(x < 5): " + !(x < 5));
System.out.println("x >= 5: " + (x >= 5));
System.out.println("!(raining && windy): " + !(raining && windy));
System.out.println("!raining || !windy: " + (!raining || !windy));
System.out.println("!(raining || windy): " + !(raining || windy));
System.out.println("!raining && !windy: " + (!raining && !windy));
String first = new String("java");
String second = new String("java");
String alias = first;
System.out.println("first == second: " + (first == second));
System.out.println("first.equals(second): " + first.equals(second));
System.out.println("first == alias: " + (first == alias));
}
}
Equivalent.main(null);
5. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one markdown cell at the top with the frontmatter:
---
layout: post
title: CSA Unit 2.06 Comparing Boolean Expressions HW
categories: [Java, Comparing-Boolean-Expressions]
lesson_language: Java
lesson_topic: Comparing-Boolean-Expressions HW
lesson_source: APCSA
lesson_type: lesson
permalink: /csa/unit_02/2_6_hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Ensure all code runs and output is visible.
- Include a markdown cell before each code section explaining the concept.
Submission Safety Rules (Read First)
[IMPORTANT] To avoid grading errors, follow these rules exactly:
- Write your prediction before you run each code section
- Execute every code cell and leave the output visible
- Show your work for any hand-traced prediction
- Do not just copy code; explain what each line does
- Label each question clearly
Activity 1: Prove De Morgan’s Laws
Fill in the table. If two columns match on every row, the expressions are equivalent.
Activity 2: Same Meaning?
For an int x, sort each expression by what it is equivalent to.
Click an item, then click a bucket. Or drag it. Click an item inside a bucket to send it back.
Activity 3: Objects, References, and equals
What does this print?
What does this print?
Which comparison is correct for an int score and a String name?
Activity 4: Remove the NOT
Rewrite each expression without a !. Spaces do not matter.
Rewrite !(a > b) without the !.
Rewrite !(x == 0 || y == 0) using De Morgan's Law.
Rewrite !(age >= 18 && hasId) using De Morgan's Law.
Popcorn Hack (In-Class)
The condition below works but is hard to read. Rewrite it without any ! so it still prints the same result for every age and member combination. Test with (15, true), (20, false), and (20, true).
Code Runner Challenge
Popcorn hack: remove every ! using De Morgan's Law and the flip rules.
View IPYNB Source
// CODE_RUNNER: Popcorn hack: remove every ! using De Morgan's Law and the flip rules.
public class Simplify {
public static void main(String[] args) {
int age = 15;
boolean member = true;
// Rewrite this condition with no ! at all
if (!(age >= 18 || !member)) {
System.out.println("Youth member rate");
} else {
System.out.println("Standard rate");
}
}
}
Simplify.main(null);
Homework Hack
Two short tasks.
- Finish the runner: the password checker has a bug because it uses
==on Strings. Fix it, then add the second check described in the comment. - Answer the quick check.
Code Runner Challenge
Homework: fix the String comparison and add a length check with no !.
View IPYNB Source
// CODE_RUNNER: Homework: fix the String comparison and add a length check with no !.
public class PasswordCheck {
public static void main(String[] args) {
String saved = new String("open-sesame");
String typed = new String("open-sesame");
// TODO 1: this comparison is wrong for Strings. Fix it.
if (typed == saved) {
System.out.println("Welcome back!");
} else {
System.out.println("Wrong password.");
}
// TODO 2: print "Too short" if typed has fewer than 8 characters,
// otherwise print "Length OK". Do it without using ! anywhere.
}
}
PasswordCheck.main(null);
Are !(a && !b) and !a || b equivalent? Type true or false.
6. Grading Plan (1 Point Total)
Classroom Rubric
-
0.2 points: Popcorn completion Student rewrote the condition with no
!and confirmed identical results for the three listed input pairs. -
0.8 points: Homework completion
- 0.5 String comparison fix: The password check uses
.equalsrather than==and works for a String built at run time. - 0.2 Second check: The additional check described in the runner comment is implemented and tested.
- 0.1 Notebook quality: Every code cell executed with output visible, and a markdown cell before each code section explaining the concept.
- 0.5 String comparison fix: The password check uses
Quick Validation Checklist
- Present:
.equalsused for every String content comparison - Present: truth table covering all input combinations
- Absent:
==comparing String contents - Test: a password assembled by concatenation still matches
7. Lesson Revisions & Feedback Evidence
Feedback Received: Team review of the Unit 2 set found that these lessons did not follow the section format used by the Unit 1 lessons, and that they ended without the assignment submission form every other CSA lesson provides.
Revision Made: The lesson was reorganised into the shared CSA lesson format, so it now opens with the LxD cycle and lesson plan, states its reference material before the code, and groups the activities, Popcorn Hack and Homework Hack under one practice section. The duplicate lesson heading was removed, and the page now renders the standard Submit Assignment form at the end.
Design Decision Kept: Activity 3 contrasts a String literal with a String built at run time, because comparing only literals is what hides the bug.
Open Item: Record peer feedback from the team teach delivery here once the lesson has been taught.
8. Summary
- Equivalent expressions agree on every row of a truth table.
- De Morgan:
!(a && b)is!a || !b, and!(a || b)is!a && !b. !flips comparisons:!(x < 5)isx >= 5.==on objects means “same object”;.equalsmeans “same contents”. Use.equalsfor Strings.- Next lesson: repeating code with
whileloops.
9. References
College Board Course and Exam Description
Topic 2.6, Comparing Boolean Expressions, is required content on the AP Computer Science A Exam. The objective quoted in section 2 of this lesson is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.
Outside Academic Reference
Reference equality is the defined behaviour of == on operands of reference type: it tests whether both operands refer to the same object, which is why two Strings with identical characters can compare as unequal.
Reference List
College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a
Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.21
Submit Assignment
Need to update a submission later? Open the submissions dashboard.