Warm Up

Answer the following questions as a group or individually. Write down your answers in your hacks notebook.

  • What is a boolean?

A binary `true` or `false` value.

  • What values can a boolean represent? How many?

It can represent `true` and `false`, so 2 values.

  • What is an example of when we’d use a boolean?

If we wanted the application to know if a certain process has already occurred, a variable `processOccurred` could be set to `true`, which could affect a conditional statement later.

Setup Class

public class ComparisonExample implements Comparable<ComparisonExample> { // uses Comparable to implement comparisons
    private int comp; // initializing variables
    private int comp2;
    // constructor for the comparisonExample class
    public ComparisonExample(int _comp, int _comp2) {
        this.comp = _comp;
        this.comp2 = _comp2;
    }
    // using @Override to override the automatic compareTo method
    @Override
    public int compareTo(ComparisonExample s) {
        // the comp attribute of both objects are compared
        return Integer.compare(this.comp, s.comp);
    }
    // compare method
    public static int compare(ComparisonExample a, ComparisonExample b)
    {
        if(Integer.compare(a.comp, b.comp)==0){ // starts by checking if the comps of the 2 objects are the same
            return Integer.compare(a.comp2, b.comp2); // ends up checking the second comp if the first are the same
        }
        else {
            return Integer.compare(a.comp, b.comp);
        }
    }
}

Describe this code and the code above using comments. Talk about how we define the compareTo in the class definition and how that compares each instance of a class using a certain comparison variables.

See my code comments on both the above and below cells.

In your notes Compare and contrast Comparator<> and compareTo

compareTo:

  • It’s a method inside a class.
  • Used to define a primary way of comparing objects.
  • Ideal for natural ordering and sorting.
  • Objects implement this method themselves.

Comparator:

  • It’s a separate class.
  • Used to define custom or multiple ways of comparing objects.
  • Ideal for flexible sorting.
  • External class defines comparison logic.
ComparisonExample c = new ComparisonExample(2, 2); // different second comp values
ComparisonExample v = new ComparisonExample(2, 3);
System.out.println(c.compareTo(v)); // Allows comparison of these two instances of a class
System.out.println(ComparisonExample.compare(c, v)); // Also allows for the instances

if(ComparisonExample.compare(c, v) < 0){ // using the custom comparison method
    System.out.println("This is less than!"); // result because of the second comp value of c
}
else { System.out.println("it is not"); }
0
-1
This is less than!

Challenge!

public class Challenge {

    private static boolean isName = false;
    private static String name = new String("John");

    public static void main(String [] args){

        Scanner sc = new Scanner(System.in);

        System.out.println("Guess my name!");

        String guess = sc.nextLine();
        System.out.println("Your guess: " + guess);
    
        if(guess.equals(name)){ // the problem was with the equality statement here
            isName = true;
        } else {
            System.out.println("Wrong! L Cope");
        }

        System.out.println(isName);

    }
}

Challenge.main(null);
Guess my name!
Your guess: John
true

I fixed it by changing the `==` equality statement to the `.equals` statement because these are not boolean conditions and thus they cannot be compared this way.

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

I created a class to compare the information of a real celebrity and the information of someone who is suspected to be going undercover as that same celebrity. If their birth years, countries of residence and blood type are the same, they are considered likely matches.

To incorporate the leap year checker, I added a checker for if the birthday of either of the two people being compared is a leap year.

public class UndercoverSuspect {
    private int birthYear;
    private String countryOfResidence;
    private String bloodType;
    private String name;

    // constructor
    public UndercoverSuspect(int birthYear, String countryOfResidence, String bloodType, String name) {
        this.birthYear = birthYear;
        this.countryOfResidence = countryOfResidence;
        this.bloodType = bloodType;
        this.name = name;
    }

    // compareTo function
    public boolean compareTo(UndercoverSuspect other) {
        // important variables
        int yearComparison = Integer.compare(this.birthYear, other.birthYear);
        boolean countryComparison = this.countryOfResidence.equals(other.countryOfResidence);
        boolean bloodTypeComparison = this.bloodType.equals(other.bloodType);

        // using boolean logic
        if ((yearComparison == 0) && countryComparison && bloodTypeComparison) {
            return true;
        } else {return false;}
    }

    // bonus leap year function
    public boolean isLeapYear() {
        return (birthYear % 4 == 0 && birthYear % 100 != 0) || (birthYear % 400 == 0);
    }

    // main demonstration
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // receiving suspect's info
        System.out.println("What is the name of this suspected celebrity (suspected Keanu Reeves)?");
        String suspectName = scanner.nextLine();
        System.out.println("In what country does the suspect live?");
        String suspectLocation = scanner.nextLine();
        System.out.println("What is the blood type of this suspected celebrity?");
        String suspectBloodType = scanner.nextLine();
        System.out.println("What is this person's birth year?\n");
        int suspectBirthYear = scanner.nextInt();

        // examples
        UndercoverSuspect celebrity = new UndercoverSuspect(1964, "USA", "A+", "Keanu Reeves");
        UndercoverSuspect suspect = new UndercoverSuspect(suspectBirthYear, suspectLocation, suspectBloodType, suspectName);
        boolean comparisonResult = celebrity.compareTo(suspect);

        if (comparisonResult) {
            System.out.println(celebrity.name + " and " + suspect.name + " might be the same person!");
        } else {
            System.out.println(celebrity.name + " and " + suspect.name + " are probably different people.");
        }

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

        scanner.close();
    }
}

UndercoverSuspect.main(null);
What is the name of this suspected celebrity (suspected Keanu Reeves)?
In what country does the suspect live?
What is the blood type of this suspected celebrity?
What is this person's birth year?

Keanu Reeves and Frankie Roosevelt might be the same person!
1964 is a leap year.

Binary Logic Hacks

NOT

The NOT operator inverses whatever it is affecting.

The most basic operation is one that does

Question: What NOT(true)

`false`

Inhibition

Question: What would 1(0) be? What about (1)(0)’? What about true&&false?

(1)(0) is 0. (1)(0)'

Nor

**Question: What would 1+0 be? What about !(true   false)**

`1 + 0` is `1`. `!(true || false)` is `false`.

XNOR

Question: what would true^false be? what would !(false^false) be?

`true^false` is `true`. A `!(false^false)` is `true`.

Hacks

Complete the weird questions below.

Weird questions

  1. !(true)&&(false) = ? what in boolean values?

!(true) -> false false && false -> false

`false`

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

(true and not (false)) -> true and true -> true true ^ false -> true true ^ true -> false false && false -> false not (false) -> true

`true`

  1. Prove the following: !A * !(B + !C) = !A * (!B * !C)

If you apply De Morgan's Law:

!(B + !C) = (!B * C)

So !A * !(B + !C) = !A * (!B * C).

I think that last C in the right hand side is supposed to be C and not !C but I’m not sure. If it stays at !C, it cannot be proven.</mark>

  1. 420 && 66 (Hint, convert to binary, then perform the operation) a. If you got this one, try 89 OR 42

Here's 420 && 66.

420: 110100100
66:  001000010

---OUTPUT---
     000000000 = 0

And here's 89 || 42.

89: 01011001
42: 00101010

---OUTPUT---
    01111011 = 123