4
4
5
5
/**
6
6
* Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR).
7
+ * This class provides the logic for fundamental Boolean operations that can be
8
+ * used in various computational contexts.
7
9
*/
8
10
public final class BooleanAlgebraGates {
9
11
10
- private BooleanAlgebraGates () {
11
- // Private constructor to prevent instantiation
12
- }
12
+ // Private constructor to prevent instantiation
13
+ private BooleanAlgebraGates () {}
13
14
14
15
/**
15
16
* Interface representing a Boolean gate that takes multiple inputs and returns a result.
16
17
*/
17
18
interface BooleanGate {
19
+ /**
20
+ * Evaluates the Boolean gate with the provided inputs.
21
+ *
22
+ * @param inputs a list of Boolean values representing the gate inputs.
23
+ * @return the result of the Boolean operation.
24
+ */
18
25
boolean evaluate (List <Boolean > inputs );
19
26
}
20
27
21
28
/**
22
29
* AND Gate implementation.
23
- * Returns true if all inputs are true; otherwise, false.
30
+ * Returns true if all inputs are true; otherwise, returns false.
24
31
*
25
- * Test cases for AND Gate:
26
- * - AND([true, true]) should return true.
27
- * - AND([true, false]) should return false.
28
- * - AND([false, false]) should return false.
29
- * - AND([true, true, true]) should return true.
30
- * - AND([true, false, true]) should return false.
31
- *
32
- * Edge case (empty input list):
33
- * - AND([]) can either return true or throw an exception depending on your design.
32
+ * <p>Test cases for AND Gate:</p>
33
+ * <ul>
34
+ * <li>AND([true, true]) should return true.</li>
35
+ * <li>AND([true, false]) should return false.</li>
36
+ * <li>AND([false, false]) should return false.</li>
37
+ * <li>AND([true, true, true]) should return true.</li>
38
+ * <li>AND([true, false, true]) should return false.</li>
39
+ * </ul>
40
+ *
41
+ * <p>Edge case (empty input list):</p>
42
+ * <ul>
43
+ * <li>AND([]) can either return true or throw an exception depending on your design.</li>
44
+ * </ul>
34
45
*/
35
46
static class ANDGate implements BooleanGate {
36
47
@ Override
@@ -46,16 +57,20 @@ public boolean evaluate(List<Boolean> inputs) {
46
57
47
58
/**
48
59
* OR Gate implementation.
49
- * Returns true if at least one input is true; otherwise, false.
60
+ * Returns true if at least one input is true; otherwise, returns false.
50
61
*
51
- * Test cases for OR Gate:
52
- * - OR([true, false]) should return true.
53
- * - OR([false, false]) should return false.
54
- * - OR([true, true, false]) should return true.
55
- * - OR([false, false, false]) should return false.
56
- *
57
- * Edge case (empty input list):
58
- * - OR([]) can either return false or throw an exception.
62
+ * <p>Test cases for OR Gate:</p>
63
+ * <ul>
64
+ * <li>OR([true, false]) should return true.</li>
65
+ * <li>OR([false, false]) should return false.</li>
66
+ * <li>OR([true, true, false]) should return true.</li>
67
+ * <li>OR([false, false, false]) should return false.</li>
68
+ * </ul>
69
+ *
70
+ * <p>Edge case (empty input list):</p>
71
+ * <ul>
72
+ * <li>OR([]) can either return false or throw an exception.</li>
73
+ * </ul>
59
74
*/
60
75
static class ORGate implements BooleanGate {
61
76
@ Override
@@ -73,12 +88,16 @@ public boolean evaluate(List<Boolean> inputs) {
73
88
* NOT Gate implementation (Unary operation).
74
89
* Only accepts a single input and returns the negation.
75
90
*
76
- * Test cases for NOT Gate:
77
- * - NOT(true) should return false.
78
- * - NOT(false) should return true.
91
+ * <p>Test cases for NOT Gate:</p>
92
+ * <ul>
93
+ * <li>NOT(true) should return false.</li>
94
+ * <li>NOT(false) should return true.</li>
95
+ * </ul>
79
96
*
80
- * Edge case:
81
- * Not applicable, as NOT is a unary operation and requires a single input.
97
+ * <p>Edge case:</p>
98
+ * <ul>
99
+ * <li>Not applicable, as NOT is a unary operation and requires a single input.</li>
100
+ * </ul>
82
101
*/
83
102
static class NOTGate {
84
103
/**
@@ -94,17 +113,21 @@ public boolean evaluate(boolean input) {
94
113
95
114
/**
96
115
* XOR Gate implementation.
97
- * Returns true if an odd number of inputs are true; otherwise, false.
116
+ * Returns true if an odd number of inputs are true; otherwise, returns false.
98
117
*
99
- * Test cases for XOR Gate:
100
- * - XOR([true, false]) should return true.
101
- * - XOR([true, true]) should return false.
102
- * - XOR([false, false]) should return false.
103
- * - XOR([true, true, true]) should return true.
104
- * - XOR([true, false, true]) should return false.
105
- *
106
- * Edge case:
107
- * - XOR([]) can either return false or throw an exception.
118
+ * <p>Test cases for XOR Gate:</p>
119
+ * <ul>
120
+ * <li>XOR([true, false]) should return true.</li>
121
+ * <li>XOR([true, true]) should return false.</li>
122
+ * <li>XOR([false, false]) should return false.</li>
123
+ * <li>XOR([true, true, true]) should return true.</li>
124
+ * <li>XOR([true, false, true]) should return false.</li>
125
+ * </ul>
126
+ *
127
+ * <p>Edge case:</p>
128
+ * <ul>
129
+ * <li>XOR([]) can either return false or throw an exception.</li>
130
+ * </ul>
108
131
*/
109
132
static class XORGate implements BooleanGate {
110
133
@ Override
@@ -119,17 +142,21 @@ public boolean evaluate(List<Boolean> inputs) {
119
142
120
143
/**
121
144
* NAND Gate implementation.
122
- * Returns true if at least one input is false; otherwise, false.
145
+ * Returns true if at least one input is false; otherwise, returns false.
123
146
*
124
- * Test cases for NAND Gate:
125
- * - NAND([true, true]) should return false.
126
- * - NAND([true, false]) should return true.
127
- * - NAND([false, false]) should return true.
128
- * - NAND([true, true, true]) should return false.
129
- * - NAND([true, true, false]) should return true.
130
- *
131
- * Edge case:
132
- * - NAND([]) can either return true or throw an exception.
147
+ * <p>Test cases for NAND Gate:</p>
148
+ * <ul>
149
+ * <li>NAND([true, true]) should return false.</li>
150
+ * <li>NAND([true, false]) should return true.</li>
151
+ * <li>NAND([false, false]) should return true.</li>
152
+ * <li>NAND([true, true, true]) should return false.</li>
153
+ * <li>NAND([true, true, false]) should return true.</li>
154
+ * </ul>
155
+ *
156
+ * <p>Edge case:</p>
157
+ * <ul>
158
+ * <li>NAND([]) can either return true or throw an exception.</li>
159
+ * </ul>
133
160
*/
134
161
static class NANDGate implements BooleanGate {
135
162
@ Override
@@ -140,16 +167,20 @@ public boolean evaluate(List<Boolean> inputs) {
140
167
141
168
/**
142
169
* NOR Gate implementation.
143
- * Returns true if all inputs are false; otherwise, false.
170
+ * Returns true if all inputs are false; otherwise, returns false.
144
171
*
145
- * Test cases for NOR Gate:
146
- * - NOR([true, false]) should return false.
147
- * - NOR([false, false]) should return true.
148
- * - NOR([false, false, false]) should return true.
149
- * - NOR([true, true, false]) should return false.
150
- *
151
- * Edge case:
152
- * - NOR([]) can either return true or throw an exception.
172
+ * <p>Test cases for NOR Gate:</p>
173
+ * <ul>
174
+ * <li>NOR([true, false]) should return false.</li>
175
+ * <li>NOR([false, false]) should return true.</li>
176
+ * <li>NOR([false, false, false]) should return true.</li>
177
+ * <li>NOR([true, true, false]) should return false.</li>
178
+ * </ul>
179
+ *
180
+ * <p>Edge case:</p>
181
+ * <ul>
182
+ * <li>NOR([]) can either return true or throw an exception.</li>
183
+ * </ul>
153
184
*/
154
185
static class NORGate implements BooleanGate {
155
186
@ Override
@@ -161,30 +192,42 @@ public boolean evaluate(List<Boolean> inputs) {
161
192
/**
162
193
* Edge Cases and Special Scenarios:
163
194
*
164
- * 1. Empty input list:
165
- * - Test handling of empty input lists for multi-input gates.
166
- * - Should throw an exception if the design assumes an empty list is invalid.
195
+ * <p>1. Empty input list:</p>
196
+ * <ul>
197
+ * <li>Test handling of empty input lists for multi-input gates.</li>
198
+ * <li>Should throw an exception if the design assumes an empty list is invalid.</li>
199
+ * </ul>
167
200
*
168
- * 2. Single input for multi-input gates:
169
- * - AND([true]) -> true
170
- * - OR([false]) -> false
171
- * - XOR([true]) -> true
172
- * - Test behavior with single input as a corner case.
201
+ * <p>2. Single input for multi-input gates:</p>
202
+ * <ul>
203
+ * <li>AND([true]) -> true</li>
204
+ * <li>OR([false]) -> false</li>
205
+ * <li>XOR([true]) -> true</li>
206
+ * <li>Test behavior with single input as a corner case.</li>
207
+ * </ul>
173
208
*
174
- * 3. Mixed inputs:
175
- * - AND([true, false, false, true]) -> false
176
- * - Similar tests for OR, XOR, and other gates.
209
+ * <p>3. Mixed inputs:</p>
210
+ * <ul>
211
+ * <li>AND([true, false, false, true]) -> false</li>
212
+ * <li>Similar tests for OR, XOR, and other gates.</li>
213
+ * </ul>
177
214
*
178
- * 4. Large input lists:
179
- * - AND([true, true, ..., true]) with 1,000 true values -> true.
180
- * - OR with mostly false and one true -> true.
215
+ * <p>4. Large input lists:</p>
216
+ * <ul>
217
+ * <li>AND([true, true, ..., true]) with 1,000 true values -> true.</li>
218
+ * <li>OR with mostly false and one true -> true.</li>
219
+ * </ul>
181
220
*
182
- * 5. Randomized tests:
183
- * - Generate random true/false input lists.
184
- * - Validate expected gate outputs, especially for XOR.
221
+ * <p>5. Randomized tests:</p>
222
+ * <ul>
223
+ * <li>Generate random true/false input lists.</li>
224
+ * <li>Validate expected gate outputs, especially for XOR.</li>
225
+ * </ul>
185
226
*
186
- * 6. Invalid input handling:
187
- * - Test behavior when null is passed as input or within the list.
188
- * - Should either throw an exception or handle per defined behavior.
227
+ * <p>6. Invalid input handling:</p>
228
+ * <ul>
229
+ * <li>Test behavior when null is passed as input or within the list.</li>
230
+ * <li>Should either throw an exception or handle per defined behavior.</li>
231
+ * </ul>
189
232
*/
190
233
}
0 commit comments