Boolean Expressions and If Statements
- What is an If-Else Statement?
- Using If-ElseIf-Else:
- Using Switch-Case-Otherwise:
- Using De Morgan's Law
public class Alarm
{
public static void printSchoolStartTime(String dayOfWeek)
{
System.out.printf("%s --> ", dayOfWeek);
// Day is Monday
if(dayOfWeek.equals("Monday"))
{
System.out.printf("school starts at 8:35\n");
}
// Day is Tuesday
else if(dayOfWeek.equals("Tuesday"))
{
System.out.printf("school starts at 8:35\n");
}
// Day is Wednesday
else if(dayOfWeek.equals("Wednesday"))
{
System.out.printf("school starts at 9:55\n");
}
// Day is Thursday
else if(dayOfWeek.equals("Thursday"))
{
System.out.printf("school starts at 8:35\n");
}
// Day is Friday
else if(dayOfWeek.equals("Friday"))
{
System.out.printf("school starts at 8:35\n");
}
// Day is Saturday or Sunday
else if((dayOfWeek.equals("Saturday")) || (dayOfWeek.equals("Sunday")))
{
System.out.printf("No school!\n");
}
// Day is Invalid
else
{
System.out.printf("Invalid day of the week\n");
}
}
}
Alarm.printSchoolStartTime("Monday");
Alarm.printSchoolStartTime("Wednesday");
Alarm.printSchoolStartTime("Friday");
Alarm.printSchoolStartTime("Sunday");
Alarm.printSchoolStartTime("June");
public class Alarm
{
public static void printSchoolStartTime2(String dayOfWeek)
{
System.out.printf("%s --> ", dayOfWeek);
switch (dayOfWeek)
{
// Day is Monday
case "Monday":
System.out.printf("school starts at 8:35\n");
break;
// Day is Tuesday
case "Tuesday":
System.out.printf("school starts at 8:35\n");
break;
// Day is Wednesday
case "Wednesday":
System.out.printf("school starts at 9:55\n");
break;
// Day is Thursday
case "Thursday":
System.out.printf("school starts at 8:35\n");
break;
// Day is Friday
case "Friday":
System.out.printf("school starts at 8:35\n");
break;
// Day is Saturday || Sunday
case "Saturday":
case "Sunday":
System.out.printf("No school!\n");
break;
// Day is Invalid
default:
System.out.printf("Invalid day of the week\n");
break;
}
}
}
Alarm.printSchoolStartTime2("Monday");
Alarm.printSchoolStartTime2("Wednesday");
Alarm.printSchoolStartTime2("Friday");
Alarm.printSchoolStartTime2("Sunday");
Alarm.printSchoolStartTime2("June");
Using De Morgan's Law
1) De Morgan's Law states that not (A and B) is the same as not A or not B, and not(A or B) is the same as not A and not B
2) Illustration of De Morgan's Law:
A | B | !A | !B | !(A or B) | !A and !B |
---|---|---|---|---|---|
0 | 0 | 1 | 1 | 1 | 1 |
0 | 1 | 1 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 0 | 0 |
1 | 1 | 0 | 0 | 0 | 0 |
A | B | !A | !B | !(A and B) | !A or !B |
---|---|---|---|---|---|
0 | 0 | 1 | 1 | 1 | 1 |
0 | 1 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 | 1 |
1 | 1 | 0 | 0 | 0 | 0 |
3) Code showing De Morgan's Law:
public class Flavors
{
public static void checkFlavor1(String flavor)
{
if (flavor.equals("Chocolate") || flavor.equals("Vanilla"))
{
System.out.printf("I do not like %s\n", flavor);
}
else
{
System.out.printf("I like %s\n", flavor);
}
}
public static void checkFlavor2(String flavor)
{
if (!flavor.equals("Chocolate") && !flavor.equals("Vanilla"))
{
System.out.printf("I like %s\n", flavor);
}
else
{
System.out.printf("I do not like %s\n", flavor);
}
}
}
Flavors.checkFlavor1("Chocolate");
Flavors.checkFlavor1("Vanilla");
Flavors.checkFlavor1("Strawberry");
System.out.printf("\n");
Flavors.checkFlavor2("Chocolate");
Flavors.checkFlavor2("Vanilla");
Flavors.checkFlavor2("Strawberry");