Your Homework
Now that you know what boolean expressions are and how to write them, as well as several comparison methods, your task is to write a class that uses either the compareTo or comparator and compare. Then create two instances of these classes and demonstrate using if statements.
BONUS: Create a program that checks if a year is a leap year or not.
Here is how the method should work:
(1) Prompt the user to input any year that they would like
(2) Determine if the year is a leap year or not
(3) Print the necessary dialogue (ex. [year] is/is not a leap year) AND return the value of any boolean(s) used
```java import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeapYear = isLeapYear(year);
System.out.println(year + (isLeapYear ? " is a leap year." : " is not a leap year."));
}
public static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
} else {
return false;
}
} } java Copy code // Demo public class Demo {
public static void main(String[] args) {
LeapYearChecker year1 = new LeapYearChecker(2007);
LeapYearChecker year2 = new LeapYearChecker(2020);
boolean isLeap1 = year1.isLeapYear();
boolean isLeap2 = year2.isLeapYear();
System.out.println("2007 is a leap year: " + isLeap1);
System.out.println("2020 is a leap year: " + isLeap2);
} }
Demo.main(null); Weird questions !(true)&&(false) = ? what in boolean values?
!(true)&&(false) = false && false = false
not ((((true and not (false)) ^ false) ^ true) && false) (remember PEMDASNAO!)
not ((((true and not (false)) ^ false) ^ true) && false) = not (((true ^ false) ^ true) && false) = not ((true ^ true) && false) = not (true && false) = not false = true
Prove the following: !A * !(B + !C) = !A * (!B * !C)
!A * (!B * C) = !A * (!C * !B)
Using the law of association, !C * !B = !B * !C
Therefore, !A * (!B * C) = !A * (!C * !B) = !A * (!B * !C)
420 && 66 (Hint, convert to binary, then perform the operation)
420 –> 110100100 66 –> 001000010 (assuming the same number of digits as 420)
110100100 & 001000010 –> 000000000 => 0
If you got this one, try 89 OR 42
89 = 1011001 in binary 42 = 0101010 in binary (assuming the same number of digits as 89)
1011001 & 0101010 –> 1111011 => 123