Write a class that represents and defines operators for Interval numbers, which are represented by a pair numeric values: int, float or mixed. We use intervals to represent approximate numbers, whose exact value we do not know. For example, in physics we might know that the acceleration produced by the the force of gravity (g) at sea level is 9.8 m/s2 +/- .05 m/s2, which we will write as 9.8(+/-.05) m/s2. With this class we can perform numeric calculations on Interval objects, which automatically keep track of the precision for each calculated value.For example, the formula sqrt(d/(2*g)) computes the amount of time it takes for an object at sea level to fall a given distance (d) in a vacuum. Given our approximation for g, and a distance that is 100(+/-1) m, we can use the Interval class to compute the amount of time it takes for an object to drop this amount as follows, including the precision with which we know the answer. g = Interval.mid_err(9.8,.05) d = Interval.mid_err(100,1) t = (d/(2*g)).sqrt() print(t)So, with g known +/-.05 m/s2, and d known +/-1 m, the results print as 2.2587923826805945(+/-0.017056289680373204)), which indicates that the time will be somewhere between about 2.24 and 2.28 seconds, having a relative error of about .76%. Note that each Interval object will store the minimum and maximum value in the interval. So 9.8(+/-.05) is stored as an Interval with a minimum of 9.75 and a maximum of 9.85.DetailsDefine a class named Interval in a module named interval.py2. Define an __init__ method that has two parameters; their arguments specify the minimum and maximum values in the interval respectively. Store them in the self variables min and max. Programmers will not use this method directly to construct Interval objects; instead, they will use the static Interval.min_max and Interval.mid_err methods (described below).For information about static methods, read the Class Review lecture notes (look for the entry on Static Methods near the bottom, before the problems).3. Define a static min_max method that has two parameters; their arguments specify the minimum and maximum values in the interval. The second parameter is optional, with None as its default value. This method should raise an AssertionError exception, with an appropriate message, if (a) the first argument is not an int or float numeric type or (b) if the second argument is not a numeric type or None, or (c) the first argument is greater than the second; if the second argument is None, use the first argument for both the minimum and maximum value (creating an interval with one value representing exactly that number). Return the appropriate Interval object.4. Define a static mid_err method that has two parameters; their arguments specify the middle value and the +/- error for the interval. The second parameter is optional, with 0 as its default value. This method should raise an AssertionError exception, with an approprate message, if (a) the first argument is not an int or float numeric type, or (b) if the second argument is not a numeric type, or (c) if the second argument is negative. Return the appropriate Interval object. For example, Interval.mid_err(9.8,.05). would produce the same object as Interval.min_max(9.75,9.85).5. Define the methods best, error, and relative_errorbest returns the best approximation for the Interval (the value at its middle).error returns the error for the Interval: half the distance between its minimum and maximum values.relative_error returns the absolute value of the ratio between the error over the best approximation as a percentage (multiply by 100).6. Define __repr__ method that returns a string, which when passed to eval returns a newly constructed Interval with the same value as the object __repr__ was called on.7. Define a __str__ method that returns a string, with the best approximation for the Interval followed in parentheses by +/- followed by error bounds on the Interval. For example, str(Interval.mid_err(9.8,.05)) returns 9.8(+/-.05)8. Define a __bool__ method that returns True if the Interval represents an interval whose error is not zero: it has different minimum and maximum values.9. Define all the underscore methods needed to ensure the prefix +/- work correctly: + returns the same interval while – returns the appropriate negated interval.10. Define all the underscore methods needed to ensure that the add, subtract, multiply, and true divide (/) operators produce the correct answers when their operands are any combination of an Interval object with an Interval, int, or float object.Treat int and float values as exact/precise values (with no error). In all cases, carefully determine from the minimum and maximum values of the operand Interval(s), what the minimum and maximum values are in the result Interval: doing so requires a bit of deep thinking about arithmetic operators; in some cases it is useful to think about whether Interval is all negative, all positive, or contains 0: having a minimum <= 0 and maximum >= 0.If Python tries to apply an arithmetic operator to an Interval object and any other type of value, return the standard value NotImplemented (which will ultimately cause Python to raise the standard TypeError exception with the standard messsage about unsupported operand types: see what 1+’a’ produces in the Python interpreter). If a divisor’s interval includes zero, it should raise the ZeroDivisionError, including an appropriate message with the offending denominator.11. Define all the underscore methods needed to ensure that the exponentiate operator (**) produces the correct answers when their left operand is an Interval object and their right operand is restricted to be an int object. Use repeated multiplication to solve this problem. In any other case, return the standard value NotImplemented (which will ultimately cause Python to raise the standard TypeError exception with the standard message). Note that if p is negative, computing a**p is equivalent to computing (1/a)**(-p).12. Define all the underscore methods needed to ensure that we can compare Interval objects using the six standard relational operators, with any combination of an Interval object and an Interval, int, or float object.If Python tries to apply a relational operator to an Interval object and any other type of value, return the standard value NotImplemented (which will ultimately cause Python to raise the standard TypeError exception with the standard message about unsupported operand types: see what 1<‘a’ produces in the Python interpreter).For == two Intervals are equal if they have the same minimum and maximum values; they != if either is different. Likewise, an int or float is == to an Interval if the minimum and maximum values of the interval are the same as the int or float.For <, <=, >, >= the rules are more complex: they depend on whether the Interval class’s compare_mode attribute is set to ‘liberal’ or ‘conservative’. For example, when using the < operator in ‘liberal’ mode, the left argument is < the right when its best value is < the right’s best value (or the right itself, if it is an int or float). When using the < operator in ‘conservative’ mode, the left argument is < the right when its maximum value is < the right’s minimum value (or the right itself, if it is an int or float). The other operators work simliarly in ‘liberal’ and ‘conservative’ mode.In fact, these four operators should raise an AssertionError, with an appropriate message, if the Interval class has no compare_mode attribute or this attribute is bound to anything other than ‘liberal’ or ‘conservative’. Initially, the Interval class should have no compare_mode attribute.13. Python automatically provides meanings for +=, -=, *=, /=, and **=.14. Definean __abs__ method for Interval values (hint: what if the interval is all negative, positive, stradles 0)a sqrt method for Interval values.15. Define a __setattr__ method that ensures objects in the Interval class are immutable. The methods that you will write should never bind any instance name (except in __init__, which initializes them) but exclusively returns newly constructed Interval objects with the correct values. If an attempt is made to mutate an object (by defining a new attribute or rebinding an existing attribute), raise an AssertionError with an appropriate messageComputer ScienceEngineering & TechnologyPython Programming ICS 33
solved : Write a class that represents and defines operators for Inte
How it works
- Paste your instructions in the instructions box. You can also attach an instructions file
- Select the writer category, deadline, education level and review the instructionsÂ
- Make a payment for the order to be assigned to a writer
- Â Download the paper after the writer uploads itÂ
Will the writer plagiarize my essay?
You will get a plagiarism-free paper and you can get an originality report upon request.
Is this service safe?
All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades
LET THE PROFESSIONALS WRITE YOUR PAPER!