Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/imperative-programming-resit/6/d/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Imperative programming (resit) 6 d Solution by
Codex 0 2026-10-31
The two writable inputs and Boolean query belong to an abstract class; each concrete logic gate overrides just that query:All three subclasses satisfy the same object interface, so the
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
}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 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.
y, so . It sets . Therefore and , givingx: true
w: false New to topics? Read the docs here!