...
Code Block | ||
---|---|---|
| ||
public int do_operationadd(int a, int b) throws ArithmeticException { long temp = (long)a + (long)b; if (temp > Integer.MAX_VALUE || temp < Integer.MIN_VALUE) { throw new ArithmeticException("Out of range"); } return (int)temp; // Value within range; can perform the addition } |
...
Code Block | ||
---|---|---|
| ||
public int do_operationadd(int a, int b) throws ArithmeticException { if( b > 0 ? a > Integer.MAX_VALUE - b : a < Integer.MIN_VALUE - b ) { throw new ArithmeticException("Not in range"); } return a + b; // Value within range so addition can be performed } |
Code Block | ||
---|---|---|
| ||
public int add(int a, int b) throws ArithmeticException {
if (((a > 0) && (b > 0) && (a > (Integer.MAX_VALUE - b)))
|| ((a < 0) && (b < 0) && (a < (Integer.MIN_VALUE - b)))) {
throw new ArithmeticException("Not in range");
}
else {
return a + b; // Value within range so addition can be performed
}
|
Compliant Solution (Use BigInteger Class)
...