How do I modify this truth table so that it uses and displays 1's and 0's rather than true and false

StackOverflow https://stackoverflow.com/questions/22912967

  •  29-06-2023
  •  | 
  •  

Question

How do I modify this truth table so that it uses and displays 1's and 0's rather than true and false.

public class Table {
    public static void main(String args[]){

        boolean p,q;

        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

        p = true;
        q = true;

        System.out.print(p+"\t"+q+"\t");
        System.out.print((p&q)+"\t"+(p|q)+"\t");
        System.out.println((p^q)+"\t"+(!p));

        p = true;
        q = false;

        System.out.print(p+"\t"+q+"\t");
        System.out.print((p&q)+"\t"+(p|q)+"\t");
        System.out.println((p^q)+"\t"+(!p));

        p = false;
        q = true;

        System.out.print(p+"\t"+q+"\t");
        System.out.print((p&q)+"\t"+(p|q)+"\t");
        System.out.println((p^q)+"\t"+(!p));

        p = false;
        q = false;

        System.out.print(p+"\t"+q+"\t");
        System.out.print((p&q)+"\t"+(p|q)+"\t");
        System.out.println((p^q)+"\t"+(!p));
    }
}
Was it helpful?

Solution

In Java, there exists no logical xor operator, only one for bitwise operations!

See Oracle.com - Operators as reference


Anyway, here your desired conversion from true and false Strings to 1 and 0:

public static void main(String[] args) {
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT(p)");

    printTable(true, true);
    printTable(true, false);
    printTable(false, true);
    printTable(false, false);
}

private static void printTable(final boolean p, final boolean q) {
    System.out.printf("%s\t", p    ? "1" : "0");
    System.out.printf("%s\t", q    ? "1" : "0");
    System.out.printf("%s\t", p&&q ? "1" : "0");
    System.out.printf("%s\t", p||q ? "1" : "0");
    System.out.printf("%s\t", p^q  ? "1" : "0");
    System.out.printf("%s\t", !p   ? "1" : "0");
    System.out.printf("\n");
}

Output is

P       Q       AND     OR      XOR     NOT (p)
1       1       1       1       0       0   
1       0       0       1       1       0   
0       1       0       1       1       1   
0       0       0       0       0       1   

OTHER TIPS

An easy solution would be to have a function toDigit(boolean value) which returns 0 for false and 1 for true; this could then be used in the print commands rather than just printing the boolean values, e.g.

System.out.print(toDigit(p)+"\t"+toDigit(q)+"\t");
System.out.print(toDigit(p&q)+"\t"+toDigit(p|q)+"\t");
System.out.println(toDigit(p^q)+"\t"+toDigit(!p));

one simple way is :

boolean flag;
//...
System.out.println(flag ? 1 : 0);

This is a simple challenge from the book "Java: A Beginner's Guide - Fifth Edition" by Herbert Schildt. Therefore, I see no problem in giving the complete solution. Below is an adaptation courtesy of blalasaadri's suggestion.

public class Table {
    public static void main(String[] args) {

        boolean p, q;

        // below is an alternative truth table in binary form (0s and 1s)
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");

        p = true; q = true;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");

        p = true; q = false;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");

        p = false; q = true;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");

        p = false; q = false;
        System.out.print(toBinary(p) + "\t" + toBinary(q) + "\t");
        System.out.print(toBinary(p&q) + "\t" + toBinary(p|q) + "\t");
        System.out.println(toBinary(p^q) + "\t" + toBinary(!p) + "\t");
    }

    // this method exchanges true for 1, and false for 0 
    private static int toBinary(boolean value) {
        if(value == true)
            return 1;
        else
            return 0;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top