CS1: Lecture 15 – Logical Operators
Dear students,
We’ve seen the arithmetic operators. We’ve seen the relational and equality operators. Today we meet the logical operators &&
, ||
, and !
. These operators have boolean
operands and yield boolean
values; they are designed for gluing together simple predicates into compound ones. Before we look at them, let’s complete a little warmup exercise.
Write a methodisLogout
that returnstrue
if and only if theString
given as a parameter is"logout"
.
Problems
As we did with the relational operators, let’s examine the logical operators in the context of a bunch of predicate methods.
- In range?Is a number in a given range?
- Is gray?Is a
String
gray in American/British English? - Is vowel?Is a letter a vowel?
- Parsnip blisters?Will a person get blisters from wild parsnip?
- Is vowel?Is a letter a vowel?
- Is food servable?Can you bring this dish to a potluck where you will be feeding strangers with that may have allergies or intolerances?
- Sleep in?Can one sleep in today?
- 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?
- Mathch?You turn over three playing cards. Do two of them add up to the third?
- Answer?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?
TODO
Here’s your TODO list to complete before our next lecture:
- Homework 2 is due today. Be sure you are running the SpecChecker on your local machine. If you don’t see “High five!”, then your code doesn’t work. If you don’t properly commit and push, then your code doesn’t work. I cannot accommodate your last-minute questions.
- Our first exam is next Monday.
- CS 145, lab 5 is posted. Feel free to start early.
See you next class!
Sincerely,
P.S. It’s time for a haiku! Hit the ->
button to animate it.
P.P.S. Here’s the code we wrote together in class…
Certainly.java
package lecture1007.cs145;
public class Certainly {
public static void samesies(int expected, int actual) {
System.out.printf("%d == %d%n", expected, actual);
}
public static void samesies(boolean expected, boolean actual) {
System.out.printf("%b == %b%n", expected, actual);
}
}
Booleanning.java
package lecture1007.cs145;
import java.util.Scanner;
public class Booleanning {
public static boolean isInRange(int lo, int hi, int number) {
// return lo <= number <= hi;
return (number >= lo) && (number <= hi);
}
public static boolean isGray(String token) {
return token.equals("gray") || token.equals("grey") || token.equals("gris");
}
public static boolean willGetBlisters(boolean hasSunlight,
boolean isExposedToParsnip) {
return hasSunlight && isExposedToParsnip;
}
public static boolean isPresidentialMaterial(int age,
boolean isNaturalBornCitizen,
int nYearsResidency,
int nTerms) {
return age >= 35 &&
isNaturalBornCitizen &&
nYearsResidency >= 14 &&
nTerms < 2;
}
public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
// System.out.print("> ");
// String color = in.next();
// System.out.println(isGray(color));
System.out.println(willGetBlisters(true, true));
}
}
Booleanning.java
package lecture1007.cs148;
import java.util.Scanner;
public class Booleanning {
public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
// System.out.print("> ");
// String token = in.next();
Certainly.samesies(true, isInRange(1, 37, 16), "");
Certainly.samesies(false, isInRange(1, 37, 42), "");
// Certainly.samesies(true, isGray(token), "");
// Certainly.samesies(true, isGray("gray"), "");
String text = "gray night";
// Certainly.samesies(true, isGray(text.substring(0, 4)), "");
Certainly.samesies(true, isGray("gr" + "ay"), "");
Certainly.samesies(false, isGray("zebra"), "");
}
public static boolean isBlistery(boolean isLit,
boolean isExposedToParsnip) {
return isLit && isExposedToParsnip;
}
public static boolean isGray(String token) {
// return token.equals("gray");
return token == "gray";
}
public static boolean isInRange(int lo, int hi, int number) {
return lo <= number && number <= hi;
}
public static boolean isMathch(int a, int b, int c) {
return a + b == c || c + a == b || c + b == a;
}
public static boolean isVowel(char c) {
return "aeiouy".contains(c + "");
// return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || (c == 'y' && Math.random() > 0.5);
}
public static boolean isPresidential(int age,
boolean isNaturalBornCitizen,
int nYearsResidency,
int nTermsServed) {
return age >= 35 &&
isNaturalBornCitizen &&
nYearsResidency >= 14 &&
nTermsServed <= 1;
}
}