Data Structures, Algorithms, & Applications in Java
Chapter 1, Exercise 7
- (a)
-
The data member
dollar is of type
long. The maximum value assignable to a variable
of type
long is
java.lang.Long.MAX_VALUE = 9223372036854775807.
The maximum allowable value for
cents is 99.
Therefore, when the representation of Program 1.7 is used,
the maximum permissible currency value is
Long.MAX_VALUE dollars and 99 cents.
Since we store the sign seprately, the minimum currency value
is just the negative of the maximum value.
- (b)
-
The maximum value assignable to a variable
of type
int is
java.lang.Integer.MAX_VALUE = 2147483647.
The maximum allowable value for
cents is 99.
When the data type of
dollars and
cents is changed to
int,
the maximum permissible currency value is
Integer.MAX_VALUE dollars and 99 cents.
Since we store the sign seprately, the minimum currency value
is just the negative of the maximum value.
- (c)
-
To avoid an error in the conversion, the result
(
a1 and
a2) should not exceed
Long.MAX_VALUE.
Therefore, the dollar amount should not exceed
Long.MAX_VALUE / 100.
Even though the dollar amount may not exceed
Long.MAX_VALUE / 100, the method
may not give the correct result. To assure a correct result,
the sum of the dollar amounts should not exceed
Long.MAX_VALUE / 100.