modifying a logical operator truth table

3.3k views Asked by At

Everyone I was wondering if there's a simple way of converting the true and false values of this Java program, into 1's and 0's without doing too much manual labour?. I figured adding the data type "byte" and having two separate variable values of 1 and 0 and manually inputting them into the code would do but, is there a simpler way?

public class LogicalOpTable1 {

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));
    }

}
6

There are 6 answers

0
Boris the Spider On

You have much code duplication. Never copy paste, Always create methods. This will make your code readable and maintainable. Even in a simple exercise like this it is always good practice.

The easiest way to print 1 or 0 is to use the ternary operator, boolean ? 1 : 0 will do the trick in very few characters.

Now, splitting your code into methods and applying the ternary operator we have:

public static void main(String args[]) {

    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    System.out.println(row(true, true));
    System.out.println(row(true, false));
    System.out.println(row(false, true));
    System.out.println(row(false, false));
}

private static String row(final boolean p, final boolean q) {
    return tabDelimited(p, q) + tabDelimited(p & q, p | q) + tabDelimited(p ^ q, !p);
}

private static String tabDelimited(final boolean a, final boolean b) {
    return (a ? 1 : 0) + "\t" + (b ? 1 : 0) + "\t";
}

A little bit better you'll agree?

Output:

P   Q   AND OR  XOR NOT
1   1   1   1   0   0   
1   0   0   1   1   0   
0   1   0   1   1   1   
0   0   0   0   0   1

You can then make the whole lot a little less hard-coded by making the tabDelimited method print any number of booleans:

public static void main(String args[]) {

    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    System.out.println(row(true, true));
    System.out.println(row(true, false));
    System.out.println(row(false, true));
    System.out.println(row(false, false));
}

private static String row(final boolean p, final boolean q) {
    return tabDelimited(p, q, p & q, p | q, p ^ q, !p);
}

private static String tabDelimited(final boolean... toPrint) {
    if (toPrint.length == 0) {
        return "";
    }
    final StringBuilder sb = new StringBuilder();
    sb.append(toPrint[0] ? 1 : 0);
    for (int i = 1; i < toPrint.length; ++i) {
        sb.append("\t").append(toPrint[i] ? 1 : 0);
    }
    return sb.toString();
}
2
Ean V On

In Java boolean is not convertible to int (1 or 0). Have a look at this

Alternatively you may use BooleanUtils from Apache here

0
newuser On

Same code repeatedly used. Create a simple method by passing the boolean values as arguments.

Method creation

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

        Sample sample = new Sample();
        sample.display(true, true);
        sample.display(true, false);
        sample.display(false, true);
        sample.display(false, false);

    }

    private void display(boolean p, boolean q)
    {
        System.out.print(p + "\t" + q + "\t");
        System.out.print((p & q) + "\t" + (p | q) + "\t");
        System.out.println((p ^ q) + "\t" + (!p));
    }
0
Pshemo On

This question seems to be opinion based. If you care mostly on form of presentation then IMHO simplest would be your way mentioned in question -> changing p and q to integers. You would only have to change way of calculating NOT from !p to something like 1-p or 1^p because there is no single byte NOT operator.

Your code can look like

private static void printRow(int p, int q) {
    System.out.print(p + "\t" + q + "\t");
    System.out.print((p & q) + "\t" + (p | q) + "\t");
    System.out.println((p ^ q) + "\t" + (1 - p));
}

public static void main(String args[]) throws Exception {

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

    printRow(1, 1);
    printRow(1, 0);
    printRow(0, 1);
    printRow(0, 0);
}

output:

P   Q   AND OR  XOR NOT
1   1   1   1   0   0
1   0   0   1   1   0
0   1   0   1   1   1
0   0   0   0   0   1
0
NewLearner On
public class Chapter2 {

    public static void main(String[] args) {
        System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
        Compare(true ,true);
        Compare(true ,false);
        Compare(false ,true);
        Compare(false ,false);
    }
    static void Compare(boolean p, boolean q) {
        System.out.println((p?0:1) + "\t" + (q?0:1) +"\t"+((p&q)?0:1) + "\t" + 
            ((p|q)?0:1) + "\t" +((p^q)?0:1) + "\t" + ((!p)?0:1));
    }       
}
0
HouinK On

I think it is a way to do this, since in that part of the book methods, :?, etc, and even if else aren't covered yet :

System.out.print(p + "\t" + q + "\t" + (p * q) + "\t");
if(p != q) System.out.println((p + q) + "\t" + (p + q) + "\t" + q);
if((p == q) & (p == 1)) System.out.println(p + "\t0\t0");
if((p == q) & (p == 0)) System.out.println(p + "\t0\t1");