Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago