Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have created this code for both boolean and integer values to display a truth table for an "AND","OR","XOR", "NOT" gate. However I think that my code needs reviewing as it could be simplified.

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

        boolean p,q;

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

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

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

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

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

        System.out.println();

        withBinary();

    }

    public static void withBinary(){

        System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
        int a = 0;
        int b = 0;
        int and = a&b;
        int or = a|b;
        int xor = a^b;
        int not  = a;


        if(a==0 && b == 0 )
            not = 1;
            System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));

        b=1;
        and = a&b;
        or = a|b;
        xor = a^b;
        not = a;
        if(a==0 && b == 1)
            System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));

        a=1;    
        b=0;
        not = b;
        and = a&b;
        or = a|b;
        xor = a^b;
        not = a;
        if(a==1 && b == 0)
            System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));  

        a=1;    
        b=1;
        not = 0;
        and = a&b;
        or = a|b;
        xor = a^b;
        if(a==1 && b == 1)
            System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));  
        }
}
share|improve this question
    
I don't really get the point of this program. Has it any purpose other than print some debug-values? –  tkausl Aug 6 at 11:35
    
Its just a simple program to print out a truth table for the logic gates, once in boolean and then in integers. It was a question in a book. They already completed the boolean part, and then it says attempt to create a new logic table with the binary equivalent values. –  mp252 Aug 6 at 11:42

1 Answer 1

up vote 8 down vote accepted

First part

You want to iterate through all possible combinations of for a pair of booleans, so you can make the code reflect that explicitly and make it simpler:

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

    for (boolean p : new boolean[] {true, false}) {
        for (boolean q : new boolean[] {true, false}) {
            System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
            System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
        }
    }
    System.out.println();

Second part

You have something suspicious in your if statements. As you don't use braces, this code smells of a copy&paste bug:

    if(a==0 && b == 0 )
        not = 1;
        System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));

If the condition is met, it will execute the line not = 1;. The second line will be executed independently of the condition. I strongly suggest you use braces even for 1-line blocks, like this:

    if (a == 0 && b == 0) {
        not = 1;
        System.out.println(a + "\t" + b + "\t" + and + "\t" + or + "\t" + xor + "\t" + (not));
    }

This way you can avoid this kind of bugs.

Now, applying the same reasoning as with the previous method, you can rewrite it as:

    System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
    for (int a : new int[] {0, 1}) {
        for (int b : new int[] {0, 1} ) {
            System.out.println(a + "\t" + b + "\t" + (a & b) + "\t" + (a | b) + "\t" + (a ^ b) + "\t" + ~a);
        }
    }

BUT What do you want to accomplish with the NOT operation? Do you mean the Bitwise Complement? I used that, but maybe you want a function that returns 0 when it's 1, and 1 when it's 0. In that case, you need to replace the ~a with something like (a == 0) ? 1 : 0.

So the whole code could be reduced to just:

public static void printTable() {
    System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    for (boolean p : new boolean[]{true, false}) {
        for (boolean q : new boolean[]{true, false}) {
            System.out.print(p + "\t" + q + "\t" + (p&&q) + "\t");
            System.out.println((p||q)+"\t"+(p^q)+"\t"+(!p));
          }
    }
    System.out.println();
    System.out.println("A\tB\tAND\tOR\tXOR\tNOT");
    for (int a : new int[]{ 0, 1}) {
        for (int b : new int[]{0, 1} ) {
            System.out.println(a + "\t" + b + "\t" + (a & b) + "\t" + (a | b) + "\t" + (a ^ b) + "\t" + ~a);
        }
    }
}
share|improve this answer
    
You could even wrap your formatting "print" code in named method for enhanced readability. –  Diego Martinoia Aug 6 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.