CS 145 Lecture 9
October 7, 2011 by Chris Johnson. Filed under cs145, fall 2011, lectures.
Agenda
- compound booleans
- logical operators && and ||
- ensuring a number in a range
- stopping a command loop
- projectiles
- writing our own String.indexOf
- if/else statements
Code
LogicalFun.java
package lecture;
import java.util.Scanner;
public class LogicalFun {
public static void main(String[] args) {
boolean isMrRight;
boolean isTall = false;
boolean isDark = false;
boolean isHandsome = false;
boolean isRich = true;
isMrRight = (isTall && isDark && isHandsome) || isRich;
isMrRight = (isTall && isDark) ||
(isTall && isHandsome) ||
(isDark && isHandsome);
// boolean isMrWrong = isTall && isDark && isHandsome;
// isMrRight = !isMrWrong;
// System.out.println(isMrRight);
boolean isFemale = false;
boolean isSingle = false;
boolean isMrs = isFemale && !isSingle;
boolean isMr = !isFemale;
boolean isMs = isFemale;
boolean isMiss = isFemale && isSingle;
Scanner in = new Scanner(System.in);
int number = Integer.MIN_VALUE;
// while (!(number >= 0 && number <= 100)) {
while (number < 0 || number > 100) {
System.out.print("Enter a # in [0, 100]: ");
number = in.nextInt();
}
in.nextLine();
boolean wantsToLeave = false;
while (!wantsToLeave) {
System.out.print("> ");
String command = in.nextLine();
// TODO: go do stuff based on command
// if they enter exit, quit, or logout
wantsToLeave = command.equals("exit") || command.equals("leave") || command.equals("abandon") || command.equals("nosebleed") || command.equals("quit") || command.equals("logout");
}
}
}
Conditional.java
package lecture;
public class Conditional {
public static int indexOf(String text, char c) {
// For each letter in text...
for () {
// See if character is c
// if so, we're done!
// if not, keep on truckin'!
}
}
}
TODO
- Read sections 4.2 and 4.3.
Haiku
Boole, that cleaving knife
good/bad, married/single, bah!
“It’s complicated”
show