Overview
In this lab, you will write basic Number and Digit classes that will allow us to represent numbers of different bases. These can be very easily applied to a base converter program or any program that requires numbers in different bases (clocks, angles in geometry, etc.)
Step 0: Create a Digit class. This class represents one digit in any number. It knows its
decimal value and its base. Include appropriate variables, constructor(s) and a toString
method. The toString method for a class will take no parameters and return a String that
represents the object. This allows us to do things like print the object more easily. Suppose
I had the following lines of code in a main:
Without a toString, the output will be something funny (and meaningless). With a
toString, the value returned by that method will be the output for d1. Perhaps in this
case, it would output D since that is the character equivalent for the value 13 in bases
higher than 10. Try the lines of code above with and without a toString method.
Step 1: Create a Number class. This class represents a multi-digit number – i.e. it is made up of a bunch of Digit objects. It also knows its decimal value and its base. Write the appropriate variables and constructor. The constructor will use a helper method that will convert the base 10 value to the appropriate set of digits in the given base. Add a toString method to this class.
Step 2: To each class, add an increment method. In Digit, the increment method will increase the value in the base by one, resetting when necessary. It will return a boolean value to indicate whether or not the digit had a carry (such as going from 9 to 0 in base 10). In Number, the increment method will increase its number by 1 (and the set of digits will be change to reflect this as well).
Step 3: Include a console program in the main method that will act as a base converter. It
will continually ask the user what base 10 value to be converted and the desired base, then
it will display the results on the screen. After the initial conversion, it will repeatedly give
the user the option to either increment the current number or create a new one. For this
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
23 | 24 | 25 | 26 | 27 | 28 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | 1 | 2 | 3 | 4 | 5 |
Get Free Quote!
441 Experts Online