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