CS 145 Lecture 14 – Logical Operators
Dear students,
Our computer can now ponder our data. It can examine order and equality, two operations at the root of all decision making. Before we sign up for something, we ask ourselves if the benefit exceeds the cost. We compare two brands of pasta on price and weight. We scan the details of our roommate’s face to see if it matches the blurry one we saw on a police bulletin.
These operators are pretty straightforward, so we won’t dwell on them. But let’s visit a few more quick problems before we jump to our next set of operators:
String
"logout"
?
String
ALL CAPs?
Now we introduce the logical operators: &&
, ||
, and !
. You should be able to draw upon a deep store of experience as a rational being as we discuss these, but the level of formality in programming makes them just a wee bit stranger.
String
contain at least one digit?
We will examine these operators through the lens of a 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
a || !b
Here’s your TODO list to complete before we meet again:
- The first midterm is Monday. You may bring a single sheet (front and back) of 8.5 by 11″ paper with notes on it. Possible exam topics include data types, variables, operators, methods,
String
,Random
,Scanner
, andMath
. The problems on the exam will be similar to ones you’ve seen in class and on homework. - We will not have lab next Tuesday. I will be grading.
- Code.org is a non-profit whose mission is to get kids learning about computer science at a young age. On Friday, October 14, they are holding a workshop at a local elementary school, and I will attend. So, no class Friday either!
See you next class!
P.S. It’s Haiku Friday!
I nailed question three
“Is it A, B, C, or D?”
It certainly is
P.P.S. Here’s the code we wrote together in class…
StringBlank.java
package lecture1007; public class StringBlank { public static void main(String[] args) { String s = "asdf"; System.out.println(s.length() == 0); System.out.println(s.isEmpty()); } }
Logout.java
package lecture1007; import java.util.Scanner; public class Logout { public static void main(String[] args) { // Scanner in = new Scanner(System.in); // System.out.print("> "); // String line = in.nextLine(); // // // System.out.println(line == "logout"); // System.out.println(line.equals("logout")); System.out.println(isAllCaps("FOO1")); } public static boolean isAllCaps(String contender) { return contender.toUpperCase().equals(contender); } }
Junctions.java
package lecture1007; public class Junctions { public static void main(String[] args) { // System.out.println(getsBlisters(true, true)); System.out.println(isMrRight(true, false, false, false)); } public static boolean getsBlisters(boolean playedInParsnip, boolean playedInLight) { return playedInParsnip && playedInLight; } public static boolean isMrRight(boolean isTall, boolean isDark, boolean isHandsome, boolean isRich) { return (isTall && isDark && isHandsome) || isRich; } public static boolean canSleepIn(boolean isHoliday, boolean isWeekday) { return isHoliday || !isWeekday; } }