java - AND Gate in Logical Circuit -
folks... i'll best explain method gets lot of members other classes logic should straightforward. so, in and gate
of logical circuit, if input signal 0
output 0
; if all
of input signals 1
output 1
; if have signals 0
, x(unknown signal)
, ouput 0
; when 1
, x
output should x
. have bug in code of method because when, let's 0x
signals fed in gate, i'm getting x
instead of 0
. smb please me? note:signal.hi
1
, signal.lo
0
, signal.x
x
. please let me know if more information needed.
public boolean propagate() { signal inputsignal; signal outputfinalsignal; signal temp = getoutput().getsignal(); list<wire> inputs = getinputs(); for(int = 0; < inputs.size(); i++) { inputsignal = inputs.get(i).getsignal(); if(inputsignal == signal.lo) { getoutput().setsignal(signal.lo); break; } else if(inputsignal == signal.x) { getoutput().setsignal(signal.x); break; } else { getoutput().setsignal(inputsignal); } } ........................//unnecessary problem code
your logic should this:
signal result = signal.hi; // default value 1 for(int = 0; < inputs.size(); i++) { inputsignal = inputs.get(i).getsignal(); if(inputsignal == signal.lo) { // input 0 - final result 0 always, break result = inputsignal; break; } else if(inputsignal == signal.x) { // input x, have 0 later, store x , continue without break result = inputsignal; } else { // signal 1, skip meet 0 or x later or met x } } getoutput().setsignal(result);