COMP 2100 Lab Exercise 7: Recursive Decimal-To-Binary Conversion
(13 points)
See note at end for due date.

Electronic computers represent everything in binary. Sometimes it is useful to explicitly see the binary representation of an item. In this exercise, you will write a recursive method that converts an int value to a String consisting of '0' and '1' characters of its binary representation. For instance, integer 5 corresponds to the string "101".

For maximum 8 points credit, your program will produce a correct string for any value in the range 0 to 127. This will be performed by a recursive method that you write. Any number of extra leading zeros, including none, is OK. You will do this by filling in the provided static method called decimalToBinaryRecursive().

For maximum 10 points credit, your program will produce a correct string of length eight for any value in the range 0 to 127. It will be left-padded with zeroes as needed to reach length eight. Note:This will require the use of the non-recursive wrapper method padDecimalToBinary() to call the recursive method you've already written, decimalToBinaryRecursive(), then perform the zero-padding on its result.

For maximum 13 points credit, your program will produce a correct string of length eight for any value in the range -128 to 127. This will require the non-recursive wrapper method fullDecimalToBinary(). Details below.

The zip file ex7.zip contains one Java file: BinaryConverter.java, which contains a built-in test driver. All your work will be in BinaryConverter.java.

Implementation Notes

  1. You can get the binary representation of an integer by repeatedly dividing it by 2 and keeping the remainders. The first remainder will become the last (low-order) binary position, the second remainder will become the next-to-last, etc. Keep dividing by 2 until the result is 0. Take 24 for example: Dividing 24 by 2 yields 12 remainder 0, dividing 12 by 2 yields 6 remainder 0, dividing 6 by 2 yields 3 remainder 0, dividing 3 by 2 yields 1 remainder 1, dividing 1 by 2 yields 0 with remainder 1. The binary value is thus 11000.
  2. For the 8 point solution, fill in the stub method decimaltoBinaryRecursive(int n). The test driver will call it directly. It must be recursive. The non-recursive wrappers for both the 10 or 13 point solution also must use it.
  3. The 10 point solution, as stated above, requires a wrapper method. A stub called padDecimalToBinary(int n) is provided. This method will call the recursive method from the 8 point solution to get the corresponding string, say "11000" for 24, then left-pad it with 0's to reach length 8, e.g. "00011000" for 24. You will have to write and test this method even if you are going for the 13 point solution.
  4. For the 13 point solution: A stub called fullDecimalToBinary(int n) is provided. If the original value is non-negative, this method returns the same result as padDecimalToBinary() (and thus can simply call it). Otherwise you need to read the directions below and write the necessary code. Negative values are stored in two's complement format. To compute two's complement, first get the binary representation of the absolute value, then invert all the bits, then add 1 to the result (simulated binary numerical addition, not string concatenation). The latter is not trivial, but can be done methodically through string manipulation: in a right-to-left traversal of the binary string convert 1's to 0's until you hit a 0. Change it to 1 then copy the remaining characters as they are. Take -24 for example. The binary for 24 is 00011000. Inverting them yields 11100111. Adding one yields 11101000.
  5. Also for the 13 point solution: The solution described above requires you to obtain the binary string for the absolute value. The absolute value of -128 is 128, which falls outside the specified range of the binary string methods. Therefore the test driver will test only values in the range -127 to 127. There will not be a test case for -128.
  6. FYI, the Integer class has a method to produce binary strings from integers. Do not use it. It produces a 32 character result, too long for our purposes.

Scoring

Maximum 8, 10, or 13 points based on results of the provided test cases, which are exhaustive.

To Turn In

Drop your completed BinaryConverter.java into your DropBox. The 8 and 10 point solutions are due before the next lab period. You need to submit your program even if you want to continue working toward the 13 point solution. The 13 point solution can be turned in at any time, but you are responsible for notifying me that you have done so.
[ COMP 2100 | Peter Sanderson | Math Sciences home page | Otterbein ]

Last updated: