I don't know what makes you so sure that "multiplySingleDigit(x/10, y/10) );" is only going to handle single-digit numbers...
Your basic problem is that you wrote code that yourself can't read or debug. You devised something that could work, but there are still some ideas that aren't completely clear, and nothing in your code to help you make them clearer. Don't try to be too clever. So:
- replace this humongous one-liner "result" computation by the same in "slow motion"
- all intermediate computations in their own variables (makes it so much easier to debug)(*)
- gives them meaningful names, for instance: "int lowTopByHighBottom= multiply(x % 10, y / 10);"
- add useful print statements (that you will remove before returning the assignment of course).
- it is even better if they help you track where you are in the recursion.
- Add to all the multiply*() methods an indent parameter for the trace
- Add some indent when calling recursive methods, ie:
Code:
static int multiplySingleDigit(String indent,int x, int y) {
System.out.printf("%s multiplySingleDigits(%d,%d)\n", indent, x, y);
[...]
}
static int multiply(String indent, int x, int y) {
System.out.printf("%s multiply(%d,%d)\n", indent, x, y);
[...]
int lowTopByLowBottom=multiplySingleDigit("--"+indent,x % 10, y % 10);
int lowTopByHighBottom= multiply("--"+indent,x % 10, y / 10);
[...]
Eventually things will sort themselves out that way.
(*) Beginning coders are often ashamed to use variables for intermediate results, thinking one-liners are the hallmark of the seasoned programmer. Nothing is farther from the truth. Good programmers don't write much comments, but they do write very self-explanatory code.
Bookmarks