If-Else Lesson
Lesson on how to use conditionals in Java
public class IfElseLesson{
public static void main(String[] args) {
String weather = "rainy"; // assign variable weather to the current weather (rainy)
// if the weather is rainy, remind the user to bring an umbrella
if (weather == "rainy") {
System.out.println("Don't forget to bring an umbrella!");
}
}
}
IfElseLesson.main(null)
public class IfElseLesson{
public static void main(String[] args) {
String weather = "sunny"; // assign variable weather to sunny
// if weather is rainy, remind to bring an umbrella
// otherwise, no need to bring an umbrella
if (weather == "rainy") {
System.out.println("Don't forget to bring an umbrella!");
} else {
System.out.println("No need for an umbrella!");
}
}
}
IfElseLesson.main(null)
public class IfElseLesson{
public static void main(String[] args) {
String weather = "rainy"; // assign variable weather to rainy
// if weather is rainy, remind to bring an umbrella
// if weather is sunny, remind to bring a hat
if (weather == "rainy") {
System.out.println("Don't forget to bring an umbrella!");
} else if (weather == "sunny") {
System.out.println("Don't forget to bring a hat!");
}
}
}
IfElseLesson.main(null)
Setting variable weather
to sunny:
public class IfElseLesson{
public static void main(String[] args) {
String weather = "sunny";
if (weather == "rainy") {
System.out.println("Don't forget to bring an umbrella!");
} else if (weather == "sunny") {
System.out.println("Don't forget to bring a hat!");
}
}
}
IfElseLesson.main(null)
public class IfElseLesson{
public static void main(String[] args) {
String weather = "cloudy"; // assign variable weather to cloudy
// because it is cloudy, print that the weather is nice
if (weather == "rainy") {
System.out.println("Don't forget to bring an umbrella!");
} else if (weather == "sunny") {
System.out.println("Don't forget to bring a hat!");
} else {
System.out.println("Nice weather outside!");
}
}
}
IfElseLesson.main(null)
import java.util.Scanner;
public class Jeopardy{
public static void main(String[] args) {
System.out.println("Welcome to Jeopardy! Choose 100, 200, 300, 400, 500");
int questionNum = 0; // keep track of selecting the 100, 200, 300, 400, or 500 question
int score = 0;
String answer = "";
Scanner sc = new Scanner(System.in);
questionNum = sc.nextInt(); // for questionNum input
System.out.println(questionNum);
Scanner questionManySpace = new Scanner(System.in); // for answer input
if (questionNum == 100) {
// ask question
System.out.println("What programming language does this code run in? ");
answer = questionManySpace.nextLine();
// if answer is correct, add 100 points to score
System.out.println(answer);
if (answer.equals("java")) {
score += 100;
}
// the format is the same for all of the answers below
} else if (questionNum == 200) {
System.out.println("How do you create a string called 'name' and assign it to 'Bob' in Java? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("String name = Bob;")) {
score += 200;
}
} else if (questionNum == 300) {
System.out.println("What is the conditional called to do something if something happens or do something else if something else happens? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("if-else")) {
score += 300;
}
} else if (questionNum == 400) {
System.out.println("How do you create a scanner object with name scanner? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("Scanner scanner = new Scanner(System.in)")) {
score += 400;
}
} else if (questionNum == 500) {
System.out.println("What backend framework runs on lwu1822.tk? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("Spring")) {
score += 500;
}
}
System.out.println("Your total score is: " + score);
}
}
Jeopardy.main(null);
import java.util.Scanner;
public class Jeopardy{
public static void main(String[] args) {
System.out.println("Welcome to Jeopardy! Choose 100, 200, 300, 400, 500");
int questionNum = 0; // keep track of selecting the 100, 200, 300, 400, or 500 question
int score = 0;
String answer = "";
Scanner sc = new Scanner(System.in);
questionNum = sc.nextInt(); // for questionNum input
System.out.println(questionNum);
Scanner questionManySpace = new Scanner(System.in); // for answer input
switch (questionNum) {
case 100:
// ask question
System.out.println("What programming language does this code run in? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
// if answer is correct, add 100 points to score
if (answer.equals("java")) {
score += 100;
}
// the format is the same for all of the answers below
case 200:
System.out.println("How do you create a string called 'name' and assign it to 'Bob' in Java? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("String name = Bob;")) {
score += 200;
}
case 300:
System.out.println("What is the conditional called to do something if something happens or do something else if something else happens? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("if-else")) {
score += 300;
}
case 400:
System.out.println("How do you create a scanner object with name scanner? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("Scanner scanner = new Scanner(System.in)")) {
score += 400;
}
case 500:
System.out.println("What backend framework runs on lwu1822.tk? ");
answer = questionManySpace.nextLine();
System.out.println(answer);
if (answer.equals("Spring")) {
score += 500;
}
}
System.out.println("Your total score is: " + score);
}
}
Jeopardy.main(null);
De Morgan's Law
First, some vocab:
- Set: A group of objects
- Universal set: A set that has all of the elements
- Subsets: Contains elements from the universal set
- Complement: For example, if we have set A, the complement of set A would have the elements in the universal set, but not in set A.
-
Union: New set has elements from both sets
Symbol: ∪
-
Intersection: New sets has elements that are shared between both sets
Symbol: ∩
De Morgan's Law is used in boolean algebra to help simplify expressions (this is important, remember this for later).
De Morgan's Law of Union states that: "The complement of the union of the two sets A and B will be equal to the intersection of A' (complement of A) and B' (complement of B)." 1
Formula: (A ∪ B)' = A'∩ B'
De Morgan's Law of Intersection states that: "The complement of the intersection of A and B will be equal to the union of A' and B'." 2
Formula: (A ∩ B)’ = A’ ∪ B’
The following is an example:
Universal set: {1, 3, 5, 6, 8, 9} Set A: {1, 3} Set B: {5, 6, 8}
De Morgan's Law of Union: (A ∪ B)' = {9}
A'∩ B' = {9}
(A ∪ B)' = A'∩ B'
De Morgan's Law of Intersection: (A ∩ B)' = {1, 3, 5, 6, 8, 9}
A’ ∪ B’ = {1, 3, 5, 6, 8, 9}
(A ∩ B)’ = A’ ∪ B’
Now how is this useful?
Remember this?
Translating De Morgan's law into code, De Morgan's Law of Union would mean: !(a || b) = (!a && !b)
.
De Morgan's Law of Intersection means: !(a && b) = (!a || !b)
3
public class DeMorgansLaw{
public static void main(String[] args) {
boolean rainy = false;
boolean sunny = false;
if (!!!(rainy || sunny) && ((!rainy && !sunny) || !(!(rainy || sunny))) ) {
System.out.println("It's a cloudy day, not too hot, not too cold");
}
}
}
DeMorgansLaw.main(null)
Well this is awfully confusing, how about we clean it up a little?
First, remove all of the !!
(negative + negative = positive)
This code reads: If it's not rainy or sunny, and: it's not rainy and it's not sunny OR it's rainy or sunny
Evaluating each part of the code:
-
If it's not rainy or sunny
: True -
If it's not rainy and it's not sunny
: True -
It's rainy or sunny
: False
2. and 3. are OR, therefore, it is True
.
1. is ANDed to 2. and 3. which is True
. -> True
AND True
is true, therefore, the message is outputted.
public class DeMorgansLaw{
public static void main(String[] args) {
boolean rainy = false;
boolean sunny = false;
if (!(rainy || sunny) && ((!rainy && !sunny) || (rainy || sunny)) ) {
System.out.println("It's a cloudy day, not too hot, not too cold");
}
}
}
DeMorgansLaw.main(null)
This code reads: If it's not sunny and it's not cloudy and it's not snowy and it's not windy, then bring an umbrella.
public class DeMorgansLaw{
public static void main(String[] args) {
boolean rainy = true;
boolean sunny = false;
boolean cloudy = false;
boolean snowy = false;
boolean windy = false;
if (!sunny && !cloudy && !snowy && !windy) {
System.out.println("Bring an umbrella!");
}
}
}
DeMorgansLaw.main(null)
This code is kind of messy, huh?
How about this code?
This code reads: If it's not sunny or cloudy or snowy or windy, then bring an umbrella.
Better?
public class DeMorgansLaw{
public static void main(String[] args) {
boolean rainy = true;
boolean sunny = false;
boolean cloudy = false;
boolean snowy = false;
boolean windy = false;
if (!(sunny || cloudy || snowy || windy)) {
System.out.println("Bring an umbrella!");
}
}
}
DeMorgansLaw.main(null)
A more complicated example...
public class DeMorgansLaw{
public static void main(String[] args) {
boolean pineappOnPizza = true;
boolean nopineappOnPizza = false;
System.out.println("Is pineapple on pizza bad?");
// huh???
if (!(!nopineappOnPizza && !pineappOnPizza)) {
System.out.println("No!");
}
}
}
DeMorgansLaw.main(null)
Is this better?
public class DeMorgansLaw{
public static void main(String[] args) {
boolean pineappOnPizza = true;
boolean nopineappOnPizza = false;
System.out.println("Is pineapple on pizza bad?");
// if no pineapple on pizza or yes pineapple on pizza...
// since there is a pineapple on pizza, then pineapple on pizza is good
if ((pineappOnPizza || nopineappOnPizza)) {
System.out.println("No!");
}
}
}
DeMorgansLaw.main(null)