Problem Statement
When we perform an arithmetic operation, the actual result v1 op v2 cannot be determined exactly, but we can determine the interval in which it is known to lie. Create a class Errors that contains the method range that takes two int measurements, m1 and m2, the possible error e, and an arithmetic operation op ('+', '-', '*', or '/') as inputs. The method returns the range, the highest possible result of v1 op v2 minus the lowest possible result, as a String.
m1 and m2 will be given in whole units, and e will be an int giving the error in thousandths of a unit. All operations are "real" operations not restricted or truncated to integers. The return value should be the range rounded to 3 decimal places (round .0005 up). It must contain no leading 0s, and must contain a decimal point followed by exactly three digits. If the range is not finite, or if the undefined result 0/0 is possible, return "INFINITY"
Definition
- Class:
- Errors
- Method:
- range
- Parameters:
- int, int, int, char
- Returns:
- String
- Method signature:
- String range(int m1, int m2, int e, char op)
- (be sure your method is public)
Notes
- '/' denotes real division, not division truncated to the nearest integer
- the error in a value may be greater than its size (see example 1)
Constraints
- m1 will be between -10,000 and 10,000 inclusive
- m2 will be between -10,000 and 10,000 inclusive
- e will be between 1 and 10,000 inclusive
- op will be '+','-','*', or '/'
- all values within .00001 of the range will round to the same 3-place result
Examples
-1
2
500
'-'
Returns: "2.000"
The smallest result would be (-1 - .500) - (2 + .500) = -4 The biggest result would be (-1 + .500) - (2 - .500) = -2 The range is therefore -2 - -4 = 2.2
-1
1250
'*'
Returns: "8.125"
The smallest result would be (3.250 * -2.250) = -7.3125 The largest result would be (3.250 * .250) = .8125 The range is .8125 - -7.3125 = 8.1252
1
1200
'/'
Returns: "INFINITY"
The interval in which the divisor is known to lie includes 0.7237
3590
2
'+'
Returns: ".008"
2
-5
5000
'/'
Returns: "INFINITY"