16
Day 3 homework
j edited this page 2023-06-08 02:48:04 +02:00

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

Once the template is created, the homework assignment is to commit a BoundedInt.py file to the repository containing the code you have as of step 9 (or 10 if you do the extra credit). You don't need to submit the intermediate code for the other steps, only the final BoundedInt.py. Once you're finished, the entire test suite should pass (all green check marks ✅)

Please do not interpret the below list as a list of questions, it's a sequence of procedures that should guide you to the objective above.

  1. Read this: https://docs.python.org/3/reference/datamodel.html?highlight=int#object.int
  2. Modify the template to pass test 1, which checks that int() called on your class will return the value of i.
  3. Read this, and the paragraph below it: https://docs.python.org/3/reference/datamodel.html?highlight=int#object.add
  4. In that reading material, there's a sentence that starts with "For instance, to evaluate the expression x + y, "... 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.
  5. Don't forget that you can access the value of a BoundedInt by using either .i on it, or int(), now that you've passed test 1.
  6. Modify the template to pass test 2, which checks that:
    • Your class implements the + operator.
    • It throws ValueError when attempting to add another BoundedInt that would exceed self.max.
  7. Do the same as 6, but for subtraction, to pass test 3.
  8. Modify the template so it also supports adding a regular integer to a BoundedInt, to pass test 4. Note that you can use isinstance() to determine the class of a variable.
  9. Same as 8, but for subtraction.
  10. You don't have to implement the code, but think about how you would change the template so it could allow adding a BoundedInt to an emoji, if there was a hypothetical function sentiment(emoji) which returned a floating point number from -10.0 to 10.0 which indicated whether the emoji was happy or sad. e.g. BoundedInt(1, min = 1, max = 10) + 😁 would raise ValueError.