Booleans
A demonstration of boolean expressions in Java with if-else statements
Code tour
This is a program created to simulate a grade calculator, so that students can enter their grade as an integer and see what their letter grade is. Depending on the grade, the program also returns some advice based on the user's academic situation. To do this I used the scanner class and multiple if-else if-else statements to assign a value for the different cases. In the code you can see how the variables are called in different parts of the program. In the first part of the program, I created a Scanner object to take the user's input as an integer. The different if statements determine the value assigned to the value variable. A 90 or above (A) is assigned value = 1, and so on. There is also the default value of 0, which is used to handle improper input. After, the value variable is called as a parameter for the switch cases, which print the output based on the grade.
import java.util.Scanner; // importing scanner to take input
int grade = 0; // grade input variable
int value = 0; // assigning a value based on grade input for cases
public class gradeCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //making Scanner instance from class to take input
System.out.println("Enter your grade in a class as an integer (ex: 90, 36) and get some words of advice!"); // printing message
int grade = input.nextInt(); //scanning for next input
if (grade >= 90) { // if grade is 90 or above
value = 1;
}
else if (grade < 90 && grade >= 80) { // below 90 but above/equal to 80
value = 2;
}
else if (grade < 80 && grade >= 70) { // below 80 but above/equal to 70
value = 3;
}
else if (grade < 70 && grade >= 60) { // below 70 but above/equal to 60
value = 4;
}
else if (grade < 60) { // below 60 (failing)
value = 5;
}
else { // invalid input
value = 0;
}
switch (value) { // using cases for each situation determined by input
case 1: // A grade
System.out.println("You currently have an A");
System.out.println("Congrats! You've worked hard to reach that A, but don't forget that it's okay to meet obstacles and ask for help when you need it.");
break;
case 2: // B grade
System.out.println("You currently have a B");
System.out.println("You're doing great! Keep doing what you're doing, and try to find areas you can improve on.");
break;
case 3: // C grade
System.out.println("You currently have a C");
System.out.println("This is technically an average grade, but for some the effort it takes to reach this is anything but. Well done!");
break;
case 4: // D grade
System.out.println("You currently have a D");
System.out.println("You're currently hanging on the edge. If you're motivated and work hard, I know you can change it!");
break;
case 5: // F grade
System.out.println("You currently have an F");
System.out.println("This isn't the end of the world. There are many resources like school counselors and tutors that can help to pull you up. I believe in you!");
break;
case 0: // bad input
System.out.println("Please enter the input correctly!");
}
}
}
gradeCalculator.main(null);
De Morgan's Laws
De Morgan's laws states that "the negation of a disjunction is the conjunction of the negations", and vice versa. Essentially, if a true/false statement uses "not(X or Y)", then it would be equivalent to "not(X) AND not(Y)". If the original statement in parentheses was "X and Y", then the "AND" in the interpretation would be switched to "or".
boolean x = false;
boolean y = false;
/* the statement is going to print based on the condition that ((NOT(x)) and (NOT(y)) is true. It demonstrates that De Morgan's laws
are valid because the operator is equivalent to NOT(x or y), which also evaluates to true. */
this statement is going to print if ((NOT(X)) or (NOT(Y))) is true, which it should be. Therefore
if ((!x) && (!y)) {
System.out.println("If you're seeing this, then it means De Morgan's laws are valid.");
}