Homework Hack

Create a Logic Gates Simulator in Python and Java this demonstrates how different logical operations (AND, OR, NOT, NAND, NOR, XOR) work with various inputs. This can visually represent how truth tables function.

Logic Gate Simulator in Python

def AND(A, B):
    return A and B

def OR(A, B):
    return A or B

def NOT(A):
    return not A

print("A     B | AND | OR | NOT A")
print("---------------------------")
for A in [True, False]:
    for B in [True, False]:
        print(f"{A:<5} {B:<5} | {AND(A, B):<4} | {OR(A, B):<3} | {NOT(A)}")

A     B | AND | OR | NOT A
---------------------------
1     1     | 1    | 1   | False
1     0     | 0    | 1   | False
0     1     | 0    | 1   | True
0     0     | 0    | 0   | True

Logic Gate Simulator in Java

%%javascript

import java.util.Scanner;

public class LogicGateSimulator {

    // Define logic gate functions
    public static boolean AND(boolean A, boolean B) {
        return A && B;
    }

    public static boolean OR(boolean A, boolean B) {
        return A || B;
    }

    public static boolean NOT(boolean A) {
        return !A;
    }

    public static boolean NAND(boolean A, boolean B) {
        return !(A && B);
    }

    public static boolean NOR(boolean A, boolean B) {
        return !(A || B);
    }

    public static boolean XOR(boolean A, boolean B) {
        return A ^ B;
    }

    // Display the results of the logic gates
    public static void displayGateOperations(boolean A, boolean B) {
        System.out.println("A: " + A + ", B: " + B);
        System.out.println("AND: " + AND(A, B));
        System.out.println("OR: " + OR(A, B));
        System.out.println("NOT A: " + NOT(A));
        System.out.println("NAND: " + NAND(A, B));
        System.out.println("NOR: " + NOR(A, B));
        System.out.println("XOR: " + XOR(A, B));
        System.out.println("-----------------------------");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Logic Gate Simulator");
        System.out.println("Enter 'exit' to quit");

        while (true) {
            System.out.print("Enter value for A (true/false): ");
            String A_input = scanner.nextLine().trim();
            if (A_input.equalsIgnoreCase("exit")) {
                break;
            }
            
            System.out.print("Enter value for B (true/false): ");
            String B_input = scanner.nextLine().trim();
            if (B_input.equalsIgnoreCase("exit")) {
                break;
            }
            
            // Convert inputs to boolean
            boolean A = Boolean.parseBoolean(A_input);
            boolean B = Boolean.parseBoolean(B_input);

            // Display the results
            displayGateOperations(A, B);
        }

        scanner.close();
    }
}

<IPython.core.display.Javascript object>
<script src="https://utteranc.es/client.js"
        repo="manas12709/portfolio_2025"
        issue-term="pathname"
        label="utterances"
        theme="github-light"
        crossorigin="anonymous"
        async>
    </script>