Write a static recursive method that returns the number of digits in the integer passed to it as an argument of type int. Allow for both positive and negative arguments. For example, ?120 has three digits. Do not count leading zeros. Embed the method in a program, and test it.
A technique similar to that in RecursionDemo2, Listing 11.4, can be used for this Project First change the number to positive if it is negative. The base case is when the number has just one digit, which returns 1 if the result of the truncated division of the number by 10 is zero. If non-zero, a recursive call is made to the method, but with the original number reduced by one digit, and (1 + the value returned by the recursive call) is returned. In this fashion, each recursive call will add 1, but not until the base case is executed. The base case returns a 1 and the stacked calls can now “unwind,” each call executing in turn and adding 1 to the total. The first call is the last to execute and, when it does, it returns the number of digits.
See the code in NumberOfDigitsDemo.java.
You might also like to view...
The data retrieved by a query is called a(n) ________
Fill in the blank(s) with correct word
Which statement is true when a superclass has protected instance variables?
a. A subclass object can assign an invalid value to the superclass’s instance variables, thus leaving an object in an inconsistent state. b. Subclass methods are more likely to be written so that they depend on the superclass’s data implementation. c. We may need to modify all the subclasses of the superclass if the superclass implementation changes. d. All of the above.