logo Practice-It logo

doubleDigit

Language/Type: Java recursion recursive programming
Author: Stuart Reges (on 04/02)

Write a recursive method called doubleDigit that takes an integer n and a digit d as parameters and that returns the integer obtained by replacing all occurrences of d in n with two of that digit. For example, doubleDigit(3797, 7) would return 377977 because the two occurrences of the digit 7 are replaced with two of that digit. The table below includes more examples.

Call Output
22
doubleDigit(0, 0) 0
doubleDigit(8, 6) 8
doubleDigit(55, 2) 55
doubleDigit(33, 3) 3333
doubleDigit(-101, 1) -11011
doubleDigit(323, 3) 33233
Call Output
200001
doubleDigit(12345, 6) 12345
doubleDigit(72773, 7) 77277773
doubleDigit(3445, 5) 34455
doubleDigit(54224, 4) 5442244
doubleDigit(-624243, 4) -62442443
doubleDigit(5340909, 0) 534009009

Notice that the number can be negative. Your method should throw an IllegalArgumentException if the value of d is not a 1-digit number (i.e., not between 0 and 9 inclusive). You are not allowed to construct any structured objects to solve this problem (no string, array, ArrayList, StringBuilder, Scanner, etc) and you may not use a while loop, for loop or do/while loop to solve this problem; you must use recursion.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.