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
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.125
2
1
1200
'/'
Returns: "INFINITY"
The interval in which the divisor is known to lie includes 0.
9999
1
999
'/'
Returns: "9994997.499"
9999, 1, 999, '/': return "9994997.499
7237
3590
2
'+'
Returns: ".008"
0
0
10000
'+'
Returns: "40.000"
-5
5
9999
'-'
Returns: "39.996"
2
7
7001
'*'
Returns: "196.042"
-2
7
6997
'*'
Returns: "195.874"
-17
-8
6543
'*'
Returns: "327.150"
0
0
92
'*'
Returns: ".017"
0
1
1
'/'
Returns: ".002"
0
0
1
'/'
Returns: "INFINITY"
0
1
1
'/'
Returns: ".002"
5
0
1
'/'
Returns: "INFINITY"
2
1
1000
'/'
Returns: "INFINITY"
2
-5
5000
'/'
Returns: "INFINITY"
10000
10
9999
'/'
Returns: "10009499.475"
1
60
10000
'/'
Returns: ".400"
5
5
2499
'/'
Returns: "2.665"
7237
3590
2
'+'
Returns: ".008"
100
20
50
'/'
Returns: ".030"
-500
-500
1000
'*'
Returns: "2000.000"
-100
100
10
'*'
Returns: "4.000"
10000
200
1
'/'
Returns: ".001"
20
10
3
'/'
Returns: ".002"
1
0
100
'/'
Returns: "INFINITY"
10000
205
1
'/'
Returns: ".000"
0
10
1000
'/'
Returns: ".222"
0
10
1
'/'
Returns: ".000"
1
-1
100
'/'
Returns: ".404"
2
1
500
'/'
Returns: "4.000"
10000
10000
10000
'+'
Returns: "40.000"
1
5
2000
'/'
Returns: "1.333"
10000
10000
10000
'*'
Returns: "400000.000"
3
5
3000
'/'
Returns: "3.000"
1
5
12
'-'
Returns: ".048"
4
0
40
'/'
Returns: "INFINITY"
0
2
500
'/'
Returns: ".667"
0
0
10000
'+'
Returns: "40.000"
10000
10000
10000
'/'
Returns: ".004"
1
1
1000
'-'
Returns: "4.000"
5
5
1
'+'
Returns: ".004"