Question: Java Programming Recursion Help- Download DigitPlay.java . Complete the numDigits method which does the same thing as the in-class example and run the program. For

Java Programming Recursion Help-

Download DigitPlay.java. Complete the numDigits method which does the same thing as the in-class example and run the program. For example, input 3278 will generate output 4; 32780 should generate output 5. Also try a negative number.

Now add another method sumDigits, which sums those digits in the input positive integer. For example, input 321 will generate output 6 since 3+2+1 = 6. Define this method in the recursive way too. You can modify numDigits easily to get sumDigits!

Be reminded that sumDigits should be static too, so it can be invoked directly in the main method without creating an object. Add code to main() to test your method.

// ****************************************************************** // DigitPlay.java // // Finds the number of digits in a positive integer. // ****************************************************************** import java.util.Scanner; public class DigitPlay { public static void main (String[] args) { int num; //a number Scanner scan = new Scanner(System.in); System.out.println (); System.out.print ("Please enter a positive integer: "); num = scan.nextInt (); if (num <= 0) System.out.println ( num + " isn't positive -- start over!!"); else { // Call numDigits to find the number of digits in the number // Print the number returned from numDigits System.out.println (" The number " + num + " contains " + + numDigits (num) + " digits."); System.out.println (); } } // ----------------------------------------------------------- // Recursively counts the digits in a positive integer // ----------------------------------------------------------- public static int numDigits (int num) { if (num < 10) return (1); else return (1 + numDigits (num/10)); } } 

Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!