I answered the questions on the wget blogs:
- https://raymondysheng.github.io/CSA_Repo//2023/09/19/binarylogic.html
- https://raymondysheng.github.io/CSA_Repo//2023/09/17/boolean-lesson_IPYNB_2_.html The hacks at the end of each blog are below
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)){
isName = true;
} else {
System.out.println("Wrong! L Cope");
}
System.out.println(isName);
}
}
Challenge.main(null);
Guess my name!
Your guess: John
true
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
public class Doggos {
private String breed;
private int age;
public Doggos(String breed, int age) {
this.age = age;
this.breed = breed;
}
public int compareTo(Doggos other)
{
return Integer.compare(this.age, other.age);
}
public String getBreed ()
{
return breed;
}
public static void main(String[] args)
{
Doggos dog1 = new Doggos("Beagle", 4);
Doggos dog2 = new Doggos("Lab", 2);
if (dog1.compareTo(dog2) < 0)
{
System.out.println(dog1.getBreed() + " is younger than " + dog2.getBreed());
}
else if (dog1.compareTo(dog2) > 0)
{
System.out.println(dog1.getBreed() + " is older than " + dog2.getBreed());
}
else
{
if (dog1.compareTo(dog2) < 0)
{
System.out.println(dog1.getBreed() + " is the same age as " + dog2.getBreed());
}
}
}
}
Doggos.main(null);
Beagle is older than Lab
public class LeapYear {
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 = isLeapYear(year);
System.out.println(year + (isLeapYear ? " is" : " is not") + " a leap year.");
if (isLeapYear)
{
System.out.println("It is a leap year");
}
else
{
System.out.println("It is not a leap year");
}
}
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
LeapYear.main(null);
Enter a year:
2024 is a leap year.
It is a leap year
Complete the weird questions below.
Weird questions
-
!(true)&&(false) = ? what in boolean values?
false -
not ((((true and not (false)) ^ false) ^ true) && false) (remember PEMDASNAO!)
not ((((true and true) ^ false) ^ true) && false)
not (((true ^ false) ^ true) && false)
not ((true ^ true) && false)
not (false && false)
not (false)
true -
Prove the following: !A * !(B + !C) = !A * (!B * C)
De Morgan’s: !A * (!B * C) = !A * (!B * C) -
420 && 66 (Hint, convert to binary, then perform the operation)
110100100 && 1000010
0000000 = 0- If you got this one, try 89 OR 42
1011001 || 101010
1111011 = 123
- If you got this one, try 89 OR 42
For each example, you can use code, but then show your work and how you got it. Please ask questions if you are confused!