CS 1: Lecture 15 – Truth Tables
Dear students,
Today we keep asking questions about data. Let’s start with some blackboxes!
Let me be frank with you. Expressing a program’s logic can get really confusing, and this is probably the spot where we make the most mistakes when writing code. There are various tools to help us think about our logic, and we will examine a very common one now: the truth table. Truth tables enumerate all possible combinations of the inputs and show the resulting outputs. For instance, here is AND’s truth table:
a
|
b
|
a && b
|
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
And OR’s:
a
|
b
|
a || b
|
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
And NOT’s:
a
|
!a
|
---|---|
0 | 1 |
1 | 0 |
What are the truth tables for these expressions?
!(a && b)
!!a
a || !b
Now let’s do some exercises:
Here’s your TODO list to complete before we meet again:
- CS 148, we will not have lab Thursday.
- Homework 3 has been posted. You’ll need to Team / Pull… from the template repository as described in homework 0, part 2. Start early. Get all your Bitbucket issues worked out now. I will not be around the night before it is due.
- Read sections 5.3 through 5.4 in your book. On a quarter sheet, express 3 complex truths of life using the logical operators
&&
,||
, and!
. For example, here’s one:boolean isEdible = isCooked || !isMeat;
See you next class!
Sincerely,

P.S. It’s time for a haiku!
I nailed question three
“Is it A, B, C, or D?”
It certainly is
P.P.S. Here’s the code we wrote together…
Exam.java
package lecture1011;
import java.awt.Color;
import java.util.Random;
public class Exam {
public static Color getRandomColor() {
Random generator = new Random();
int r = generator.nextInt(256);
int g = generator.nextInt(256);
int b = generator.nextInt(256);
Color rgb = new Color(r, g, b);
return rgb;
}
}
Logic.java
package lecture1011;
public class Logic {
public static boolean canSleepIn(boolean isScheduled, boolean isEnjoyable) {
return !(isScheduled && isEnjoyable);
// return !isScheduled || !isEnjoyable;
}
public static boolean canSleepIn2(boolean isWeekday,
boolean isHoliday) {
return !isWeekday || isHoliday;
}
public static boolean isPresidential(int age,
boolean isNBC,
boolean isFelon,
int nYearsResident,
int nTermsServed) {
return age >= 35 &&
isNBC &&
!isFelon &&
nYearsResident >= 14 &&
nTermsServed < 2;
}
}
Blackboxes.java
package lecture1011;
public class Blackboxes {
public static void main(String[] args) {
System.out.println(isNeighbors(-1, 0));
}
public static boolean canFit(String s, int max) {
return s.length() <= max;
}
public static boolean isNeighbors(int a, int b) {
return Math.abs(a - b) == 1;
// return a - b == 1 || b - a == 1;
}
public static boolean isCapital(char c) {
return 'A' <= c && c <= 'Z';
// return Character.isUpperCase(c);
}
}
Blackboxes.java
package lecture1011;
public class Blackboxes {
public static boolean isShorterThan(String s, int n) {
return s.length() <= n;
}
public static boolean isNeighbors(int a, int b) {
return Math.abs(a - b) == 1;
// return (a - b) * (a - b) == 1;
// return a - b == 1 || a - b == -1;
// return a + 1 == b || a - 1 == b;
}
public static boolean isCapital(char c) {
// return !(c + "").toLowerCase().equals(c + "");
// return 'A' <= c && c <= 'Z';
return Character.isUpperCase(c);
}
public static void main(String[] args) {
System.out.println(isCapital('\\'));
}
}
Logic.java
package lecture1011;
public class Logic {
public static boolean canSleepIn(boolean isWeekday,
boolean isHoliday) {
return isHoliday || !isWeekday;
}
public static boolean isPresidential(int age,
boolean isNaturalBornCitizen,
int nYearsResident,
int nTermsServed) {
return age >= 35 &&
isNaturalBornCitizen &&
nYearsResident >= 14 &&
nTermsServed < 2;
}
}