Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating truth tables in Java

I'm trying to print some truth tables as part of a school assignment. How can I generate a dynamic size truth table in Java?

So that printTruthTable(1) prints:

0
1

printTruthTable(3) prints:

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

And so on. I have been trying to implement it using recursion, but I just can't get it right.

like image 435
David Weng Avatar asked May 23 '12 15:05

David Weng


People also ask

How do you create a truth table in Java?

Well if you want to make a truth table, just put a for loop around your big if/elseif/else block of logic and set a,b,c,&d at the start of the loop. Your existing println() calls will fill in the last table column.

How do you create a table in Java?

How to Create a Table in Java. To create a table, you need to make an instance of the JTable class. You need to provide two arguments (row and column) in its constructor for the table to be constructed, as shown in this example code snippet: JTable table = new JTable (row, column);

Can we create a table in Java?

First, prepare a CREATE TABLE statement to create the table you want. Second, connect to the database. Third, create a new instance of the Statement class from a Connection object. Fourth, execute the CREATE TABLE statement by calling the executeUpdate() method of the Statement object.

How do you print the truth table in Python?

Here is the Python code for our Truth Table Generator function truthTable() which takes two parameters: A Boolean expression: e.g. A AND NOT (B OR C) The number of inputs: either 2, 3 or 4: A, B, C and D.


3 Answers

here's my take on your problem, all written nice and tight in a small class, just copy/paste

notice how I used modulo2 (the % sign) to get 0's and 1's from the loop indices

public class TruthTable {
    private static void printTruthTable(int n) {
        int rows = (int) Math.pow(2,n);

        for (int i=0; i<rows; i++) {
            for (int j=n-1; j>=0; j--) {
                System.out.print((i/(int) Math.pow(2, j))%2 + " ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        printTruthTable(3); //enter any natural int
    }
}
like image 66
svarog Avatar answered Oct 08 '22 19:10

svarog


This is not a truth table - rather, it's a table of binary numbers. You can use Java's Integer.toBinaryString method to generate the zeros and ones that you need; inserting spaces should be trivial.

int n = 3;
for (int i = 0 ; i != (1<<n) ; i++) {
    String s = Integer.toBinaryString(i);
    while (s.length() != 3) {
        s = '0'+s;
    }
    System.out.println(s);
}
like image 42
Sergey Kalinichenko Avatar answered Oct 08 '22 21:10

Sergey Kalinichenko


The magic of recursion:

public static void main(String args[]) {
    int size = 3;
    generateTable(0, size, new int[size]);
}

private static void generateTable(int index, int size, int[] current) {
    if(index == size) { // generated a full "solution"
        for(int i = 0; i < size; i++) {
            System.out.print(current[i] + " ");
        }
        System.out.println();
    } else {
        for(int i = 0; i < 2; i++) {
            current[index] = i;
            generateTable(index + 1, size, current);
        }
    }
}
like image 38
Tudor Avatar answered Oct 08 '22 19:10

Tudor