= Solution
The two writable inputs and Boolean query belong to an <abstract class>; each concrete <logic gate> overrides just that query:
``
abstract class Gate {
var inp1: Boolean = false
var inp2: Boolean = false
def evalGate: Boolean
}
class OrG extends Gate {
override def evalGate: Boolean = inp1 || inp2
}
class AndG extends Gate {
override def evalGate: Boolean = inp1 && inp2
}
class XorG extends Gate {
override def evalGate: Boolean = inp1 ^ inp2
}
``
All three subclasses satisfy the same <object interface>, so the `Gate` declarations in the supplied simulator still allow access to inputs and invoke the correct <Boolean function> through <dynamic dispatch>. Boolean `^` is <exclusive or>; `||` is <Boolean disjunction> and `&&` is <Boolean conjunction>.
The program first sets both inputs of `y`, so $y=\mathrm{true}\land\mathrm{true}=\mathrm{true}$. It sets $z=\mathrm{false}\mathbin{\mathrm{xor}}\mathrm{false}=\mathrm{false}$. Therefore $x=y\lor z=\mathrm{true}$ and $w=z\land\mathrm{true}=\mathrm{false}$, giving
``
x: true
w: false
``
\b[Those are the two output lines.] This is a simulation of <combinational logic> in an order where each gate's inputs have already been calculated; the Boolean assignments copy values rather than create live wire connections.
Back to article page