Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python class from goody import type_as_str from math import sqrt class Interval:

ID: 3604632 • Letter: P

Question

Python class

from goody import type_as_str
from math import sqrt


class Interval:
pass


  
if __name__ == '__main__':
g = Interval.mid_err(9.8,.05)
print(repr(g))
g = Interval.min_max(9.75,9.85)
print(repr(g))
d = Interval.mid_err(100,1)
t = (d/(2*g)).sqrt()
print(t,repr(t),t.relative_error())

import driver
driver.default_file_name = 'bsc2.txt'
# driver.default_show_exception=True
# driver.default_show_exception_message=True
# driver.default_show_traceback=True
driver.driver()

Problem #2 : Interval Class Problem Summary: Write a class that represents and defines operators for Interval numbers, which are represented by a pair nunerie 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/s". With this class we can perform meric calculations on Interval objects, which automatically keep track of the precision for each calculated value For example, he formula sqrt (d/ (2*)) comutes the aount oftine it takes for a object at sea level to fall a given distance (d) in a vacuum. Given our approximation for g, and a distance that is 100 (t/-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 Interval. mid-err (9.8, . Ob) = g d - Interval. miderr (100, 1) t (d/ (2*g) ) , sqrt () print(t) So, with g knon +/-. 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.21 and 2.28 seconds, having a relative error of about 7.6%. Note that each Interval ob,ject will storeminimum and maximum value in the interval. So 9.8(1/-.05) is stored as an Interval with a minimum of 9. 75 and a maximum of 9.85. Dctails 1. Dcfine a class named Intcrval in a module named interval. py 2. Defin init method that has two parameters; their arguments specify th minimum and maximum values in the interval respectively. Store them in the self variables min and max. Programmers wi1l not use this method directly to construct Interval ohjects instead they will use the static Interval.min_max and Tnterval.mid erT methods (described below)

Explanation / Answer

Answer:

Note: The given program specification is not clear. So based on the understanding I did it.

When compiling with Python 2.7.12, Im getting an error message “No module named driver”. So I removed that part in the main() and compiled the code.

Code:

from goody import type_as_str

from math import sqrt

class Interval:

    def __init__(self, minv, maxv):

        self.min=minv

        self.max=maxv

    @staticmethod

    def mid_err( apval, preval):

        return Interval(apval-preval, apval+preval)

    @staticmethod

    def min_max( minv, maxv):

        return Interval(minv,maxv)

    def __repr__(self):

        return "("+str((self.min+self.max)/2)+", +/-"+str((self.max-self.min)/2)+")"

    def __rmul__(self,other):

        return Interval.min_max(self.min*2 ,self.max*2)

    def __div__(self, other):

        return Interval.min_max(self.min/other.min, self.max/other.max)

    def relative_error(self):

        error = (self.max-self.min)

        return error *100

    def sqrt(self):

        return Interval.min_max(sqrt(self.min), sqrt(self.max))

if __name__ == '__main__':

    g = Interval.mid_err(9.8,.05)

    print(repr(g))

    g = Interval.min_max(9.75,9.85)

    print(repr(g))

    d = Interval.mid_err(100,1)   

    t = (d/(2*g)).sqrt()

    print(t,repr(t),t.relative_error())

Sample Output:

(9.8, +/-0.05)

(9.8, +/-0.05)

((2.25873475935, +/-0.00553191074889), '(2.25873475935, +/-0.00553191074889)', 1.1063821497771187)