2

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

}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
user2742001
  • 21
  • 1
  • 2
  • 1
    In the simplest case you can use `p?1:0`, i.e. the ternary operator. You could go further and add a utility method that wraps the operator. – Boris the Spider Sep 03 '13 at 07:28
  • @BoristheSpider You should write that as an answer. – Fildor Sep 03 '13 at 07:29
  • This is from "a Java beginner's guide 5th edition", I just copied the code. Next time I have a question I'll try to simplify the code. All the replies are really informational, thank you. – user2742001 Sep 04 '13 at 05:55

6 Answers6

2

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));
    }
newuser
  • 8,338
  • 2
  • 25
  • 33
2

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();
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
1

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

Alternatively you may use BooleanUtils from Apache here

Community
  • 1
  • 1
Ean V
  • 5,091
  • 5
  • 31
  • 39
  • You mean "Boolean does not have convertion methods". Of course you can "b?1:0". Introducing a dependency for this seems overkill to me. – Fildor Sep 03 '13 at 07:31
  • I meant not convertible without an if statement. That dependency is for simplicity. You may create your own utility method :) – Ean V Sep 03 '13 at 07:40
0

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
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0
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));
    }       
}
slfan
  • 8,950
  • 115
  • 65
  • 78
0

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");
HouinK
  • 1