beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Repeatable Calculator

[email protected] // 1.0 // type-checkable

Write a class named Calculator that keeps track of a running integer total, starting at zero. It should have three methods: add, multiply, and repeat. add takes an integer and adds it to the running total. multiply also takes an integer, but multiplies the running total by it. repeat takes no other parameters, but does the same thing as the last add or multiply call. If add or multiply has not been called yet, repeat should raise a ValueError. All three methods return the value of the running total after they have done their operation. The class could be used like this:

calc = Calculator()
calc.add(1)       # returns 1  (0 + 1)
calc.add(2)       # returns 3  (1 + 2)
calc.repeat()     # returns 5  (3 + 2)
calc.multiply(3)  # returns 15 (5 * 3)
calc.repeat()     # returns 45 (15 * 3)