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"
.
As we did with the relational operators, let’s examine the logical operators in the context of a bunch of predicate methods.
String
gray in American/British English?
Here’s your TODO list to complete before our next lecture:
See you next class!
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…
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);
}
}
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));
}
}
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;
}
}
Comments