Representing an integer value with a specified radix.
In this assignment, you will implement and test a method that constructs a String
representation of a specified long
value, using a specified radix (number base).
In the edu.cnm.deepdive.Radices
class, the toString
method is declared with the following signature, return type, and modifiers:
public static String toString(long value, int radix) throws IllegalArgumentException
The implementation must not change the modifiers, return type, method name, parameter types/number/order, or possible exceptions shown above. For more method declaration details, see the Javadoc documentation.
Important: For this assignment, you are expected to implement an algorithm to construct the desired representation in your code.1 Simply invoking a method (whether in the standard Java library or a 3rd-party library) that constructs the representation, and then returning the value returned by that other method, is not sufficient. In particular, your implementation must not invoke the toString
method of any of the Number
subclasses; similarly, it must not invoke Formatter.format
, String.format
, PrintStream.printf
, or any other method that directly constructs a String
representation of a number.
If radix
is not in the range 2–36 (inclusive), IllegalArgumentException
must be thrown. (Mathematically speaking, it’s possible to use negative or non-integral radices, but these are out of the current scope.)
For digits, the representation will use the first radix
characters of '0'
–'9'
and 'a'
–'z'
.
If any of the characters in 'a'
–'z'
are used (i.e. for radix > 10
), the lowercase forms must be used.
The allowed range of value
is the entire range of long
, including negative values. For a negative value
, the representation returned must the same as the representation that would be returned for Math.abs(value)
, but with '-'
(the minus sign) prepended.
If value == 0
, and if radix
is valid (according to the above), the String
"0"
must be returned.
The values returned must not include the underscore digit separator characters.
You may find it useful to create one or more additional static
methods as “helpers”; the problem can be solved without doing so, however.
Do not hesitate to declare any constants (static final
fields) that you feel might simplify or clarify your code.
The method to be completed includes a TODO
comment to that effect.
The following test cases will be used for unit testing the Radices.toString
method.
value |
radix |
Expected return value of Radices.toString(value, radix) |
Expected exception |
---|---|---|---|
-250 |
2 |
"-11111010" |
(none) |
1000 |
5 |
"13000" |
(none) |
-5_000 |
7 |
"-20402" |
(none) |
7_500 |
8 |
"16514" |
(none) |
-100_000 |
16 |
"-186a0" |
(none) |
2_000_000 |
20 |
"ca000" |
(none) |
5_000_000_000 |
36 |
"2aovcow" |
(none) |
0 |
2 |
"0" |
(none) |
0 |
10 |
"0" |
(none) |
10 |
1 |
(none) | IllegalArgumentException |
100 |
0 |
(none) | IllegalArgumentException |
1_000 |
-2 |
(none) | IllegalArgumentException |
10_000 |
40 |
(none) | IllegalArgumentException |
See “Alternative Radices: Conversions Between Radices” for a pseudocode expression of an algorithm to construct a representation of an integral value in any integral base greater than or equal to 2. ↩