-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThresholdGate.java
49 lines (42 loc) · 1.33 KB
/
ThresholdGate.java
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
// ThresholdGate.java
import java.util.Scanner;
/** Threshold logic gates
* @author Piotr Smietana
* @version 2019-04-03
* @see Gate
*/
public class ThresholdGate extends InputCountGate {
private int threshold = 0;
/** Constructor, used only from within subclasses.
* @param sc the scanner
* @param name the name of the new gate
*/
public ThresholdGate( Scanner sc, String name ){
super( name );
threshold = ScanSupport.scanPositiveInt( sc, () -> this.toString() );
delay = ScanSupport.scanPositiveFloat( sc, () -> this.toString() );
ScanSupport.finishLine( sc, () -> this + ": followed by" );
}
/** Every subclass of gate offers a sanity check.
*/
public void sanityCheck() {
if (threshold > inCount) {
Errors.warn( this.toString() + ": has threshold > input wires" );
}
}
/** ThresholdGate toString() method.
*/
public String toString() {
return super.toString() + " threshold " + threshold + " " + delay;
}
/** Compute the gate's logical value.
* <p>
* Each subclass must implement this method.
* @param count the number of ones on the input
* @return logic value
*/
public int logicRule( int count ) {
if (count >= threshold) return 1;
return 0;
}
}