= Solution
Compare areas before and after each completed setter call. Widen before multiplication so all products of two `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
}
``
The history <invariant> is that `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. \b[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.
Back to article page