Week 10

Source Code

import java.util.*;

public class Week10 {
    public static void main(String[] args) {
        String myName = ""; // add your name
        System.out.println("Week 10" + " - " + myName);

        // Menu
        System.out.println("Menu");
        System.out.println("E1 - Example 1");


        // setup Scanner
        Scanner in = new Scanner(System.in);
        System.out.print("Choice: ");
        String choice = in.nextLine();

        // switch choices
        switch(choice) {
            case "E1":
                System.out.println("Example 1");
                example1();
                break;
            case "E2":
                System.out.println("Example 2");
                example2();
                break;
            case "E3":
                System.out.println("Example 3");
                example3();
                break;
            case "E4":
                System.out.println("Example 4");
                example4();
                break;
            case "E5":
                System.out.println("Example 5");
                example5();
                break;
            case "E6":
                System.out.println("Example 6");
                example6();
                break;

        }
    }
    // example1 method
    public static void example1() {
        Scanner in = new Scanner(System.in);
        System.out.print("A: ");
        boolean A = in.nextBoolean();
        System.out.print("B: ");
        boolean B = in.nextBoolean();
        // De Morgan's First Law
        // Negation of a Disjunction
        
        
    }

    // example2 method
    public static void example2() {
        Scanner in = new Scanner(System.in);
        System.out.print("isWeekday: ");
        boolean isWeekday = in.nextBoolean();
        System.out.print("isEarlyBird: ");
        boolean isEarlyBird = in.nextBoolean();

        // Customer is NOT eligible for any discount
        // (neither weekday NOR early bird applies)


        // It's not a weekday AND it's not before 6pm


        // Checks if these are logically equivalent
        

    }

    // example3 method
    public static void example3() {
        Scanner in = new Scanner(System.in);
        System.out.print("A: ");
        double A = in.nextDouble();
        in.nextLine();
        System.out.print("B: ");
        double B = in.nextDouble();
        in.nextLine();
        // original
        
        
        // applying De Morgan's First Law
        
        
        // simplifying the negation
        
        
    }
    // example4 method
    public static void example4() {
        Scanner in = new Scanner(System.in);
        System.out.print("A: ");
        boolean A = in.nextBoolean();
        System.out.print("B: ");
        boolean B = in.nextBoolean();
        // De Morgan's Second Law
        // Negation of a Conjunction
        
        
    }
    // example5 method
    public static void example5() {
        Scanner in = new Scanner(System.in);
        System.out.print("isValid: ");
        boolean isValid = in.nextBoolean();
        in.nextLine();
        System.out.print("isComplete: ");
        boolean isComplete = in.nextBoolean();
        in.nextLine();
        // original
        

        // applying De Morgan's First Law
        

    }
    // example6 method
    public static void example6() {
        // .equals() method
        

        // == operator
        

        // compare object to null
        

        // object not equal to null
        
    }
}

W10Problem1.java

import java.util.*;

public class W10Problem1 {
    public static void main(String[] args) {
        System.out.println("Problem 1");
        Scanner in = new Scanner(System.in);
        System.out.print("a: ");
        int a = in.nextInt();
        in.nextLine();
        System.out.print("b: ");
        int b = in.nextInt();
        in.nextLine();

        // original
        System.out.println("Original");
        if (!(a < 5 || b >= 10)) {
            System.out.println("Condition met: a is greater than or equal to 5, and b is less than 10.");
        } else {
            System.out.println("Condition not met: Either a is less than 5 or b is greater than or equal to 10.");
        }

        // Simplified condition using De Morgan's Laws
        System.out.println("Simplified");


    }
}

W10Problem2.java

import java.util.*;

public class W10Problem2 {
    public static void main(String[] args) {
        System.out.println("Problem 2");
        Scanner in = new Scanner(System.in);
        System.out.print("num1: ");
        int num1 = in.nextInt();
        in.nextLine();
        System.out.print("num2: ");
        int num2 = in.nextInt();
        in.nextLine();

        // original
        System.out.println("Original");
        if (!((num1 <= 0) || (num2 > 100))) {
            System.out.println("Condition met: num1 is greater than 0 and num2 is less than or equal to 100.");
        } else {
            System.out.println("Condition not met: Either num1 is less than or equal to 0, or num2 is greater than 100.");
        }

        // Simplified condition using De Morgan's Laws
        System.out.println("Simplified");

    }
}

W10Problem3.java

import java.util.*;

public class W10Problem3 {
    public static void main(String[] args) {
        System.out.println("Problem 3");
        Scanner in = new Scanner(System.in);
        System.out.print("x: ");
        int x = in.nextInt();
        in.nextLine();
        System.out.print("y: ");
        int y = in.nextInt();
        in.nextLine();
        System.out.print("z: ");
        int z = in.nextInt();
        in.nextLine();

        // original
        System.out.println("Original");
        if (!(!(x == y) || z != 0)) {
            System.out.println("Condition met: x equals y and z equals 0.");
        } else {
            System.out.println("Condition not met: Either x is not equal to y or z is not 0.");
        }

        // Simplified condition using De Morgan's Laws
        System.out.println("Simplified");

    }
}

W10Problem4.java

import java.util.*;

public class W10Problem4 {
    public static void main(String[] args) {
        System.out.println("Problem 4");
        Scanner in = new Scanner(System.in);
        System.out.print("userLoggedIn: ");
        boolean userLoggedIn = in.nextBoolean();
        System.out.print("hasPermission: ");
        boolean hasPermission = in.nextBoolean();

        // original
        System.out.print("Original");
        if (!(userLoggedIn && hasPermission))  {
            System.out.println("Access denied: User is either not logged in or does not have permission.");
        } else {
            System.out.println("Access granted: User is logged in and has permission.");
        }

        // Simplified condition using De Morgan's Laws
        System.out.print("Simplified");


    }
}

W10Problem5.java

import java.util.*;

public class W10Problem5 {
    public static void main(String[] args) {
        System.out.println("Problem 5");
        Scanner in = new Scanner(System.in);
        System.out.print("temperature: ");
        double temperature = in.nextDouble();
        in.nextLine();
        System.out.print("pressure: ");
        double pressure = in.nextDouble();
        in.nextLine();

        // original
        System.out.print("Original");
        if (!(temperature > 100 && pressure < 50)) {
            System.out.println("Condition met: Either the temperature is less than or equal to 100, or the pressure is greater than or equal to 50.");
        } else {
            System.out.println("Condition not met: The temperature is above 100, and the pressure is below 50.");
        }

        // Simplified condition using De Morgan's Laws
        System.out.print("Simplified");
        

    }
}

W10Problem6.java

import java.util.*;

public class W10Problem6 {
    public static void main(String[] args) {
        System.out.println("Problem 6");
        Scanner in = new Scanner(System.in);
        System.out.print("score: ");
        int score = in.nextInt();
        in.nextLine();
        System.out.print("level: ");
        int level = in.nextInt();
        in.nextLine();

        // original
        System.out.println("Original");
        if (!(score == 100 && level == 10)) {
            System.out.println("Condition met: Either the score is not 100 or the level is not 10.");
        } else {
            System.out.println("Condition not met: The score is 100 and the level is 10.");
        }

        // Simplified condition using De Morgan's Laws
        System.out.println("Simplified");
        

    }
}

W10Problem7.java

import java.util.*;

public class W10Problem7
{
    public static void main(String[] args)
    {
        System.out.println("Problem 7");

        
        
    }
}

W10Problem8.java

import java.util.*;

public class W10Problem8
{
    public static void main(String[] args)
    {
        System.out.println("Problem 8");

        
        
    }
}

W10Problem9.java

import java.util.*;

public class W10Problem9
{
    public static void main(String[] args)
    {
        System.out.println("Problem 9");

        
        
    }
}

Last updated

Was this helpful?