V22.0101 (Honors) ......Homework Assignment 1 ................ Fall 2006
Base Conversion

Assigned: Mon Sept 11.
Due: Sun Sept 17 at midnight, so we can talk about it on the 18th, when I will post a follow-up assignment. Please email me a single Java source file that I can compile and execute (remember to use the subject header "honors 101"). A program that does not compile and run will receive the grade 0. If you have questions, please contact me before Wednesday if possible, so we can address them in class if necessary. Late submissions will be accepted up to 7 days late, but they will be penalized 25%. They will not be accepted more than 7 days late. If you are having trouble just getting started, come and see me in my office as soon as possible.

We need to start by understanding representation in bases. Here are some examples, all representing the decimal number 25:

BaseRepresentation
2 11001
3 221
5 100
7 34
8 31
10 25
16 19
However, we got lucky with base 16. If we had wanted to represent the number 26, we would have needed another symbol for the "digit" after 9. In fact, it's standard in computer science to use A for this, and B,C,D,E,F for the "digits" for 11 through 15. Start by confining your attention to bases from 2 to 10, to avoid the need for additional symbols, and extend this if you have time.

Write and test two (static) methods:

• str2int should convert a given string representing a base b integer to the corresponding int value. Thus it has 2 parameters (the string and the base) and one return value (the converted int value).
• int2str converts a given int value to a string representing a base b integer, which it returns. Thus it has 2 parameters (the int value and the base) and one return value (the string).
When these are both written, you'll be able to test them by writing a main method to prompt the user to input a string, an input base, and an output base, all via JOptionPane.showInputDialog, then converting the input string to an int value (using the input base), and converting this to the output string (using the output base). There should be no input or output inside the methods str2int and int2str. In the main method, you can print the intermediate int value for debugging purposes, but remember that when this is displayed in decimal, yet another conversion is going on inside System.out.println, as the internal format for int is binary. The reason we need to convert to the intermediate int format is so that we can do the arithmetic necessary for the conversion. (Arithmetic in the computer is done using the binary format in the CPU; decimal is only used for input and output). Do not use Integer.parseInt or any other automatic conversion method.

The method str2int should

• process the characters of the string from left to right, using the charAt method of the String class to extract the individual characters, starting with the "zero-th" character. You can use a for loop controlled by the length of the string, which you can obtain from the length method of the String class
• make sure the characters are valid; if a character is not valid, use '0' instead and print an informative error message using System.out.println
• subtract '0' from each character to get the int value 0 or 1
• add the result to a running sum after multiplying the latter by the base b
• return the result as an int
For example, the base 2 string "11001" is processed as follows:
• the running sum is initialized to 0
• this is multiplied by 2 before 1 is added to it
• this is multiplied by 2 before 1 is added to it
• this is multiplied by 2 before 0 is added to it
• this is multiplied by 2 before 0 is added to it
• this is multiplied by 2 before the final 1 is added to it.

The method int2str reverses the process (typically, though not necessarily, called using a different base). Thus, it should

• process the number by repeatedly dividing by the new base to obtain the characters of the new representation right to left. The remainder (use the % operator) gives you the next "digit"
• add this to '0' to get the corresponding character value
• thus, build up the new string from right to left, using the concatenation operator +, starting with the empty string
Using the example twenty-five again, if we wanted to obtain the base 3 representation we would do the following:
• dividing twenty-five by 3 gives eight with 1 remainder (the last digit)
• dividing eight by 3 gives two with 2 remainder (the middle digit)
• dividing two by 3 gives zero with 2 remainder (the first digit)
This time, you will need to use a while loop, not a for loop, since you don't know in advance how many digits or bits the number has. See the top of p.25 of the text for another example.

You may wonder: what type is the base b? You'll be prompting the user to input it as a string, and then you'll have to convert it to a numerical type (probably int, but byte or short would be fine) using charAt and subtracting '0'. This conversion could be done inside str2int and int2str or in the main method, whichever you prefer. Added Sept 15: I just realized that for base 10 or higher, it's more complicated to convert the base from string to int format; since calling Integer.parseInt is not allowed, you could either write a simple method to convert a two-character string to an integer or just call str2int (with base 10, since it's assumed the base is entered in decimal). Start by writing one method and test it to make sure that it works before you go on to the second. You should try enough examples to make sure everything is working correctly. You can start by "hard-wiring" the example numbers into the main method, but in the final version of your program you must process input from the user using JOptionPane.showInputDialog. Use a while loop so that the user is prompted for more until he/she enters the empty string, which you can recognize as having length 0.

Your program should handle at least bases 2 through 10, and preferably up to 16, or perhaps higher if you can think of a clean way to do this.