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

import java.util.Comparator;

class Book implements Comparable<Book> {
    private String title;
    private int year;

    public Book(String title, int year) {
        this.title = title;
        this.year = year;
    }

    public String getTitle() {
        return title;
    }

    public int getYear() {
        return year;
    }

    @Override
    public int compareTo(Book otherBook) {
        return this.title.compareTo(otherBook.title);
    }

    public static void main(String[] args) {
        Book book1 = new Book("Java Programming", 2020);
        Book book2 = new Book("Python Programming", 2019);

        if (book1.compareTo(book2) < 0) {
            System.out.println(book1.getTitle() + " comes before " + book2.getTitle());
        } else if (book1.compareTo(book2) > 0) {
            System.out.println(book1.getTitle() + " comes after " + book2.getTitle());
        } else {
            System.out.println(book1.getTitle() + " is the same as " + book2.getTitle());
        }
    }
}

Book.main(null);
Java Programming comes before Python Programming
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();
        scanner.close();

        boolean isLeapYear = false;

        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            isLeapYear = true;
        }

        if (isLeapYear) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }

        // Return the boolean value (true if it's a leap year, false if not)
        System.out.println("Is it a leap year? " + isLeapYear);
    }
}

LeapYearChecker.main(null);
Enter a year: 2000 is a leap year.
Is it a leap year? true
  1. !(true)&&(false) = ? what in boolean values? False, NOT true and false = false

  2. not ((((true and not (false)) ^ false) ^ true) && false) (remember PEMDASNAO!) True.

  3. Prove the following: !A * !(B + !C) = !A * (!B * !C) NOT A and NOT(B OR NOT(C)) = NOT(A or NOT(B and C)) !(B or !C) = (!B and !C) !(B or !C) = (!B and !C) Not equivalent, NOT(B) or C is not equivalent to NOT(B and C).

  4. 420 && 66 (Hint, convert to binary, then perform the operation) 420 to binary –> 110100100 66 to binary –> 001000010 = 110100100 && 001000010 = 000000000

    1. If you got this one, try 89 OR 42 000101010 001011001 000001000

For each example, you can use code, but then show your work and how you got it. Please ask questions if you are confused! f