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:
- Sleep in?Can one sleep in today?
- Has digit?Does a password contain at least one digit?
- No attachment?Is the word “attachment” absent from the body of an email?
- Even pizza?You are having a party for some friends who struggle with jealousy. One friend may or may not be able to make it, which makes planning difficult but not impossible. Can you divide the slices of pizza evenly amongst all guests, so as to arouse no one’s jealousy?
- Answer phone?You normally don’t answer your phone in the mornings—unless it’s a parent calling you. But you never answer if you’re asleep. The phone is ringing now. Do you answer?
- Is presidential?Are you eligible to be president of the United States?
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;
}
}