Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program you will be taking numbers (as digits) entered by the user and printing them out in English one digit at a time.

In this program you will be taking numbers (as digits) entered by the user and printing them out in English one digit at a time. You will be applying your knowledge of recursion to make this program.

You will need to check if the user entered a valid number (or numbers) in a JOptionPane dialog box, if not, display an error message in a JOptionPane dialog box, and re-prompt the user.

Then go through each number given by the user, one at a time (a loop will be helpful here). Try to convert the string to an integer, if there is an error, display an error message and continue to the next number if there is one (you might need to try and catch something here).

If the number parses successfully, you should check if the number is negative, and print out the word "Negative" before any of the digits if it is.

After successfully retrieving the number, call printDigits2Num () with the number.

After finishing with the current number, loop again and complete the rest of the user entered numbers if there are any.

private void printDigits2Num (int num);

This is the recursive method to display all the digits in their English format. Note: this method must be recursive, and you may not add any extra parameters to the method. If the function is not recursive, or you change the parameters, you will receive 0 points for this program.

With each recursive call to printDigits2Num () print out the rightmost digit of num in its English format.

Determine if you need to make another recursive call, this will happen when num contains more than one digit. After the recursive call isolate the rightmost digit of num (there is a simple math operation to use here). Since num might be negative, you should check to see if the isolated digit is negative and convert it to positive (do not use multiplication to do this).

Display the final resulting English number in a JOptionPane dialog box.

There are a few things to deal with that have not already been mentioned:

? Separate each English digit by a space, but there should not be a space before the first digit or after the last digit. This will require you to do some logic to make this work properly.

? Each number needs to be on its own line (see the sample output below to understand this).

? There must be a period printed after the last digit for each number.

? Notice in sample output #8 leading zeros are ignored, Integer.parseInt () will take care of this, you do not need to do anything special

Sample output

1.) No arguments: Error Message: num1[, num2, ...:] must be valid integers.

2.) One valid single digit argument Input: 5 Output: Five.

3.) One valid multiple digit argument: Input: 12 Output: One Two.

4.) One argument that is too large: Input: 99999999999999999999999999999 Output: Error: 99999999999999999999999999999 must be a valid integer.

5.) One argument that is not a valid integer: Input: 12cse11 Output: Error: 12cse11 must be a valid integer.

6.) Two valid arguments: Input: 350 787 Output: Three Five Zero. Seven Eight Seven.

7.) One valid and one invalid argument: Input: 737 22abc Output: Seven Three Seven. Error: 22abc must be a valid integer.

8.) Two valid and two invalid arguments: Input: 999999999999999999999999 87654 32xyz 3454 Output: Error: 999999999999999999999999 must be a valid integer. Eight Seven Six Five Four. Error: 32xyz must be a valid integer. Three Four Five Four.

9.) One valid argument with leading zeros, and one valid negative number: Input: 0000002340 -1005 Output: Two Three Four Zero. Negative One Zero Zero Five.

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

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions