This lab is pretty straightforward. Your goal is to great a program that will convert any base 10 number into a base (?) that the user inputs. Of course, since hexadecimal and the like have alphabetical characters, you may need to add some more parts to your code if you want the program to be able to handle those conversions but for the purpose of this lab, being able to convert up to base 9 will be good enough.
There is also a limit to how robust this program is just because this is a beginner lab. This program is only going to be able to convert base 10 numbers that fit in 4 characters in the desired base. So you’ll have to ask the user for what base they’d like to convert to first. Then, you’ll have to calculate the range of acceptable base 10 numbers that will result in 4 characters in the converted base and print this out to the user so that they can input a base 10 number in the acceptable range. This lab should have been explained by your lab sheet and it should have the skeleton code on it so that’s going to be what you’re working off of.
The way I went about completing this lab might be different from what you came up with. The way I used to convert bases was a trick taught to me by my teacher. I don’t know if your teacher taught you this method but here’s a link if they didn’t.
Of course, being a programmer, there are a few things we need to think about.
What global variables do you need at the beginning of the code? How many?
This being a math problem, what sort of mathematical operations do we need and in what order do we need to use those operations?
Lastly, what sort of outputs and prompt lines do I need and where for the program to be user-friendly?
These questions should be considered when writing this program. For further hints, I have hidden hints below. You just need to highlight the text within each quote box. Please only use the hints when absolutely needed. Else, you failing a test or a quiz wouldn’t be surprising. This class is very skill-based and logic-based so you need practice and familiarity with the concepts.
Hint 1: What variables?
Well, you would need a variable to store what base you want to convert to. Then, you would need variables to store the converted values after each step of the conversion and another one to keep alterations of the original base 10 number as it goes through each step of the conversion, per the remainder method (as illustrated in the link above). So, you would need 4 variables for each character of the final converted value in the new base, 1 variable to store the base you want to convert to and 1 more variable to store the value of the original inputted number in base 10 resulting in a grand total of 6 variables.
Hint 2: What math operations? What order?
Well, since you need to find remainders, using the modulus operator (%) would be good. For the part with computing what acceptable base 10 numbers to convert to, you need to use the Math.pow() method. As for the order when actually calculating the converted number in the new base, you would need to alternate between dividing and taking the remainder and that’s all I’m going to say…
Hint 3: User-friendliness?
You need to prompt for the new base. Then, you need to print out the range of accepted base 10 values to convert into your new base. Finally, you need to print out the converted value in the new base. All in complete sentences, of course.
Next Page for the Lab Sheet