Parry Chen Parry Chen - 4 months ago 15
Java Question

How to change output of what's printed?

I have this code that prints out a logic table.

It prints out this table.
P Q AND OR XOR NOTp
true true true true false false
true false false true true false
false true false true true true
false false false false false true
I'm trying to change all the outputted true and false to 1 and 0. The first line would look like this.
P Q AND OR XOR NOTp
1 1 1 1 0 0

I'm not even sure where to begin. I've tried if statements. Any help would be appreciated.

public class LogicalOpTable {
public static void main(String[] args) {
boolean p, q;
System.out.println("P\tQ\tAND\tOR\tXOR\tNOTp");

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

Answer

try using this, if the prameter of print is true it returns 1, else 0

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

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

public static String print(boolean stat) {
    if(stat) return "1";        // if stat is true
    return "0";                 // else false
}

your table is correct

Output:

P   Q   AND OR  XOR NOTp
1   1   1   1   0   0
1   0   0   1   1   0
0   1   0   1   1   1
0   0   0   0   0   1
Comments