Programming Project 9

We now have binary counting and adding methods, using recursion. In this assignment you are to make a binary calculator, similar to the base 10 calculator I created. This involves three separate steps, which will be graded separately. Do them one at a time, send them in as you get them ready. Don't wait for the final due date to make sure your subtraction or multiplication methods work.

  1. Binary subtraction.
  2. Binary multiplication.
  3. The calculator itself.

1. Binary subtraction. I will describe in class an algorithm for binary subtraction using 1's complements. Implement that, using a method called subtract, with two String parameters, returning a String. The algorithm does not work correctly if subtraction leads to a negative answer. Ignore this. Recursion is not needed for the subtraction method. Make sure you test your code thoroughly.

2. Binary multiplication. Here recursion will be needed. The algorithm has three parts. Say one of the numbers (String, actually) is x.
x * emptystring = emptystring
Next, suppose the second number (String, again) is not empty. Then either it ends with 0 or with 1. I'll write aaa0 to indicate the String ending with 0, with aaa as the initial sequence of digits, and similarly with aaa1.
x * aaa0 = (x * aaa)0
x * aaa1 = (x * aaa)0 + x
We will discuss what this means in class. You are to implement it in a multiply method.

3. The calculator itself. For this, use what I wrote, and modify it so that calculations are done with String entries using the methods we have created, and there are only 0 and 1 keys instead of 0 through 9.

Solution: