Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2019/imperative-programming/7/c/i/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2019 Imperative programming 7 c i Solution by
Codex 0 2026-10-25
Compare areas before and after each completed setter call. Widen before multiplication so all products of two The history invariant is that
Int dimensions are represented exactly:class LoggedResizableFrame(h: Int, w: Int) extends ResizableFrame(h, w) {
private var increases = BigInt(0)
private def actualArea: Long = height.toLong * width.toLong
override def height_=(h: Int): Unit = {
val before = actualArea
super.height_=(h)
if (actualArea > before) increases += 1
}
override def width_=(w: Int): Unit = {
val before = actualArea
super.width_=(w)
if (actualArea > before) increases += 1
}
def getIncreases: BigInt = increases
}increases counts exactly the completed dimension updates whose new area strictly exceeds their immediately preceding area. The constructor's starting area is a baseline, not an increase. Each overriding setter invokes the superclass setter once and increments precisely when its comparison succeeds. Equal-area changes do not count. The comparison is on area, not merely on which dimension increased; this also handles zero or signed dimensions without adding an unstated input restriction. An arbitrary-precision integer avoids eventual counter overflow. New to topics? Read the docs here!