Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON Write a class that represents and defines operators for Interval numbers, which are represented by a pair numeric values: int, float or mixed. We

PYTHON

image text in transcribed

image text in transcribed

image text in transcribed

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. Interval.mid_err(9.8,.05) Interval.mid-err(100,1) (d/(2*g)) . sqrt() d = t print(t) So, with g known +/-.05 m/s-, and d known +/-1 m, the results print as 2.258792382 6805945(+/-0.01 7056289680373204), which indicates that the time will be somewhere between about 2.24 and 2.28 seconds, having a relative error of about 7.6%. 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 Details 1. Define a class named Interval in a module named intervalpy 2. 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 Intervalmid 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)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Publishing With Filemaker Pro On The Web

Authors: Maria Langer

1st Edition

0201696657, 978-0201696653

More Books

Students also viewed these Databases questions

Question

Ty e2y Evaluate the integral dy

Answered: 1 week ago

Question

1. Identify outcomes (e.g., quality, accidents).

Answered: 1 week ago