5
\$\begingroup\$

I have classes representing gates (OR, XOR, AND etc) with the following method that is called whenever a property changes.

Here is the XORGate code:

public void computeOutput() {
        if (this.input1 != null && this.input0 != null) {
            if (this.input0.getValue() || this.input1.getValue()) {
                if (this.input0.getValue() && this.input1.getValue()) {
                    this.output.setValue(false);
                }
                else {
                    this.output.setValue(true);
                }

            } else {
                this.output.setValue(false);
            }
        }
        else {
            this.output.Value = false;
        }

    }

It's horrible, but I can't think of a nicer way. It works as follows:

If any of the inputs are null, treat the output as false. If any of the inputs are true BUT not all of the outputs are true (i.e just one is true), set the output to true. If BOTH input terminals are true, output is false. And finally, if none of the input is true, output is false.

Very basic stuff.

\$\endgroup\$
4
\$\begingroup\$
public void computeOutput() {
    output.setValue(
            (input1 == null || input0 == null)
            ? false
            : (input0.getValue() ^ input1.getValue()));
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.