3
3
import java .util .List ;
4
4
5
5
/**
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.
6
+ * Implements various Boolean algebra gates (AND, OR, NOT, XOR, NAND, NOR)
9
7
*/
10
8
public final class BooleanAlgebraGates {
11
9
12
- // Private constructor to prevent instantiation
13
- private BooleanAlgebraGates () {}
10
+ private BooleanAlgebraGates () {
11
+ // Prevent instantiation
12
+ }
14
13
15
14
/**
16
- * Interface representing a Boolean gate that takes multiple inputs and returns a result.
15
+ * Represents a Boolean gate that takes multiple inputs and returns a result.
17
16
*/
18
17
interface BooleanGate {
19
18
/**
20
- * Evaluates the Boolean gate with the provided inputs.
19
+ * Evaluates the gate with the given inputs.
21
20
*
22
- * @param inputs a list of Boolean values representing the gate inputs .
23
- * @return the result of the Boolean operation .
21
+ * @param inputs The input values for the gate.
22
+ * @return The result of the evaluation .
24
23
*/
25
24
boolean evaluate (List <Boolean > inputs );
26
25
}
27
26
28
27
/**
29
28
* AND Gate implementation.
30
- * Returns true if all inputs are true; otherwise, returns false.
31
- *
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>
29
+ * Returns true if all inputs are true; otherwise, false.
45
30
*/
46
31
static class ANDGate implements BooleanGate {
47
32
@ Override
@@ -57,20 +42,7 @@ public boolean evaluate(List<Boolean> inputs) {
57
42
58
43
/**
59
44
* OR Gate implementation.
60
- * Returns true if at least one input is true; otherwise, returns false.
61
- *
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>
45
+ * Returns true if at least one input is true; otherwise, false.
74
46
*/
75
47
static class ORGate implements BooleanGate {
76
48
@ Override
@@ -86,25 +58,14 @@ public boolean evaluate(List<Boolean> inputs) {
86
58
87
59
/**
88
60
* NOT Gate implementation (Unary operation).
89
- * Only accepts a single input and returns the negation.
90
- *
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>
96
- *
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>
61
+ * Negates a single input value.
101
62
*/
102
63
static class NOTGate {
103
64
/**
104
- * Evaluates the negation of a single input.
65
+ * Evaluates the negation of the input.
105
66
*
106
67
* @param input The input value to be negated.
107
- * @return The negation of the input .
68
+ * @return The negated value .
108
69
*/
109
70
public boolean evaluate (boolean input ) {
110
71
return !input ;
@@ -113,21 +74,7 @@ public boolean evaluate(boolean input) {
113
74
114
75
/**
115
76
* XOR Gate implementation.
116
- * Returns true if an odd number of inputs are true; otherwise, returns false.
117
- *
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>
77
+ * Returns true if an odd number of inputs are true; otherwise, false.
131
78
*/
132
79
static class XORGate implements BooleanGate {
133
80
@ Override
@@ -142,21 +89,7 @@ public boolean evaluate(List<Boolean> inputs) {
142
89
143
90
/**
144
91
* NAND Gate implementation.
145
- * Returns true if at least one input is false; otherwise, returns false.
146
- *
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>
92
+ * Returns true if at least one input is false; otherwise, false.
160
93
*/
161
94
static class NANDGate implements BooleanGate {
162
95
@ Override
@@ -167,67 +100,12 @@ public boolean evaluate(List<Boolean> inputs) {
167
100
168
101
/**
169
102
* NOR Gate implementation.
170
- * Returns true if all inputs are false; otherwise, returns false.
171
- *
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>
103
+ * Returns true if all inputs are false; otherwise, false.
184
104
*/
185
105
static class NORGate implements BooleanGate {
186
106
@ Override
187
107
public boolean evaluate (List <Boolean > inputs ) {
188
108
return !new ORGate ().evaluate (inputs ); // Equivalent to negation of OR
189
109
}
190
110
}
191
-
192
- /**
193
- * Edge Cases and Special Scenarios:
194
- *
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>
200
- *
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>
208
- *
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>
214
- *
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>
220
- *
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>
226
- *
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>
232
- */
233
111
}
0 commit comments