CS 145 Lecture 17 – Pigeon-holing with Conditional Statements
Agenda
- what ?s
- what does this do?
TODO
- Compose on a 1/4 sheet a question to share before the quiz in lab on Monday.
Revisiting Precedence of && and ||
Is there a combination of values for a
, b
, and c
that produces different values for the two expressions (a && b) || c
and a && (b || c)
?
Enumerating a truthtable helps.
What Does This Do?
Program This
show showCode
Precedence.java
package lecture1010;
public class Precedence {
public static void main(String[] args) {
System.out.println(true && true || false && false);
}
}
Pad.java
package lecture1010;
public class Pad {
public static void main(String[] args) {
}
public static String padTo4(int n) {
if (n < 10) {
return "000" + n;
} else if (n < 100) {
return "00" + n;
} else if (n < 1000) {
return "0" + n;
} else {
return "" + n;
}
}
}
Randaurant.java
package lecture1010;
import java.util.Random;
public class Randaurant {
public static void main(String[] args) {
System.out.println(getRandomRestaurant());
}
public static String getRandomRestaurant() {
Random g = new Random();
int i = g.nextInt(100);
if (i < 3) {
return "Cancun";
} else if (i < 78) {
return "Kwik Trip";
} else if (i < 90) {
return "Holiday";
} else {
return "McDonalds";
}
}
}