Dear students,
The software we can now write feels alive. We can get user input. We can ask questions about it. And we can respond in different ways with conditional statements. These conditional statements appear in three general forms. Last time we saw the diversion form, in which we perform some extra work when a condition is true. Today we visit another pattern: the bifurcation.
In a bifurcation, we use a boolean criteria to choose between two mutually exclusive actions. Let’s write some programs that demonstrate this pattern.
Flashmod
—which prompts the user to solve a mod problemSometimes
—which presents a web page that is sometimes red and sometimes blueWhichwise
—at the Ade Olson track, you must run clockwise on even daysBirthday
—is today the day?Here’s your TODO list to complete before we meet again:
See you next class!
P.S. It’s time for a haiku!
Computers don’t think
You know what AI stands for?
A whole lotta Ifs
P.P.S. Here’s the code we wrote together in class…
package lecture1018.cs145;
import java.util.Random;
import java.util.Scanner;
public class FlashMod {
public static void main(String[] args) {
Random generator = new Random();
int a = generator.nextInt(101);
int b = generator.nextInt(10) + 1;
int answer = a % b;
System.out.printf("%d %% %d = ", a, b);
Scanner in = new Scanner(System.in);
int guess = in.nextInt();
if (guess == answer) {
System.out.println("You got it!");
} else {
System.out.println("\uD83D\uDCA9");
}
}
}
package lecture1018.cs145;
import java.util.Random;
public class SometimesRedSometimesBlue {
public static void main(String[] args) {
Random generator = new Random();
String color;
if (generator.nextBoolean()) {
color = "blue";
} else {
color = "red";
}
String html = String.format("<html><head><title>Red || Blue</title></head><body style=\"background-color: %s;\"></body></html>", color);
System.out.println(html);
}
}
package lecture1018.cs145;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Whichwise {
public static void main(String[] args) {
GregorianCalendar now = new GregorianCalendar();
int day = now.get(Calendar.DAY_OF_MONTH);
if (day % 2 == 0) {
System.out.println("\uD83D\uDD03");
} else {
System.out.println("\uD83D\uDD04");
}
}
}
package lecture1018.cs148;
import java.util.Random;
import java.util.Scanner;
public class FlashMod {
public static void main(String[] args) {
// double foo = 5 / 0.0;
// System.out.println(foo + Math.PI);
Random generator = new Random();
int random1 = 30 + generator.nextInt(70);
int random2 = 1 + generator.nextInt(30);
int answer = random1 % random2;
System.out.printf("%d %% %d = ", random1, random2);
Scanner in = new Scanner(System.in);
int guess = in.nextInt();
if (guess == answer) {
System.out.println("\uD83E\uDD20");
} else {
System.out.println("\uD83D\uDE02");
}
}
}
package lecture1018.cs148;
import java.util.Random;
public class Sometimes {
public static void main(String[] args) {
Random generator = new Random();
String color;
if (generator.nextBoolean()) {
color = "blue";
} else {
color = "gold";
}
String html = String.format("<html><head><title>Blu || Gold</title></head><body style=\"background-color: %s;\"></body></html>", color);
System.out.println(html);
}
}
package lecture1018.cs148;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Whichwise {
public static void main(String[] args) {
GregorianCalendar now = new GregorianCalendar();
int day = now.get(Calendar.DAY_OF_MONTH);
if (day % 2 == 1) {
System.out.println("\u27f3");
} else {
System.out.println("\u27f2");
}
}
}
Comments