diff --git a/Day 3 homework.md b/Day 3 homework.md new file mode 100644 index 0000000..0a3471e --- /dev/null +++ b/Day 3 homework.md @@ -0,0 +1,40 @@ +We're going to implement a class, `BoundedInt`, that only allows arithmetic within a given range of integers. For simplicity, we're only going to support addition and subtraction for now. + +Here's the standard class template, which we'll go over in more detail later on. The test program is here: + +https://gogs.gtsplab.com:5432/j/python-test/src/main/test13.py + +You can either append it to the template below, or save the template as BoundedInt.py in the same directory and run test13.py to execute the tests. + +``` +class BoundedInt(): + def __init__(self, i, min, max): + """ + Create a bounded integer. + + Args: + i (int): The initial value of the integer. + min (int): The minimum value of the integer. + max (int): The maximum value of the integer. + """ + self.i = i + self.min = min + self.max = max +``` + +1. Read this: + +https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__int__ + +Modify the template to pass test 1, which checks that `int()` called on your class will return the value of `i`. + +2. Read this, and the paragraph below it: https://docs.python.org/3/reference/datamodel.html?highlight=__int__#object.__add__ + +What do you think the types of x and y are? Guess, without checking first. What are they, actually? You can use the debugger's local window to find out. + +Modify the template to pass test 2, which checks that: + + * Your class implements the + operator. + * It throws ValueError when attempting to add two BoundedInts that will exceed the limits. + +3. Do the same as 2, but