This program can be tricky for some folks. Don't be afraid to ask questions.
For this problem, nb, is the representation of an integer value, n, in base b. For example, 2110 = 258. One algorithm for converting a base 10 number to another base b involves a series of division operations, in which the divisor is always b. Each time a division is performed the remainder and quotient are saved. The quotient from each step becomes the dividend in the next step; the divisor is always b. The algorithm stops when the quotient is 0. The represenation of the number in the new base is the sequence of the remainders in reverse order (the last remainder computed is the leftmost digit--the most significant digit; the first one computed is rightmost--the least significant digit).
Example--determine the base 8 representation of 13910:
139/8 = 17 (the quotient) with a remainder of 3
17/8 = 2 (the quotient) with a remainder of 1
2/8 = 0 (the quotient) with a remainder of 2
Therefore, 13910 is 2138.
Here's a related base 10 example. Suppose you have an integer variable x in a Java program. You don't know in advance what the value of the variable is (perhaps the user has input it), but you'd like to know what the least significant digit will be when x when it is expressed as a base-10 value. I.e., you'd like to know what the least significant digit is of x10
x%10 gives you the answer. (The remainder of dividing x by 10.) As an example, if x is 123, then dividing x by 10 yields 12, with a remainder of 3. Note that 3 is the rightmost (i.e., least significant) digit of 12310. Note that because both x and 10 are integers, Java treats the "/" as integer division. I.e., it "drops" any fractional part of the division. Using our example value of x=123 again, x/10 yields 12, while x%10 yields 3.
Note that having obtained the 12 and hopefully stored it in another variable (or even back into x). You could now repeat our process to obtain the next most significant digit of the base ten representation of x, which is 2. You could repeat the process yet again to obtain the next most significant digit of the representation, which is 1. It only remains to print the 3, the 2 and the 1 in the correct order to get the result of converting the original value of x (12310) into the string "123". Here's a recap of the process:
123%10 = 3 <=== least significant digit of the base 10 number we are generating
123/10 = 12 <=== the quotient is 12, and this will be the dividend of the next step....
12%10 = 2 <=== middle digit of the base 10 number we are generating
12/10 = 1 <=== the quotient is 1, and this will be the dividend of the next step....
1%10 = 1 <=== most significant digit of the base 10 number we are generating
1/10 = 0, so we're done.
The same process will work for converting x into whatever base we want. For example, if I wanted to convert it into base 8 notation (called "octal") I would repeatedly calculate the remainder of division by 8 to access the individual digits of the octal representation. I would use and integer division by 8 to get successive quotient so as to be able to access the more significant digits. As an example, let's look at how we can calculate that 12310 is 1738:
123%8 = 3 <=== least significant digit of the base 8 representation of x
123/8 = 15
15%8 = 7 <=== middle digit of the base 8 representation of x
15/8 = 1
1%8 = 1 <=== most significant digit of the base 8 representation of x
1/8 = 0, so we're done.
In this exercise you will use this algorithm to write a program that converts a base 10 number to a 4-digit number in another base (we haven't covered enough in class yet to allow you to be able to convert if you don't know how many digits there will be). The base 10 number and the new base (between 2 and 9) will be input to the program. The start of the program is in the file BaseConvert.java. Save this file to your directory, then modify it one step at a time as follows:
// First compute place0 -- the units place. Remember this comes // from the first division so it is the remainder when the // base 10 number is divided by the base (HINT %). // Then compute the quotient (integer division / will do it!) - // You can either store the result back in base10Num or declare a // new variable for the quotient // Now compute place1 -- this is the remainder when the quotient // from the preceding step is divided by the base. // Then compute the new quotient // Repeat the idea from above to compute place2 and the next quotient // Repeat again to compute place3
String bob; //Initialize bob to be empty string int x = 567; bob = "" + x; // bob is now the String "567". bob = bob + 81; // bob is now the String "56781".So, we will use a variable of type String. Near the top of the program a variable named baseBNum has been declared as an object of type String and initialized to an empty string. Add statements to the program to concatenate the digits in the new base to baseBNum and then print the answer. Compile and run your program. Test it using the following values: Enter 2 for the base and 13 for the base 10 number -- the program should print 1101 as the base 2 value; enter 8 for the base and 1878 for the number -- the program should print 3526 for the base 8 value; enter 3 for the base and 50 for the number -- the program should print 1212.
Here is sample output from a solution to this assignment:
Base Conversion Program
Please enter a base (2 - 9): 5
The largest base 10 value we can represent with four digits in base 5 is 624
Please enter a base 10 number between 0 and 624 to convert to base 5: 123
The base 5 representation of 123 (base 10) is 0443 (base 5)
Base Conversion Program
Please enter a base (2 - 9): 7
The largest base 10 value we can represent with four digits in base 7 is 2400
Please enter a base 10 number between 0 and 2400 to convert to base 7: 2209
The base 7 representation of 2209 (base 10) is 6304 (base 7)
Base Conversion Program
Please enter a base (2 - 9): 3
The largest base 10 value we can represent with four digits in base 3 is 80
Please enter a base 10 number between 0 and 80 to convert to base 3: 65
The base 3 representation of 65 (base 10) is 2102 (base 3)