-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproplogic.java
More file actions
102 lines (80 loc) · 1.99 KB
/
proplogic.java
File metadata and controls
102 lines (80 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public class PropLogic {
public boolean x;
public boolean y;
PropLogic(boolean x, boolean y)
{
this.x = x;
this.y = y;
}
public static boolean AND(boolean x, boolean y)
{
return x && y;
}
public static boolean OR(boolean x, boolean y)
{
return x || y;
}
public static boolean NOT(boolean y)
{
return !y;
}
public static boolean XOR(boolean x, boolean y)
{
return x == y;
}
public static boolean IMPLICATION(boolean x, boolean y)
{
return OR(NOT(x),y);
}
public static boolean CONVERSE(boolean x, boolean y)
{
return OR(NOT(y),x);
}
public static boolean BI_IMPLICATION(boolean x, boolean y)
{
return AND(OR(NOT(x),y),CONVERSE(x,y));
}
public static boolean INVERSE(boolean x, boolean y)
{
return OR(NOT(NOT(x)),NOT(y));
}
public static boolean CONTRAPOSITIVE(boolean x, boolean y)
{
return OR(NOT(NOT(y)),NOT(x));
}
public static String DEMORGAN(String x) // Only works for expressions of form P AND Q, If adding NOT, write : NOTP
{
String c = "";
String[] proparray = x.split(" ");
if(proparray[0].startsWith("NOT"))
{
c = c + proparray[0].substring(3) + " ";
}
else
{
c = c + "NOT" + proparray[0] + " ";
}
if(proparray[1].equals("AND"))
{
c = c + "OR ";
}
else
{
c = c + "AND ";
}
if(proparray[2].startsWith("NOT"))
{
c = c + proparray[2].substring(3);
}
else
{
c = c + "NOT" + proparray[2];
}
return c;
}
public static void main(String[] args) {
// Just checking if DeMorgan works properly
String f = "NOTP AND Q";
System.out.println(DEMORGAN(DEMORGAN(f)).equals(f)); // returns true according to DeMorgans law
}
}