Last active
June 6, 2024 02:46
-
-
Save luckasRanarison/e9a172f6f51cf4c104bbffb5a0895915 to your computer and use it in GitHub Desktop.
Static truth table generator using Java for showcasing polymorphism.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
p q * | |
0 0 1 | |
0 1 1 | |
1 0 0 | |
1 1 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
class Program { | |
public static void main(String[] args) { | |
char[] variables = {'p', 'q'}; | |
int variableCount = variables.length; | |
int possibleCases = (int)Math.pow(2, variableCount); | |
Eval expression = new Impl(new Var('p'), new Var('q')); // p -> q | |
HashMap<Character, Boolean> env = new HashMap<>(); | |
for (char v : variables) { | |
System.out.print(v + " "); | |
} | |
System.out.println("*"); | |
for (int i = 0; i < possibleCases; i++) { | |
boolean[] booleans = numberToBooleanArray(i, variableCount); | |
for (int j = 0; j < variableCount; j++) { | |
env.put(variables[j], booleans[j]); | |
System.out.print((booleans[j] ? 1 : 0) + " "); | |
} | |
System.out.println(expression.eval(env) ? 1 : 0); | |
} | |
} | |
private static boolean[] numberToBooleanArray(int value, int size) { | |
boolean[] result = new boolean[size]; | |
for (int i = size - 1; i >= 0; i--) { | |
result[size - 1 - i] = ((value >> i) & 1) == 1; | |
} | |
return result; | |
} | |
} | |
interface Eval { | |
boolean eval(HashMap<Character, Boolean> env); | |
} | |
class Var implements Eval { | |
char name; | |
public Var(char name) { | |
this.name = name; | |
} | |
@Override | |
public boolean eval(HashMap<Character, Boolean> env) { | |
return env.get(name); | |
} | |
} | |
class Not implements Eval { | |
Eval value; | |
public Not(Eval value) { | |
this.value = value; | |
} | |
@Override | |
public boolean eval(HashMap<Character, Boolean> env) { | |
return !value.eval(env); | |
} | |
} | |
abstract class Binary { | |
protected Eval lhs, rhs; | |
public Binary(Eval lhs, Eval rhs) { | |
this.lhs = lhs; | |
this.rhs = rhs; | |
} | |
} | |
class And extends Binary implements Eval { | |
public And(Eval lhs, Eval rhs) { | |
super(lhs, rhs); | |
} | |
@Override | |
public boolean eval(HashMap<Character, Boolean> env) { | |
return lhs.eval(env) && rhs.eval(env); | |
} | |
} | |
class Or extends Binary implements Eval { | |
public Or(Eval lhs, Eval rhs) { | |
super(lhs, rhs); | |
} | |
@Override | |
public boolean eval(HashMap<Character, Boolean> env) { | |
return lhs.eval(env) || rhs.eval(env); | |
} | |
} | |
class Impl extends Binary implements Eval { | |
public Impl(Eval lhs, Eval rhs) { | |
super(lhs, rhs); | |
} | |
@Override | |
public boolean eval(HashMap<Character, Boolean> env) { | |
return !lhs.eval(env) || rhs.eval(env); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment