Jump to content
• Advertisement

# Number Bases....help please

This topic is 4341 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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?

#### Share this post

##### Share on other sites
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?

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10
• Advertisement

• 14
• 13
• 14
• 73
• 22
• ### Forum Statistics

• Total Topics
632143
• Total Posts
3004331

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!