A document from MCS 275 Spring 2021, instructor Emily Dumas. You can also get the notebook file.

Quiz 3

MCS 275 Spring 2021 - Emily Dumas

Instructions:

Deadline

This quiz must be submitted in Gradescope by 12:00pm CST on Tuesday, Februrary 2, 2021.

Resources you are allowed to consult

Quizzes are INDIVIDUAL, closed book, and only allow access to specified resources. For this quiz you can access:

Shorter than usual

There is only one problem on this quiz. I hope a shorter quiz will give you more time to work on Project 1.

(No problem number 1, as usual)

The 3 points assigned by the autograder based on syntax and docstring checking will be listed as problem 1 in Gradescope.

Problem 2: Arithmetic forbidden - 4 points

THIS IS THE ONLY PROBLEM ON THE QUIZ

Make a subclass of the built-in class int called NoArithInt which "forbids arithmetic", meaning that it is not possible to use the operations +, -, *, or / with two instances of NoArithInt.

Save your class definition in a file called quiz3prob2.py and submit it.

Your subclass should NOT have its own constructor, because int already has one that does everything needed. The only methods in your subclass should be special methods that handle the arithmetic operations listed above.

To illustrate the desired behavior, here are some statements using integers that will work:

In [11]:
# make some instances of int
x = 5
y = 15
# Test that arithmetic between ints works
print(x+y) # 20
print(x-y) # -10
print(x*y) # 75
print(y/x) # 3.0
20
-10
75
3.0

And here are some similar statements that attempt to do arithmetic with NoArithInt objects, which should fail. It is assumed here that NoArithInt is available in the same scope as this code is run, so you might need to change it to quiz3prob2.NoArithInt and add import quiz3prob2 if you're running these statements in a separate file.

In [ ]:
# make some no arithmetic integers
x = NoArithInt(5)
y = NoArithInt(15)

# Test that arithmetic between NoArithInt instances fails.

# should give TypeError: unsupported operand type(s) for +: 'NoArithInt' and 'NoArithInt'
print(x+y)

# should give TypeError: unsupported operand type(s) for -: 'NoArithInt' and 'NoArithInt'
print(x-y)

# should give TypeError: unsupported operand type(s) for *: 'NoArithInt' and 'NoArithInt'
print(x*y)

# should give TypeError: unsupported operand type(s) for /: 'NoArithInt' and 'NoArithInt'
print(y/x)

# WARNING: To test the print() statements above, you'll need to run them one at a time.
# If you just copy all of them into a file and run it, execution will stop as soon as
# the first one raises an exception.