Number Bases....help please

Started by
0 comments, last by haegarr 17 years, 7 months ago
I have an assignment from my computer science class stating this:
Quote:Write a Java application that will input two integers. The first is a base 10 number and the second is a number base to convert the first number to. In your application, create a recursive method called convertToBase that has two input parameters - the number and the conversion base. convertToBase should return a String which is the converted number. The reason a String is used, is because for bases above 10, letters of the alphabet are used for the digits above nine. 10 is a, 11 is b, etc. Your application should work for any number base between 2 and 36. If the inputs are not in this range, output a message to the user. For example if the inputs are 4763 and 14, the resulting String would be 1a43. Hint: A String consisting of the digits 0-9 followed by the letters a-z might be useful. To select a particular character in the string, you can use the charAt() method of the String class.
Now, when I attempted the convertToBase function, I figured that if I divide the first parameter, which I will call aNum, by the second paramenter, finalBase, then I would get an integer, result. Then I would place the remainder into a different variable by use of mod division, thus remainder function. With result, the best thing to do would be to check to see if resultis greater than finalBase, since no number can be bigger than the base it is represented in. However, things get very complicated as to showing that I might need to add another parameter to the function. Do you think I am taking the wrong approach at this, and if so, is there a simpler way to do this?
If you are reading this, then you are too attached to signatures!!
Advertisement
Due to the fact that this is homework I'll answer in a general way only:

Notice the "create a recursive method called convertToBase" in the assignment. Ask yourself how many digits the resulting string will have. It is restricted to 1 digit? If not, where came the other digits from?

Look at the example from the assignment:
"given 4763 and 14, the resulting String would be 1a43"

If you do your computations, namely
4763 / 14 = 340 (the integer division)
4763 - 340 * 14 = 3 (the remainder)
then you have just the rightmost digit of the result.

Now, since the method should work recursice, what is the break criterion, what are the argument values in the recursive invocation, and what is the respective result of the method invocations?

This topic is closed to new replies.

Advertisement