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:

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:

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

For example, the base 2 string "11001" is processed as follows:

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

Using the example twenty-five again, if we wanted to obtain the base 3 representation we would do the following: 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.

You will not get any credit if you don't have any comments in your program. Comments are essential for both programmer and user to understand what is going on. However, you shouldn't write brain-dead comments, such as   j++; //increment j . Your comments should include your name. Remember that it's OK to talk with other students in the class about your program, but that it's not OK to copy code from them or provide it to them to copy, or to share the work of writing the program. If you get useful help from someone, acknowledge it in the comments.