Question: PLEASE CHECK MY CODE BEFORE SOLVING Complete the RECURSIVE method toBase12() below that converts the given positive integer, n, into the duodecimal base (base-12) and

PLEASE CHECK MY CODE BEFORE SOLVING

Complete the RECURSIVE method toBase12() below that converts the given positive integer, n, into the duodecimal base (base-12) and returns the duodecimal String. Duodecimal numbers have place values that are powers of 12 (1's place, 12's place, 144's place, 1728's place, and so on). Each place can have digits equivalent to the decimal values 0 to 11. To represent 10 and 11, use the letters A and B, respectively. For example, the value 20A in duodecimal is: 2 x 122 + 0 x 121 + 10 x 120 = 2 x 144 + 0 x 12 + 10 x 1 = 288 + 0 + 10 = 298 To convert the given base-10 number to duodecimal, use a process of repeated division to find quotients and remainders. The quotients are used in the next step. The remainders are the digits of the result in duodecimal, in reverse order. For example, to convert the decimal number 4259 to base-12: 4259 / 12 = 354 and 4259 % 12 = 11 = "B" in duo decimal 354 / 12 = 29 and 354 % 12 = 6 29 / 12 = 2 and 29 % 12 = 5 2/ 12 = 0 and 2 % 12 = 2 so, reading the remainders in reverse order we get that 4259 in base-10 is 256B in base-12 (duodecimal). To write the method recursively: 1) consider the base case of returning the String of just the number itself when it is less than 12 (the quotient will be 0 when dividing by 12 and the remainder is the number). return "A" or "B" if the number is 10 or 11. 2) consider the recursive case of the number greater than or equal to 12. In this case, first call the method recursively using the quotient and concatenate the result (use the + operator to connect the Strings) with the result of another recursive call using the remainder. Return the concatenated result of the two recursive calls. Note: to return an integer, n, as a String, you can use the method Integer.toString(n). However you may NOT use the toString() method to convert a number to a different radix (base) since your code is supposed to be doing this task using recursion */

public static String toBase12(int n) {

String word = "";

int remainder = n % 12;

if (remainder == 0){ return "";

} if (remainder == 10){

word += "A";

} else if (remainder == 11){

word += "B";

} else {

word = remainder + word;

}

return toBase12(n /12) + word;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!